rasraziel Posted February 3, 2012 Share Posted February 3, 2012 Hello, guys. I am experiencing some problems with an INSERT statement in this page. It simply won't write to the database! I added echo at the bottom to check my variables and they print the values just fine. I checked the database, table and datafield names and everything is correct, plus I don't have any issues with the other 25 tables of my database. I'm using XAMPP btw... Any help would be appreciated! <!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="content-type"> <title>Doctors</title> <link rel="stylesheet" href="style.css" media="screen" /> </head> <body > <?php session_start(); $inc_code=$_SESSION['incident']; $doc_code=$_REQUEST['doctor_code']; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } $mdb = "registry_db"; mysql_select_db($mdb, $con); mysql_query("SET NAMES 'utf8'", $con); ?> <div id="myform"> <p> <h2>Doctor in charge</h2> </p> <?php $sql="INSERT INTO doctors_per_incident(Incident_code, doctor_code) VALUES ('$inc_code', '$doc_code')"; echo "1 record added"." ".$inc_code." ".$doc_code; mysql_close($con); ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/256329-sql-insert-statement-is-not-working-in-php-page/ Share on other sites More sharing options...
litebearer Posted February 3, 2012 Share Posted February 3, 2012 the session_start( ) function must come before any HTML, including blank lines, on the page. Quote Link to comment https://forums.phpfreaks.com/topic/256329-sql-insert-statement-is-not-working-in-php-page/#findComment-1314008 Share on other sites More sharing options...
spiderwell Posted February 3, 2012 Share Posted February 3, 2012 you dont appear to be actually executing the sql statement in the variable $sql, so that might be why Quote Link to comment https://forums.phpfreaks.com/topic/256329-sql-insert-statement-is-not-working-in-php-page/#findComment-1314009 Share on other sites More sharing options...
rasraziel Posted February 3, 2012 Author Share Posted February 3, 2012 you dont appear to be actually executing the sql statement in the variable $sql, so that might be why You're absolutely right! How the hell did I miss that? Thanks guys! Problem solved! <!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="content-type"> <title>Doctors</title> <link rel="stylesheet" href="style.css" media="screen" /> </head> <body > <?php session_start(); $inc_code=$_SESSION['incident']; $doc_code=$_REQUEST['doctor_code']; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } $mdb = "registry_db"; mysql_select_db($mdb, $con); mysql_query("SET NAMES 'utf8'", $con); ?> <div id="myform"> <p> <h2>Doctor in charge</h2> </p> <?php $sql="INSERT INTO doctors_per_incident(Incident_code, doctor_code) VALUES ('$inc_code', '$doc_code')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"." ".$inc_code." ".$doc_code; mysql_close($con); ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/256329-sql-insert-statement-is-not-working-in-php-page/#findComment-1314012 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.