madmhan84 Posted July 23, 2009 Report Share Posted July 23, 2009 (edited) I created a database named testdb, and a table named symbols in phpmyadmin here's the code: CREATE TABLE `symbols` ( `id` int(11) NOT NULL auto_increment, `country` varchar(255) NOT NULL default '', `animal` varchar(255) NOT NULL default '', PRIMARY KEY (`id`) ) TYPE=MyISAM; INSERT INTO `symbols` VALUES (1, 'America', 'eagle'); INSERT INTO `symbols` VALUES (2, 'China', 'dragon'); INSERT INTO `symbols` VALUES (3, 'England', 'lion'); INSERT INTO `symbols` VALUES (4, 'India', 'tiger'); INSERT INTO `symbols` VALUES (5, 'Australia', 'kangaroo'); INSERT INTO `symbols` VALUES (6, 'Norway', 'elk'); I used to display it in my browser using the PHP code: <?php $db = "testdb"; $connection = mysql_connect('localhost','root',''); mysql_select_db('testdb'); $query = "SELECT * FROM symbols"; $result = mysql_query($query); if (mysql_num_rows($result) >0) { echo " while ($row = mysql_fetch_row($result)) { echo " echo "" .$row[0] ."";echo "" .$row[1] ."";echo "" .$row[2] ."";echo "";} echo "";} mysql_free_result($result); mysql_close($connection); ?> .......And it works fine BUT..... (here's my problem) I want to insert a record that's not define in MySQL or in phpMyAdmin here's the code that i make but does not working and i get the error message: Parse error: parse error in D:\Program Files\wamp\www\test.php on line 12 <?php $db = "testdb"; $connection = mysql_connect('localhost','root',''); mysql_select_db('testdb'); //HERE'S THE CODE THAT I ADD mysql_query("INSERT INTO symbols (id, country, animal) VALUES ('7', 'Africa', 'zebra'))"; $query = "SELECT * FROM symbols"; $result = mysql_query($query); if (mysql_num_rows($result) >0) { echo " while ($row = mysql_fetch_row($result)) { echo " echo "" .$row[0] ."";echo "" .$row[1] ."";echo "" .$row[2] ."";echo "";} echo "";} mysql_free_result($result); mysql_close($connection); ?> Hope someone could help me... THANKS A LOT! Edited July 23, 2009 by madmhan84 Quote Link to comment Share on other sites More sharing options...
administrator Posted July 23, 2009 Report Share Posted July 23, 2009 Hi, Check out this line of code: VALUES ('7', 'Africa', 'zebra'))"; ... Try not setting a value for your ID field. I seem to remember ... that it was set to be auto incremented and was of data type INT: CREATE TABLE `symbols` ( `id` int(11) NOT NULL auto_increment, As you know, '7' means you are sending MySQL a string when it wants to insert an INT. Let me know Stefan Quote Link to comment Share on other sites More sharing options...
falkencreative Posted July 23, 2009 Report Share Posted July 23, 2009 Like Stefan says, try ignoring the ID field entirely, and doing this: mysql_query("INSERT INTO symbols (country, animal) VALUES ('Africa', 'zebra'))"; MySQL will add it to the correct table, and automatically assign it the next available id. Quote Link to comment Share on other sites More sharing options...
madmhan84 Posted July 25, 2009 Author Report Share Posted July 25, 2009 Sir Stef and Sir Benj thank you very much!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.