0

I am trying to add a data into my database and everytime after I click the 'submit' button, the data isn't inserted into the database. Here is my code.

<?php include("/common/sql.php") ?>

<?php
if (isset($_POST['submit']))
    {
    mysqlquery("INSERT INTO categories (id, name) VALUES('NULL', '$_POST[name]')"); 
    }
?>

<html>
<head>
<title>Add a category</title>
</head>
<body>
    <h1>Add a category</h1>

    <form action='add_category.php' method='POST'>
        <div>
            <label>Name: <input type='text' name='name' value=''></label>
        </div>
        <div>
            <input type='submit' value='Add Category'>
        </div>
    </form>
</body>
</html>
2
  • ... VALUES( NULL, ..., not ... VALUES('NULL', ... Commented Aug 25, 2011 at 11:24
  • Daniel, please read this php-MySQL tutorial: tizag.com/mysqlTutorial Commented Aug 25, 2011 at 11:40

5 Answers 5

3

1. Query

Take a look at the difference between my query bellow and your query:

mysqlquery("INSERT INTO categories (name) VALUES('$_POST[name]')"); 

You can see that we've removed id under the assumption you are auto-generating this.

2. Function Name

I'm hoping (praying) that you haven't wrapped mysql_query into a function that looks like:

function mysqlquery($sql){
  return mysql_query($sql);
}

If that is not the case, use mysql_query().

3. Form submit name

As pointed out by strauberry, you also need to add the name attribute to your submit button:

<input name='submit' type='submit' value='Add Category'>

4. Side Notes

As a side note, you should defiantly be sanitizing $_POST['name'] somewhere - at least, mysql_real_escape_string. Also, when developing, it would be very helpful to you to set error reporting:

error_reporting(E_ALL);
ini_set('display_errors', '1');

and make use of mysql_error().

Sign up to request clarification or add additional context in comments.

Comments

1

It is not going inside if condition.

Change

<input type='submit' value='Add Category'>

to

<input name='submit' type='submit' value='Add Category'>

Then you have to change mysqlquery to mysql_query and 'null' to NULL (without quotation)

Comments

1

change it to

$name = mysql_real_escape_string($POST['name']); 

mysql_query("INSERT INTO categories (id, name) VALUES(NULL, '$name')");

also give name to submit button as

<input type='submit' value='Add Category'  name='submit'>

edit:

also you should give id as auto generated in table structure and give query as:

 mysql_query("INSERT INTO categories (name) VALUES('$name')");

1 Comment

You can't insert 'NULL' into id (assuming its an int)
0

Your boolean expression if (isset($_POST['submit'])) is evaluated false, because there's no such value. Therefore the mysql_query() command is never executed! You have to give your button a name

<input type='submit' value='Add Category' name='submit' />

To see which data is transfered, use print_r($_POST);

2 Comments

The error is not in your sql statement, there are a couple of errors in that as well.
@Johan you're right :-) I didn't look at the statement ^^ Edited my answer
-1

Assuming that you have successfully connected to your db, I'd recommend you first correct your syntax:

mysql_query("INSERT INTO categories (name) VALUES('$_POST[name]')");

I've made an assumption, that 'id' is set as an int and to auto increment in your database, so you don't need to NULL it, which, by the way, will be sent as a string as it's within quotes.

What might help is some error outputting add this after your query and don't forget to close the connection to the server.

Your code should look like this:

$name = mysql_real_escape_string($_POST[name]);    
$mysql_query("INSERT INTO categories (name) VALUES('$name')");

    if (!mysql_query($mysql_query,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 new record added";

    mysql_close($con)

I hope this helps.

3 Comments

@johan Thanks for trolling. If you spot an error, you should contribute with a solution otherwise you're just wasting everyone's time.
I did contribute, I marked the answer as "not useful". As far as a solution goes, I edited @mithunsatheesh answer to fix the errors in that answer. So I did do some work in addition to my "trolling". I see you have copied some of the code. You still have an error though mysql_query("INSERT INTO categories (name) VALUES('$name')"); if (!mysql_query($sql,$con)) should be $sql = "INSERT INTO categories (name) VALUES('$name')"; if (!mysql_query($sql,$con)) Other than that this code is much improved.
Oh and the first part should be $name = $_POST['name']; /*extra quotes*/ $name = mysql_real_escape_string($name); /*don't forget to assign the escaped var */

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.