tags:

views:

93

answers:

7

I'm learning PHP right now and I'm trying to insert data into a MySQL database called "pumpl2" The table is set up like this.

create table product
 ( productid int unsigned not null auto_increment primary key,
   price int(9) not null,
   value int(9) not null,
   description text
 );

I have a form and want to insert the fields from the form in the database. Here is what the php file looks like.

<?php

// create short variable names
$price = $_POST['price'];
$value = $_POST['value'];
$description = $_POST['description'];

if (!$price || !$value || !$description) {
 echo "You have not entered all the required details.<br />"
  ."Please go back and try again.";
 exit;
}

@ $db = new mysqli('localhost', 'pumpl', '********', 'pumpl2');

if (mysqli_connect_errno()) {
 echo "Error: Could not connect to database. Please try again later.";
 exit;
}

$query = "insert into pumpl2 values
 ('".$price."', '".$value."', '".$description."')";
$result = $db->query($query);

if ($result) {
 echo  $db->affected_rows." product inserted into database.";
} else {
 echo "An error has occurred.  The item was not added.";
}

$db->close();

?>

When I submit the form, I get an error message "An error has occurred. The item was not added."

Does anyone know what the problem is? Thank you!

+3  A: 

You only insert three columns but have four defined in your table. Thus you have to name the columns explicitly:

 INSERT INTO tableName (ColumnA, ColumnB, ColumnC) VALUES ('A', 'B', 'C')
ZeissS
+1 nice catch..
zaf
Thank you! This fixed the problem.
Victor
+4  A: 

This should give you more information:

echo "An error has occurred: " . $db->error();
Michael Borgwardt
+1 for telling the OP how to diagnose the problem himself!
symcbean
Thank you. I added this to the script. I'm sure it will help in the future.
Victor
@Victor, actually, that's wrong approach. trigger_error() should be used instead of echo. and to add a query itself to the error message would be also good idea.
Col. Shrapnel
+3  A: 

You are trying to insert into the table called pumpl2, but the CREATE TABLE statement created a table called product.

In addition, as ZeissS noted, you have to consider the following:

CREATE TABLE product ( 
    productid int unsigned not null auto_increment primary key,
    price int(9) not null,
    value int(9) not null,
    description text
);
Query OK, 0 rows affected (0.09 sec)

INSERT INTO product VALUES (1, 1, 'test');
ERROR 1136 (21S01): Column count doesn't match value count at row 1

To solve that error, you need to explicitly specify the list of the columns:

INSERT INTO product (price, value, description) VALUES (1, 1, 'test');
Query OK, 1 row affected (0.03 sec)
Daniel Vassallo
Thank you. I believe this is correct. I combined this advice with advice above and it helped me fix the problem.
Victor
No, you should'nt insert NULL here, since the column is defined as `not null` and `auto_increment`. I would say, working with NULL here is beeing dependent on the underlying database too much. Make it clear (other people are not confused by the null value) and simple: Don't insert this column.
ZeissS
@ZeissS: Good point. Fixed. (In fact, it wouldn't have worked in strict mode.)
Daniel Vassallo
A: 

It could be several reasons.

Try

echo "Errormessage: ".$db->error;

to get more details, why the Insert didn't work.

Björn
+1  A: 

Your query is wrong, you didn't have the columns specified. Try it with:

"INSERT INTO pumpl2 (price, value, description) VALUES ('".$price."', '".$value."', '".$description."')"

Besides that, do not use the $_POST values to enter them directly into the database. Search for SQL Injection on this one. Use mysql_real_escape_string on the $_POST data first, or even better use prepared statements.

TheCandyMan666
Specifying the values is not necessary if you're inserting a value for every column, and in the correct order. However, it *is* good practice to specify them. EDIT: ah right, the OP wasn't inserting into all columns.
DisgruntledGoat
A: 

Your table is called products not pumpl2. Furthermore you should do:

insert into product (price, value, description) values ( ...
klausbyskov
+1  A: 
Quassnoi