0

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

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Ashesh Kumar
  • 223
  • 2
  • 12

5 Answers5

3

Remove the id column from your query:

$sql=mysql_query("INSERT INTO `store`(`name`, `description`, `price`, `image`) VALUES ('".$name."','".$description."','".$amount."','".$image."')");
John Conde
  • 217,595
  • 99
  • 455
  • 496
2

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

?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • dont forget strip_tags()! – alexander7567 Oct 10 '14 at 14:54
  • @AsheshKumar updated it! Try the new one, also i would suggest you to escape your POST! $connenction is you connection variable. – Rizier123 Oct 10 '14 at 14:54
  • @Rizier123 Why i have to pass to $connection ? – Ashesh Kumar Oct 10 '14 at 14:56
  • @AsheshKumar Its optional but it specifies the MySQL connection. – Rizier123 Oct 10 '14 at 14:58
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 10 '14 at 15:01
  • @Rizier123 Now this! ` Deprecated: mysql_real_escape_string(): 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 4` – Ashesh Kumar Oct 10 '14 at 15:02
  • @Rizier123 and this ` 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 10" – Ashesh Kumar Oct 10 '14 at 15:02
1

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 (

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
0

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

CapitanFindus
  • 1,498
  • 15
  • 26
-2

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.

Sudheera
  • 1
  • 1
  • 1