Prob with inserting

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Prob with inserting

Post by Steveo31 »

Code: Select all

<?php 
if(!empty($_POST['name']) && !empty($_POST['email'])) {
	echo "Both filled in.";
}
$db = mysql_connect("localhost", "root") or die(mysql_error);
mysql_select_db("directory", $db);
$sql = "INSERT INTO `person` ( `name` , `email` ) VALUES ( ".$_POST['name'].", ".$_POST['email']." ) " or die(mysql_error());
$result = mysql_query($sql, $db) or die(mysql_error());
?>
Returns:

Code: Select all

Unknown column 'steve' in 'field list'
I want to just add my name to the database "directory", in table "person", and the appropriate values of 'name' and 'email' (both textboxes on the same page... just didn't include here) in the "email" and "name" varchars.

Any idears?
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

try this

Code: Select all

$sql = "INSERT INTO person VALUES ( ".$_POST['name'].", ".$_POST['email']." )" or die(mysql_error());
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Thanks for the response, but no dice.

Tried

Code: Select all

$sql = "INSERT INTO person VALUES (".$_POST['name'].",".$_POST['email'].")" or die(mysql_error());
As well as:

Code: Select all

$sql = "INSERT INTO person (name, email) VALUES (".$_POST['name'].",".$_POST['email'].")" or die(mysql_error());
And a few others. No dice....

-edit-

Ok, this works:

Code: Select all

$sql = "INSERT INTO `person` ( `name` , `email` ) VALUES ( 'steven', 'stevesemailaddress' )";
Buuuut, I need those values to be variables, not set. I'll do some homework tomorrow and see if I can't come up with something. Meanwhile, do you have a solution?

:)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You want,

Code: Select all

$sql = "INSERT INTO person (name, email) VALUES ('".$_POST['name']."', '".$_POST['email']."')";
mysql_query($sql) or die(mysql_error());
So it produces VALUES like 'steve', 'foo@bar.com' and not steve, foo@bar.com. Without the single quotes mysql will treat them as column names (hence the error). And you don't want the or die bit on the end of the $sql = string, just on the mysql_query() call.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Alright, makes sense! That was the other error I go, something to the effect of "You can't use '@aol.com', focused on the @ symbol.

:D
Post Reply