Page 1 of 1

Prob with inserting

Posted: Mon Mar 08, 2004 11:50 pm
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?

Posted: Tue Mar 09, 2004 12:09 am
by PrObLeM
try this

Code: Select all

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

Posted: Tue Mar 09, 2004 12:59 am
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?

:)

Posted: Tue Mar 09, 2004 1:13 am
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.

Posted: Tue Mar 09, 2004 9:19 am
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