Jump to content

Insert Error


stublackett

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/97239-insert-error/
Share on other sites

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)."')"; 

Link to comment
https://forums.phpfreaks.com/topic/97239-insert-error/#findComment-497583
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.