max
max

Reputation: 7

error when try to insert data into database

HTML code

<form id="form1" name="addAnnouncement" method="post" action="ownerAddAnnouncement_exec.php" onsubmit="return validateForm()">
  <label style="font-size:18px">Title:
  <input type="text" name="title" />
  </label>
  <p>
  <label style="margin-left: -36px; font-size:18px;">Description:
  <textarea name="description" rows="6" cols="60"></textarea>
  </label>
  </p>
  <label style="font-size:18px">Date & Time: <br>
  From
  <input type="text" name="from" /> <br>
  To <input type="text" name="to" />
  </label>  <br>
  <label style="font-size:18px">Venue
  <input type="text" name="venue" />
  </label>        
  <p>
  <label>
  <input type="submit" name="Submit" value="Submit" />
  </label>
  </p>
  </fieldset>
  </form>

PHP code

<?php


$title = $_POST['title'];
$description = $_POST['description'];
$from = $_POST['from'];
$to = $_POST['to'];
$venue = $_POST['venue'];


$link = mysql_connect("localhost","root","") or die();
$db = mysql_select_db("condo") or die("no database found");
$insert_sql="INSERT INTO announcement (title, description, from, to,venue, status)
             VALUES('$title', '$description', '$from', '$to','$venue', 'Pending')"; 

$sql_result=mysql_query($insert_sql) or die("Error in inserting data due to ".mysql_error());

if($sql_result)
    echo "Succesfully insert new data. Please log in back";

        else
    echo "Error in inserting new data";

?>

an error like this ("Error in inserting data due to 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 'from, to, status) VALUES('melvin', 'sdsaadsd', 'wew', 'ewrerw', 'we3', 'Pendi' at line 1" ) is show out when try to insert a data into database.

Anyone please help me fix the code.i have been stuck at here for 1 hour.

Upvotes: 0

Views: 197

Answers (4)

Nasir Iqbal
Nasir Iqbal

Reputation: 872

Regarding current error it is about reserved keywords like from as field name, so to avoid it either rename your db column or enclose it in back-quotes like `from`

further you may face other errors as you are ignoring many good practices in your code, for example

Upvotes: 0

Venkata Krishna
Venkata Krishna

Reputation: 1776

From is a keyword. And also To. It is not recommended to use them. But if you can't avoid it and still want to use them, add backquote ` like below in your insert query :

INSERT INTO announcement (`title`, `description`, `from`, `to`, `status`) VALUES('$title', '$description', '$from', '$to', 'Pending')

Hope this helped.

Upvotes: 0

Deval Khandelwal
Deval Khandelwal

Reputation: 3548

You should escape reserved keywords using backticks. Currently, you are using the following reserved keywords - From and To Try this :-

$insert_sql="INSERT INTO `announcement` (`title`, `description`, `from`, `to`,`venue`, `status`)
         VALUES('$title', '$description', '$from', '$to','$venue', 'Pending')"; 

Upvotes: 0

Divakar Gujjala
Divakar Gujjala

Reputation: 803

Display the field names with in ``.

Convert the insert statement to

$insert_sql="INSERT INTO announcement (`title`, `description`, `from`, `to`,`venue`, `status`)
             VALUES('$title', '$description', '$from', '$to','$venue', 'Pending')"; 

Upvotes: 2

Related Questions