insert values from a drop down list to database table

najmal picture najmal · Jun 4, 2014 · Viewed 78.3k times · Source

I am new to PHP. When i am trying to insert the value from drop down list to database table showing some errors

the code

<html>
<head>
<title>OPTION</title>
</head>
<body>
<form action="option.php" method="get">
Name :<select name="name">
<option value="name1">name1</option>
<option value="name2">name2</option>
<option value="name3">name3</option>
<option value="name4">name4</option>
</select><br>
<input type="submit" name="submit" value="Insert">
</form>
</body>
</html>
<?php
if(isset($_GET['submit']))
{
    $name=$_GET['name'];
    $c=mysql_connect("localhost","root","");
    mysql_select_db("test");
    $ins=mysql_query("insert into option (name) values ('$name')",$c);
    if($ins)
    {
        echo "<br>".$name."inserted";
    }
    else
    {
        echo mysql_error();
    }
}
?>

when i am trying out put showing this 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 'option (name) values ('name3')' at line 1

Thank you..,

Answer

AeJey picture AeJey · Jun 4, 2014

you have to change the name of your table (option). Option is a mysql keyword and so it will cause syntax error while executing queries with php.

Change the table name to option_test or something and make appropriate changes in your php code too. Then it will work.

Also start using mysqli_ or PDO since mysql_ has been deprecated from PHP5 onwards.