Jump to content
xisto Community
web_designer

Error When Inserting Data To A Database Using Form

Recommended Posts

Hi everyone, I have this error message appears when I try to insert data to my database through a form, here is the error message:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

I checked everything and it looks fine, here is the code for inserting the data:

<form id="form2" name="form1" method="POST" action="<?php echo $editFormAction; ?>">			    <p>				  <label for="chapter">Chapter</label>				  <input type="text" name="chapter" id="chapter" />			    </p>			    <p>				  <label for="pages">Pages</label>				  <input type="text" name="pages" id="pages" />			    </p>			    <p>				  <label for="startDate">Start Date</label>				  <input type="text" name="startDate" id="startDate" />			    </p>			    <p>				  <label for="needsBy">Needs By</label>				  <input type="text" name="needsBy" id="needsBy" />			    </p>			    <p>				  <label for="completed">Completed</label>				  <input type="text" name="completed" id="completed" />			    </p>			    <p>				  <input name="hiddenField" type="hidden" id="hiddenField" value="<?php echo $row_rsChapters['bookID']; ?>" />			    </p>			    <p>				  <input type="submit" name="button" id="button" value="Submit" />			    </p>			    <p>				  <input type="hidden" name="MM_insert" value="form1" />			    </p>			    <p> <a href="<?php echo $logoutAction ?>">Log out</a></p>			  </form>


and this is the php code for the insertion method:


$bookVar = $_GET['bID'];if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {  $insertSQL = sprintf("INSERT INTO chapterstatus (bookID, chapter, pages, startDate, needsBy, completed) VALUES (" .$bookVar. ", %s, %s, %s, %s, %s)",					   GetSQLValueString($_POST['chapter'], "int"),					   GetSQLValueString($_POST['pages'], "text"),					   GetSQLValueString($_POST['startDate'], "text"),					   GetSQLValueString($_POST['needsBy'], "text"),					   GetSQLValueString($_POST['completed'], "text"));	   	     mysql_select_db($database_connWill, $connWill);  $Result1 = mysql_query($insertSQL, $connWill) or die(mysql_error());  $insertGoTo = "addChapters2.php";  if (isset($_SERVER['QUERY_STRING'])) {    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";    $insertGoTo .= $_SERVER['QUERY_STRING'];  }  header(sprintf("Location: %s", $insertGoTo));}mysql_select_db($database_connWill, $connWill);$query_rsStudent = "SELECT * FROM students";$rsStudent = mysql_query($query_rsStudent, $connWill) or die(mysql_error());$row_rsStudent = mysql_fetch_assoc($rsStudent);$colname_rsBooks = "-1";if (isset($_GET['bID'])) {  $colname_rsBooks = $_GET['bID'];}

so where is the error I really don't know, so any ideas why I got this error message? thanks in advance for any help.

Share this post


Link to post
Share on other sites

There is an error in the SQL code, but since the Form data is changing on each submission, we need to see the actual mysql code as it is failing.Use a php Echo statment just before the INSERT to display the MYSQL statement and then check to see if the same code complete with your data will work in another situation.Use the echo'd out statement in the mysql console or PHPMYADMIN to see if the statement works in another client.

$insertSQL = sprintf("INSERT INTO chapterstatus (bookID, chapter, pages, startDate, needsBy, completed) VALUES (" .$bookVar. ", %s, %s, %s, %s, %s)",                                           GetSQLValueString($_POST['chapter'], "int"),                                           GetSQLValueString($_POST['pages'], "text"),                                           GetSQLValueString($_POST['startDate'], "text"),                                           GetSQLValueString($_POST['needsBy'], "text"),                                           GetSQLValueString($_POST['completed'], "text"));           echo $insertSQL ; // this should printout the actual SQL being tested. Cut and paste into another client        mysql_select_db($database_connWill, $connWill);  $Result1 = mysql_query($insertSQL, $connWill) or die(mysql_error());  $insertGoTo = "addChapters2.php";

Share this post


Link to post
Share on other sites

Thanks for the suggestion jlhaslip, I really appreciate your help. but the issue is resolved, I was working on it the past few days, and it seems that it is only a matter of organizing error, because I was trying to insert data to the chapters table where I didn't insert a book first :Ptherefore I got this error, because as I said before, the whole database is related in many to many relationship. Thanks for your help again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.