Ashesh Kumar
Ashesh Kumar

Reputation: 223

PHP fails to insert into Mysql (auto increment)


I have made my Mysql Table link this:

enter image description here enter image description here

That problem occurs because i am not able to insert auto_increment into my mysql query.

index.php

<?php

        $name=$_POST["name"];
        $description=$_POST["description"];
        $image=$_POST["image"];
        $amount=$_POST["amount"];

 $sql=mysql_query("INSERT INTO `store`(`id`, `name`, `description`, `price`, `image`) VALUES (NULL,'".$name."','".$description."','".$amount."','".$image."')");




?>

I post to it by a HTML form and it does executes the query but i get no rows in that table. I thnk its because of auto_increment or Unique or Primary. i was told to use NULL in place of auto_increment value but it doesn't work.

Any Help?

P.S Noob here!

EDIT: error is:

Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in D:\xampp\htdocs\Html\Home\index.php on line 9

Upvotes: 0

Views: 4714

Answers (5)

Sudheera
Sudheera

Reputation: 1

you don't need a single quote or an apostrophe to enclose column names, since these data don't replace with variables. apostrophes need when we want to replace columns with data, here in values.

Upvotes: -2

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23409

To address the Issue of mysql_query being deprecated, I recommend PDO instead. It's super easy and much safer with prepared staements. This snippet will provide a connection to the DB to make your queries:

$mysql_host = "localhost";
$mysql_db = "my_database";
$mysql_user = "my_user";
$mysql_password = "mypassword";
$db = new PDO('mysql:host='.$mysql_host.';dbname='.$mysql_db.';charset=utf8', $mysql_user, $mysql_password);

Once you have that on your page, you can use $db to interact with your database.

$q = $db->prepare("INSERT INTO `store` (`name`, `description`, `price`, `image`) VALUES (:name, :desc, :amt, :img)");
$q->execute(array(":name"=>$name, ":desc"=>$description, ":amt"=>$amount, ":img"=>$image));

Also, for auto-imcrement values, you can omit that field all together.

PS, there's a stntax error. You're missing a space before the opening parenthesis: store (

Upvotes: 1

Rizier123
Rizier123

Reputation: 59701

You can't insert NULL into a AUTO_INCREMENT field! It will automaticly fill in this field, so make the query without the id!

UPDATE!

As far as i know best practice is PDO so i would suggest you to change to PDO!

Also you have to make a connection first! (DB_HOST and so on are constants! you can change this to variables if you want)

$dbh = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASSWORD);

Then try this:

<?php

    $statement = $dbh->prepare("INSERT INTO `store`(`name`, `description`, `price`, `image`)
                                            VALUES (':name',':description',':amount',':image')");

    $statement->bindParam(':name', $_POST['name']);
    $statement->bindParam(':description', $_POST['description']);
    $statement->bindParam(':amount', $_POST['amount']);
    $statement->bindParam(':image', $_POST['image']);

    $statement->execute();

?>

Upvotes: 2

CapitanFindus
CapitanFindus

Reputation: 1526

Remove 'id' from values into your query

$sql=mysql_query("INSERT INTO `store`(`name`, `description`, `price`, `image`) VALUES ('".$name."','".$description."','".$amount."','".$image."')");

AUTO_INCREMENT works by itself ;)

Upvotes: 0

John Conde
John Conde

Reputation: 219924

Remove the id column from your query:

$sql=mysql_query("INSERT INTO `store`(`name`, `description`, `price`, `image`) VALUES ('".$name."','".$description."','".$amount."','".$image."')");

Upvotes: 3

Related Questions