web_designer 7 Report post Posted July 5, 2012 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 nearI 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
jlhaslip 4 Report post Posted July 9, 2012 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
web_designer 7 Report post Posted July 9, 2012 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
jlhaslip 4 Report post Posted July 11, 2012 Glad it is resolved.Happy to help when I can. Share this post Link to post Share on other sites