开发者

Problem with inserting data to mySQL database

开发者 https://www.devze.com 2023-03-30 11:47 出处:网络
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.

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>
        &l开发者_开发技巧t;/div>
        <div>
            <input type='submit' value='Add Category'>
        </div>
    </form>
</body>
</html>


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().


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)


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


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消