| | |
INSERT INTO through PHP
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2009
Posts: 24
Reputation:
Solved Threads: 0
Hello,
This query works when entered directly into MySQL but when used in a php page it fails and generates the following error:
Also, the date values are not accepted in either the php page or mysql terminal, they default to '0000-00-00'.
Here is my query:
What do you guys think?
This query works when entered directly into MySQL but when used in a php page it fails and generates the following error:
•
•
•
•
Column count doesn't match value count at row 3
Here is my query:
PHP Syntax (Toggle Plain Text)
mysql_select_db("ecommerce"); $query = "INSERT INTO products VALUES ( '00001', 'toothbrush', 'Brush your teeth with this.', 395.00, '2009-21-04'), ('00002', 'tooth paste', 'You will need this too.', 695.00, '2009-21-04'), ('00003', 'mouth wash', 'Good to use after toothbrush.', 1,250.00, '2009-21-04')"; $result = mysql_query($query) or die(mysql_error()); echo "Products added successfully!";
What do you guys think?
No I always use the ' to add data. Does this fill your fields exactly? and is this the order in wich yu have to put them in? I would try:
The extra bit I added ( (id, item ) should reflect the name of the fields in wich you are adding them to.
Another point is that you are not using your conection variable to tell MySql what server username or password to use. I understand you didn't want to post them but you must make sure that when you use the mysql_select_db function you use it like this [icode] mysql_select_db( "ecommerce" , $con ); con being your connection variable.
Also have you tried adding them individually?
I did also notice that where you have submitted the price you have used a comma to seperate the digits. This could confuse MySql/PHP that you want to move onto the next field.
Just some ideas to reflect on.
php Syntax (Toggle Plain Text)
mysql_select_db("ecommerce"); $query = "INSERT INTO products (id, item, description, price, date) VALUES ( '00001', 'toothbrush', 'Brush your teeth with this.', 395.00, '2009-21-04'), ('00002', 'tooth paste', 'You will need this too.', 695.00, '2009-21-04'), ('00003', 'mouth wash', 'Good to use after toothbrush.', 1,250.00, '2009-21-04')"; $result = mysql_query($query) or die(mysql_error()); echo "Products added successfully!";
The extra bit I added ( (id, item ) should reflect the name of the fields in wich you are adding them to.
Another point is that you are not using your conection variable to tell MySql what server username or password to use. I understand you didn't want to post them but you must make sure that when you use the mysql_select_db function you use it like this [icode] mysql_select_db( "ecommerce" , $con ); con being your connection variable.
Also have you tried adding them individually?
php Syntax (Toggle Plain Text)
mysql_select_db("ecommerce"); $query = "INSERT INTO products VALUES ( '00001', 'toothbrush', 'Brush your teeth with this.', 395.00, '2009-21-04')"; $query .= "INSERT INTO products VALUES ('00002', 'tooth paste', 'You will need this too.', 695.00, '2009-21-04')"; $query .= "INSERT INTO products VALUES ('00003', 'mouth wash', 'Good to use after toothbrush.', 1,250.00, '2009-21-04')"; $result = mysql_query($query) or die(mysql_error()); echo "Products added successfully!";
I did also notice that where you have submitted the price you have used a comma to seperate the digits. This could confuse MySql/PHP that you want to move onto the next field.
Just some ideas to reflect on.
Posts should be like mini-skirts, long enough to cover enough, but not too long that you cover too much.
My Liveperson: http://liveperson.com/josh-connerty/
My Liveperson: http://liveperson.com/josh-connerty/
•
•
Join Date: Apr 2009
Posts: 24
Reputation:
Solved Threads: 0
•
•
•
•
I did also notice that where you have submitted the price you have used a comma to seperate the digits. This could confuse MySql/PHP that you want to move onto the next field.
Just some ideas to reflect on.
My dates still show up as a series of 0's though. "0000-00-00".
Do you have any thoughts on this? Also, while we're on this train of thought, is there any good way to insert commas into a numeric value in MySQL? Can you escape the commas with \backslashes\ as in '1\,250'?
Last edited by kssi89; Apr 23rd, 2009 at 10:54 am.
Hmm I can't really think without knowing how your MySql database is structured. Can you export the database (or just the table) and post it here please. I'm not to sure why MySql would do that...
Posts should be like mini-skirts, long enough to cover enough, but not too long that you cover too much.
My Liveperson: http://liveperson.com/josh-connerty/
My Liveperson: http://liveperson.com/josh-connerty/
•
•
•
•
Hello,
This query works when entered directly into MySQL but when used in a php page it fails and generates the following error:
Also, the date values are not accepted in either the php page or mysql terminal, they default to '0000-00-00'.
Here is my query:
PHP Syntax (Toggle Plain Text)
mysql_select_db("ecommerce"); $query = "INSERT INTO products VALUES ( '00001', 'toothbrush', 'Brush your teeth with this.', 395.00, '2009-21-04'), ('00002', 'tooth paste', 'You will need this too.', 695.00, '2009-21-04'), ('00003', 'mouth wash', 'Good to use after toothbrush.', 1,250.00, '2009-21-04')"; $result = mysql_query($query) or die(mysql_error()); echo "Products added successfully!";
What do you guys think?
php Syntax (Toggle Plain Text)
mysql_select_db("ecommerce"); $query = "INSERT INTO products VALUES ('".mysql_real_escape_string('00001')."', '".mysql_real_escape_string('toothbrush')."', '".mysql_real_escape_string('Brush your teeth with this.')."', '".mysql_real_escape_string('395.00')."', '".mysql_real_escape_string('2009-21-04')."'), ('".mysql_real_escape_string('00002')."', '".mysql_real_escape_string('tooth paste')."', '".mysql_real_escape_string('You will need this too.')."', '".mysql_real_escape_string('695.00')."', '".mysql_real_escape_string('2009-21-04')."'), ('".mysql_real_escape_string('00003')."', '".mysql_real_escape_string('mouth wash')."', '".mysql_real_escape_string('Good to use after toothbrush.')."', '".mysql_real_escape_string('1,250.00')."', '".mysql_real_escape_string('2009-21-04')."')"; $result = mysql_query($query) or die(mysql_error()); echo "Products added successfully!";
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - MacGyver Fan
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - MacGyver Fan
![]() |
Similar Threads
- need help with insert.php (PHP)
- Basic PHP Includes (PHP)
- How to insert PHP in HTML language? (PHP)
- phpBB to php-nuke integration help (PHP)
- New to PHP (PHP)
Other Threads in the PHP Forum
- Previous Thread: How to display MSQL product data on web page?
- Next Thread: uploading wont work
| Thread Tools | Search this Thread |
.htaccess action ajax apache api array auto beginner binary bounce broken cakephp checkbox class cms code cron curl database date display dynamic echo email error errorlog file files folder form format forms function functions google href htaccess html image include insert integration interactive ip java javascript joomla limit link login loop mail malfunctioning masterthesis menu mlm mod_rewrite multiple mysql nodes oop paypal pdf php popup problem query radio ram random recursion reference regex remote return script search server sessions sms soap source space sql syntax system table tutorial unset update upload url validation validator variable video web websitecontactform xml youtube






