Jump to content

SQL INSERT statement is not working in php page.


rasraziel

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.