stublackett Posted March 21, 2008 Share Posted March 21, 2008 Hi, I've got a DB Table Setup for Announcements But I'm getting the error : 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 ''3')' at line 1 My code is as follows <?php include("dbconnect.php"); $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($dbname,$db); if(isset($_POST['Submit'])) { $title = mysql_escape_string($_POST['title']); $description = mysql_escape_string($_POST['description']); $subject = $_POST['subject']; // If everything is filled out print the message. if($title && $description) { { // If all is ok, Insert into DB $sql = "INSERT INTO $db_table4(title,description,time,subject) VALUES ('$title','$description',NOW()),'$subject')"; ($result = mysql_query($sql ,$db) or die(mysql_error())); } echo "<b>Thank you! Your announcement has been added!<br>You will be redirected to the Home Page in(4) Seconds"; echo "<meta http-equiv=Refresh content=4;url=index.php>"; } } // If the form has not been submitted, display it! else {//begin of else ?> <form method="post" action="<?php echo $PHP_SELF ?>"> <fieldset> <div> <label for="title">Title : <em>(*required*)</em> </label> <input name="title" type="text" id="title" size="30" class="txt" tabindex="1" /> </div> <div> <label for="description">Description : <em>(*required*)</em> </label> <textarea name="description" cols="45" rows="5" wrap="virtual" class="multiline" id="description" tabindex="2"></textarea> </div> </fieldset> <p> <label for="subject">For School Subject : <select name="subject" id="subject"> <option value="1">Design & Technology</option> <option value="2">English</option> <option value="3">Humanities</option> <option value="4">ICT</option> <option value="5">Mathematics</option> <option value="6">Modern Foreign Languages</option> <option value="7">Physical Education</option> <option value="8">Science</option> </select> </label> </label> </p> <div> <fieldset class="submit"> <input name="Submit" type="submit" class="btn" value="Submit News" /> <input name="Reset" type="reset" class="btn" value="Reset" /> </fieldset> </div> </form> <?php }//end of else ?> With the error showing the "3" I'm assuming its the select menu that is causing the issue, Any help is appreciated Thanks Quote Link to comment Share on other sites More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 You $subject variable probably has a ' in it. Try this: // If all is ok, Insert into DB $sql = "INSERT INTO $db_table4(title,description,time,subject) VALUES ('".mysql_real_escape_string($title)."','".mysql_real_escape_string($description)."',NOW()),'"mysql_real_escape_string($subject)."')"; Quote Link to comment Share on other sites More sharing options...
stublackett Posted March 21, 2008 Author Share Posted March 21, 2008 That brought up an unexpected T_STRING on the SQL Insert line? I can see what your saying though, I'll have a play about with it see if I can it to work that way Quote Link to comment Share on other sites More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 oops...missed a period... $sql = "INSERT INTO $db_table4(title,description,time,subject) VALUES ('".mysql_real_escape_string($title)."','".mysql_real_escape_string($description)."',NOW()),'".mysql_real_escape_string($subject)."')"; Quote Link to comment Share on other sites More sharing options...
stublackett Posted March 21, 2008 Author Share Posted March 21, 2008 Still no joy with that fella, I'm now getting that SQL Error once again :'( Quote Link to comment Share on other sites More sharing options...
AndyB Posted March 21, 2008 Share Posted March 21, 2008 You can guess for weeks, or display the query that generates the error - which should help you solve the problem. $result = mysql_query($sql ,$db) or die("Error: ". mysql_error(). " with query ". $sql); // what's the real problem Quote Link to comment Share on other sites More sharing options...
stublackett Posted March 21, 2008 Author Share Posted March 21, 2008 Cheers for that AndyB that worked a treat, Spotted my error straight away Quote Link to comment 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.