Prefect Posted December 31, 2008 Share Posted December 31, 2008 Hello all. I'm new to PHP and this forum, and need some guidance on the subject of inserting $_POST form data to a sql database. I have the two scripts noted below but have been unable to make them work. Please show me the corrected syntax and what I'm doing wrong. Many thanks. HTML Form <html><head><title></title></head> <body> <form action = "customer_2.php" method = "POST"> <br> <input type = "text" name = "firstname" /> First Name <br><br> <input type = "text" name = "lastname" /> Last Name <br><br> <input type = "submit" value = "Submit Registration" /> <br><br> </form> </body> </html> PHP insert script $conn = mysql_connect("localhost", "root", ""); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("customer_temp_database")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $query = "INSERT INTO customer (firstname, lastname) VALUES ('$_POST[firstname]','$_POST[lastname]')"; Link to comment https://forums.phpfreaks.com/topic/139051-inserting-_post-form-data-to-sql-database/ Share on other sites More sharing options...
revraz Posted December 31, 2008 Share Posted December 31, 2008 Not too bad, one thing you forgot is the mysql_query($query) line. You'll also want to sanitize those POST variables before inserting as well as putting them in your insert statement properly. Link to comment https://forums.phpfreaks.com/topic/139051-inserting-_post-form-data-to-sql-database/#findComment-727259 Share on other sites More sharing options...
chronister Posted December 31, 2008 Share Posted December 31, 2008 <?php $conn = mysql_connect("localhost", "root", ""); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("customer_temp_database")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $firstName = mysql_real_escape_string($_POST[firstname]); $lastName = mysql_real_escape_string($_POST[lastname]); $query = "INSERT INTO customer (firstname, lastname) VALUES ('$firstName','$lastName')"; $result = mysql_query($query); if(mysql_affected_rows() > 0) { echo 'Inserted to database sucessfully'; } ?> Nate Link to comment https://forums.phpfreaks.com/topic/139051-inserting-_post-form-data-to-sql-database/#findComment-727289 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.