Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting into a database, HELP PLEASE

Status
Not open for further replies.

internetguy

Technical User
Nov 22, 2003
57
0
0
US
I am having trouble inserting data into a database. The connection is good because there is no error reported in the mysql_connect(). Here is my code, let me know if there is anthing wrong.

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$url = $_POST['url'];

$db = mysql_connect('localhost','root');
if (!$db)
{
echo 'Failure to connect to database, please try later.';
}
mysql_select_db('users');
$query = &quot;INSERT INTO info VALUES ('&quot;.$name.&quot;', '&quot;.$url.&quot;', '&quot;.$email.&quot;')&quot;;
$result = mysql_query($query);
if ($result)
echo 'your information has been submited';
?>
 
Add some error checking to the statement. MySQL actually tells you what's going wrong.
Code:
$result = mysql_query($query) OR die(mysql_error());

BTW, using 'root' is not advisable especially if there's no password. Set a root password immediately (as recommended by the MySQL installation instructions) and create a different user who is used to connect.
 
Also add:
Code:
mysql_select_db('users') or die(mysql_error());
 
ok, it says that the column count doesn't match value count at row 1. I have 4 columns in my MySQL database, ID, name, url, and email. I made the ID row as an auto-incrementing integer so that it assigns each user an ID. I am not sure if this has anything to do with it or not.
 
$query = &quot;INSERT INTO info VALUES ('&quot;.$name.&quot;', '&quot;.$url.&quot;', '&quot;.$email.&quot;')&quot;;

is just plain wrong, then. you have four fields to consider inserting to. you could rewrite that in two ways.

(1)
$query = &quot;INSERT INTO info (name, url, email) VALUES ('&quot;.$name.&quot;', '&quot;.$url.&quot;', '&quot;.$email.&quot;')&quot;;

or (2)
$query = &quot;INSERT INTO info VALUES ('', '&quot;.$name.&quot;', '&quot;.$url.&quot;', '&quot;.$email.&quot;')&quot;;

note the two single quotes at the start of your list to accommodate the auto_increment ID (assuming that's how you ordered your fields)... specify a blank field for auto_increment fields to have the DB automatically assign it

glad to be of help.

[wink]
 
Wow thank you so much, that line got it to work perfectly. I really appreciate it, I have been so mad cause I couldn't figure it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top