3

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..,

najmal
  • 85
  • 1
  • 1
  • 9
  • you have to change the name of your table. Oprion is a mysql keyword and so it will cuse syntax error while executing queries with php. Also start using mysqli_ or PDO since mysql_ is been deprecated. – AeJey Jun 04 '14 at 04:49

5 Answers5

5

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.

AeJey
  • 1,447
  • 20
  • 40
  • when using mysqli displays error Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\wamp\www\new test\option.php on line 22 – najmal Jun 04 '14 at 05:04
  • Check this tutorial. Good one for beginners. Try following the object oriented method as in this tutorial. You can learn quickly and object oriented method has many advantages too. http://codular.com/php-mysqli – AeJey Jun 04 '14 at 05:41
  • $db = new mysqli('localhost', 'user', 'pass', 'db_name'); – AeJey Jun 04 '14 at 05:41
1

Try this code, I changed the name of select to names and

use backticks around table name

    <form action="option.php" method="GET">
    Name :<select name="names">
    <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>

    <?php
    if(isset($_GET['names']))
    {
        $name=$_GET['names'];
        $c=mysql_connect("localhost","root","");
        mysql_select_db("test");
        $ins=mysql_query("INSERT INTO `option` 
                          (name)
                          VALUES ('$name')",$c) or die(mysql_error());
        if($ins)
        {
            echo "<br>".$name."inserted";
        }

    }

?>
1

Html Code Is:

<select name="a">
    <option value="val1">value1<option>
    <option value="val2">value2<option>
    <option value="val3">value3<option>
</select>

PHP Code Is:

<?php
    $a=$_POST[a];
?>

The above php code will help you to fetch data from the selected option into php variable $a. From there on ou can insert your data into the query simply as you inserted before.

Rahul Goel
  • 66
  • 1
  • 1
  • 8
0

in your code mysql_query("insert into option (name) values ('$name')",$c); here is no need to add connection variable to that function.
you can just write mysql_query("insert into option (name) values ('$name')")or die(mysql_error()); reason for calling function mysql_error() is that you came to know what type of error is coming..
and another thing is you should change the name of your table from option to option1 or anything else...then it will work fine..no worry...:)

Nils
  • 11
  • 4
0

please use POST not GET because you want to insertdatas in datdabse. like this :

  <form action="option.php" method="post`">