I have a contact/inquiry form in my new website. I wanted this form to insert data into MySQL db, then to get every new form submission to my email, i.e. by pulling out from the db. The data insertion works well but I couldn't get the email from the database. I used separate php script files for both processes. Can anyone help me with this? Below here is the code. <?php $linkdb = mysqli_connect("localhost", "username", "password"); if(!$linkdb){ die("ERROR: Could not connect. " . mysqli_connect_error()); }else{ mysqli_select_db("my_database"); } $query = "SELECT * FROM table_name"; $result = mysqli_query($linkdb,$query); // Sending contact message from database via email if(mysqli_num_rows($result) > 0){ while($row = mysqli_fetch_array($result)){ // variables for sending emails $to = 'info@example.com, pr@example.com'; $subject = 'Message from contact us form'; $copyrecip = 'gm@example.com'; // Body of the message $message = '<html><body>'; $message .= '<h2><b>Inquiry Topic:</b>' . $row['inquiry_topic'] . '</h2>'; $message .= '<p style="color:#031634;">Message:' . $row['message'] . '</p>'; $message .= '<ul style="color:#01017e;">'; $message .= '<b>Company name:</b>' . $row['company'] . '</li>'; $message .= '<li><b>Region:</b>' . $row['region'] . '</li>'; $message .= '<li><b>Location address:</b>' . $row['address'] . '</li>'; $message .= '<li><b>Office phone:</b>' . $row['land_phone'] . '</li>'; $message .= '<li><b>Mobile phone:</b>' . $row['mobile_phone'] . '</li>'; $message .= '</ul></body></html>'; $message .= stringwordwrap($message, 70, "\r\n"); // Setting content-type header for an HTML email $headers = 'MIME-Version: 1.0' . "\r\n" . $headers = 'Content-type: text/html; charset=iso-8859-1'; // Email headers $headers = 'From: '.$row['contact_name']."\r\n" . 'To: '.$to."\r\n". 'Cc: '.$copyrecip."\r\n". 'Reply-To: '.$row['contact_email']."\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); } } //closing database connection mysqli_close($linkdb); ?> PHP:
You have the example of PHP code that gets the data from the database: So you could use it and get any data you need from the database. There should be no difficulties.
Using OOP would really help make this cleaner and easier to debug. Example: https://code.tutsplus.com/tutorials/real-world-oop-with-php-and-mysql--net-1918