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
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
Share on other sites

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

Link to comment
Share on other sites

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

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.