This study focused on enhancing the sensitivity and selectivity to detect melamine by utilizing a photoelectrochemical method. This was achieved by combining a melamine-imprinted polymer with a CuO/g-C3N4 nanocomposite, which was synthesized through chemical precipitation and calcination. The resulting nanocomposite exhibits improved carrier mobility and photoelectrochemical properties. A molecularly imprinted receptor for selective detection was created through bulk polymerization with methacrylic acid and a melamine template. The characterization of the nanocomposite was performed using X-ray photoelectron spectroscopy for the chemical oxidation state, X-ray diffraction patterns for the crystalline structure, and ultraviolet/visible/near-infrared spectroscopy for optical properties. The CuO/g-C3N4 nanocomposite exhibits photoactivity under visible light. The modified electrode, incorporating the CuO/g-C3N4 nanocomposite and melamine-imprinted polymer, demonstrates a linear detection range of 2.5 to 50 nM, a sensitivity of 4.172 nA/nM for melamine, and a low detection limit of 0.42 nM. It shows good reproducibility and high selectivity to melamine, proving effective against interferences and real samples, showcasing the benefits of the molecularly imprinted polymer.