Simple, fast, and sensitive molecularly imprinted composite thin-film-based electrochemical sensor developed by using in situ co-electropolymerization of aniline and acrylic acid in the presence of melamine as a template is described here. The prepolymerization complex formation was studied by using Fourier transform infrared (FTIR) spectrophotometry, while the film formation was performed and characterized by cyclic voltammetry, Fourier transform infrared (FTIR), and scanning electron microscopy (SEM). The optimization of important parameters and removal of melamine generated the binding sites in the polymer matrix, which can recognize melamine specifically. Electrochemical measurements were performed to achieve the linear range, the limit of quantification, and limit of detection of 0.1–180, 0.0573, and 0.0172 nM, respectively. The sensitivity of the sensor was attributed to the synergistic effects of amine from aniline and the carboxylic group from acrylic acid to form multiple noncovalent interactions with the template. Melamine-spiked infant formula and raw milk were analyzed by the developed sensor, and the recovery range of 95.87–105.63% with a relative standard deviation of 1.11–2.23% was obtained. The results showed that the developed sensor using the new composite polymer receptor is promising for the online monitoring of melamine in the food industries in the future.