Insert Into.. Not inserting?

I’m new to PHP and MySQL, working on an inventory system for my current job. Something simple, but working.

Right now you can login, view tables, etc, but I need to make it so that you can submit data to be added to the table.

Here is the a snippet of code from my tabledit.php

[php]

Item Name:
Model:
Serial Number:
Condition:
Notes:
[/php]

Now when you press sumbit, it’s supposed to use the saveproduct.php to submit the data. Here is the code the the saveproduct.php.

[php]<?php

$itemname=$_POST[‘itemname’];
$model=$_POST[‘model’];
$serialnumber=$_POST[‘serialnumber’];
$conidition=$_POST[‘condition’];
$notes=$_POST[‘notes’];
$fin = “Record Added”;

mysql_connect(“localhost”, “root”, “”) or die(mysql_error());

mysql_select_db(“CTD_Inventory”) or die(mysql_error());

mysql_query(“INSERT INTO inventory VALES (‘itemname’, ‘model’, ‘serialnumber’, ‘condition’, ‘notes’)”);

echo $fin;

header(“location: tableedit.php#page=addpro”);
?>[/php]

Now the weird thing is, is that it doesn’t submit the data. It clears the text boxes, I get no errors, but the MySQL database is not updated.

Any ideas? I appreciate the help!

The code for the saveproduct.php was wrong, I forgot to fix it before posting.

[php]<?php

$itemname=$_POST[‘itemname’];
$model=$_POST[‘model’];
$serialnumber=$_POST[‘serialnumber’];
$conidition=$_POST[‘condition’];
$notes=$_POST[‘notes’];
$fin = “Record Added”;

mysql_connect(“localhost”, “root”, “”) or die(mysql_error());

mysql_select_db(“CTD_Inventory”) or die(mysql_error());

mysql_query(“INSERT INTO inventory (item, model, serialnumber, condition, notes) VALUES (‘itemname’, ‘model’, ‘serialnumber’, ‘condition’, ‘notes’)”);

echo $fin;

header(“location: tableedit.php#page=addpro”);
?>[/php]

Hi Wisefire,

Your query will not work properly the way you have it. You are not using your variables, you are using string literals. In addition, you are assigning a variable named $conidition instead of $condition.

See if this works for you:[php]<?php
$itemname=$_POST[‘itemname’];
$model=$_POST[‘model’];
$serialnumber=$_POST[‘serialnumber’];
$condition=$_POST[‘condition’];
$notes=$_POST[‘notes’];
$fin = “Record Added”;

mysql_connect(“localhost”, “root”, “”) or die(mysql_error());

mysql_select_db(“CTD_Inventory”) or die(mysql_error());

mysql_query(“INSERT INTO inventory (item, model, serialnumber, condition, notes) VALUES (’$itemname’, ‘$model’, ‘$serialnumber’, ‘$condition’, ‘$notes’)”);

echo $fin;

header(“location: tableedit.php#page=addpro”);
?>[/php]

Note that you also be checking for a valid post submission and data, as well as sanitizing everything before inserting into your table.

Hm, that didn’t seem to work. I really appreciate the help. Any idea for where I should go from here?

I would try the following:
[php]<?php
$itemname=$_POST[‘itemname’];
$model=$_POST[‘model’];
$serialnumber=$_POST[‘serialnumber’];
$condition=$_POST[‘condition’];
$notes=$_POST[‘notes’];

echo ‘

’;
print_r($_POST);
echo ‘
’;

echo “INSERT INTO inventory (item, model, serialnumber, condition, notes) VALUES (’$itemname’, ‘$model’, ‘$serialnumber’, ‘$condition’, ‘$notes’)”;

mysql_connect(“localhost”, “root”, “”) or die(mysql_error());
mysql_select_db(“CTD_Inventory”) or die(mysql_error());

mysql_query(“INSERT INTO inventory (item, model, serialnumber, condition, notes) VALUES (’$itemname’, ‘$model’, ‘$serialnumber’, ‘$condition’, ‘$notes’)”);

?>[/php]

And verify that you are getting post data and that your query is being created properly. If not, this should give you a good idea of where the problem lies.

One other thing: do you really want the # in your header or do you want this:[php]
header(“location: tableedit.php?page=addpro”);[/php]

Typically a fragment url would be something like this instead:[php]header(“location: tableedit.php#addpro”);[/php]

Still doesn’t seem to be working, this is very weird. My account creation script works just fine… So I know the database is there etc. Anything that I could add to this post to help solve the problem?

Did you see the $_POST data and the query? If not, make sure that this code is in a file named: saveproduct.php and that it is in the same directory as the form.

I did not see the #_POST data or the SQL Query, and the files are in place as should be.

Is your form definitely sending you to saveproduct.php upon submit? Also, did you leave the header out of the saveproduct.php for the test? If you included it, you should remove it and rerun the test.

I have tested the form code and the saveproduct.php code and it definitely is working on my server. Try putting the form code I previously posted in a new file called tableedit2.php and see if it works. If it does, we will probably need to look at the rest of your tableedit.php code.

Okay using the code posted, I do get the output after submitting, but it still is not inserted into the database. I created a tableedit2.php with the following form:

[php]

Item Name:
Model:
Serial Number:
Condition:
Notes:
[/php]

Still not working.

OK, at least we are making progress!

If it output the mysql query (it should have) look it over and make sure it looks correct.

Do you have access to phpmyadmin? If so, you might try executing the query directly in phpmyadmin and see if it works or returns an error. If not, are these the only columns in your “inventory” table and what type are each of them set to? (VARCHAR, etc).

If none of that gets us anywhere, we will create a different test file to target the database, but lets make sure we aren’t missing something small here first.

Okay, I do have access to phpmyadmin, and I have two other columns, id and date. I have attached a screenshot of the settings from phpmyadmin. Also, when using the code I get an error from phpmyadmin.

Code given “INSERT INTO inventory (item, model, serialnumber, condition, notes) VALUES (‘a’, ‘a’, ‘a’, ‘a’, ‘a’)”

Screenshot:
https://picasaweb.google.com/103322219311605224120/October82012#5797094538437819634

Here is the error, forgot it.

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘condition, notes) VALUES (’$itemname’, ‘$model’, ‘$serialnumber’, ‘$condition’, ’ at line 1

Off-topic - Can we edit posts here… Don’t see the option.

OK, I see the issue!

I hadn’t noticed it before, but you have a column named “condition” which is a mysql reserved word. I added ` around the column name in the code below (note that this character is the one above the tab key on most keyboards, not a single quote (apostrophe))

See if this works:[php]<?php
$itemname=$_POST[‘itemname’];
$model=$_POST[‘model’];
$serialnumber=$_POST[‘serialnumber’];
$condition=$_POST[‘condition’];
$notes=$_POST[‘notes’];

echo “INSERT INTO inventory (item, model, serialnumber, condition, notes) VALUES (’$itemname’, ‘$model’, ‘$serialnumber’, ‘$condition’, ‘$notes’)”;

mysql_connect(“localhost”, “root”, “”) or die(mysql_error());
mysql_select_db(“CTD_Inventory”) or die(mysql_error());

mysql_query(“INSERT INTO inventory (item, model, serialnumber, condition, notes) VALUES (’$itemname’, ‘$model’, ‘$serialnumber’, ‘$condition’, ‘$notes’)”);

?>[/php]

That worked! Thank you so much, appreciate the help! I’m sure I will be back! Glad I found this place!

Glad it worked! There are a number of reserved words in mysql, it’s easy to miss one in a query!

Sponsor our Newsletter | Privacy Policy | Terms of Service