Jump to content

How to insert a new record in MySQL using PHP?


madmhan84

Recommended Posts

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 by madmhan84
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...