...

View Full Version : Adding to MySQL DB



gsnedders
06-06-2004, 11:46 PM
I have this code:
$query="INSERT INTO email(email,name VALUES($email,$name)";
$result=mysql_query($query); and in my DB I have a table called email, with to columns, email and name, why isn't this script working?

johnrau
06-07-2004, 12:24 AM
should be:



$query="INSERT INTO email VALUES($email,$name)";
mysql_query($query);


-j

gsnedders
06-07-2004, 07:02 PM
Still doesn't work, here's my full code:
<?PHP
require ('config.php');

mysql_connect(localhost,$db_user,$db_pass);
@mysql_select_db($db) or die( "Unable to select database");
$query="INSERT INTO email VALUES($email,$name)";
mysql_query($query);

mysql_close();
?>

Config just has some varibiles in it.

jeskel
06-07-2004, 07:35 PM
as far as I can see, you will need to tell where to insert these values.


$query=
"INSERT INTO emailDB
SET email = '$email'
, name = '$name'";

you can also use the parantheses with VALUES but I like it better like that.

Don't forget the single quotes as the values you are inserting are not only numbers.

Nightfire
06-07-2004, 08:17 PM
INSERT INTO emailDB (email,name) VALUES ($email,$name)

How are $email and $name set? Via a form or url? If so, you need to use the superglobals, ie;
$email = $_GET['email'];
$name = $_GET['name'];
at the top of the page, use $_POST instead of $_GET if you're using the POST method in the form

jeskel
06-08-2004, 09:46 PM
btw this thread makes wonder what are the differences between using INSERT ... SET
and
INSERT... () VALUES ().

sorry for hijacking your thread error404 :)

MrShed
06-08-2004, 09:48 PM
yeh i always use SET....afaik they are exactly the same

Nightfire
06-08-2004, 10:17 PM
There's no difference. There's 3 ways to INSERT

INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] tbl_name [(col_name,...)]
VALUES ({expr | DEFAULT},...),(...),...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

Or:

INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] tbl_name
SET col_name={expr | DEFAULT}, ...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

Or:

INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...

It's all down to your preference really.

jeskel
06-08-2004, 10:30 PM
thanks for your replies. you confirmed what I expected :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum