MYSQL INSERT

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

Moderator: General Moderators

Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

MYSQL INSERT

Post by Parody »

Im having trouble with a MYSQL insert query. I checked the code against some examples I found and from that it looked fine. But when I run the file it is in I get an error, a parse error, this to be precise:

Code: Select all

Parse error: parse error in c:\program files\eas1yphp1-8\www\registertmp.php on line 75
This is the line:

Code: Select all

mysql_query("INSERT INTO users (username, password, regdate, email) VALUES ('".$_POST['uname']."', '$rand', "$regdate", '".$_POST['email']."')";

And this is the block around it:

Code: Select all

mysql_connect("localhost", "(myusername)", "(mypassword)") or die(mysql_error());
mysql_select_db("users") or die(mysql_error());
$regdate = date('m d, Y');
mysql_query("INSERT INTO users (username, password, regdate, email) VALUES ('".$_POST['uname']."', '$rand', "$regdate", '".$_POST['email']."')";
mysql_query("INSERT INTO stats('".$_POST['uname']."','$chrctr', '100', '2000', '001', '01', 000, '01', '01', '01', '1', '0', '00000000', '00000000', '00000000', '00000000')");
echo mysql_errno() . ": " . mysql_error() . "<br><br>";
mysql_close();
Any ideas :?:
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

your $regdate is inside a terminated string.

you need to concat it with the rest of the string using "."

ie:

Code: Select all

$query = "blalh blah blah ".$regdate." more blah";
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

Still get the same error :(
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Need to add ' ' around the value too....
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

So the line looks like this?:

Code: Select all

mysql_query("INSERT INTO users (username, password, regdate, email) VALUES ('".$_POST['uname']."', '$rand', '.$regdate.', '".$_POST['email']."')";
If that is what you mean, it didn't work :(
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

mysql_query("INSERT INTO users (username, password, regdate, email) VALUES ('".$_POST['uname']."', '".$rand."', '".$regdate."', '".$_POST['email']."')";
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

do this

Code: Select all

$query = "INSERT INTO users (username, password, regdate, email) VALUES ({$_POST['uname']}, {$rand}, {$regdate}, {$_POST['email']})";
print $query;
#$result = mysql_query($query) or die (mysql_error());
this will tell you exactly what the query is in case theres some formatting errors that MySQL doesnt like, also i think it is better to seperate query from call anyway
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

I still get the same error :(
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

you shouldnt get the error because your not even executing the query, your simply printing it out to check its all looking right
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try what I posted did you?

need the single quotes in there you do to insert properly to MySQL.

Agree with malcom I do in that you should probably create your query string outside of your actual insert function so that you can "debug" it...but the one I posted should work for you.

edit: updated to Yoda...it's Wednesday and forgot I did 8O
Last edited by Burrito on Wed Jun 08, 2005 11:31 am, edited 1 time in total.
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

I tried just printing it, it doesn't work. It keeps saying parse error :(
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

Oh, wait it works :) But that doesnt insert my data :(
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

$qry = "INSERT INTO users (username, password, regdate, email) VALUES ('".$_POST['uname']."', '".$rand."', '".$regdate."', '".$_POST['email']."')";
echo $qry;
mysql_query($qry)
  or die(mysql_error());
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Post by Parody »

No errors(for that problem), so I think that works, but I now have another error :(

This is the piece:

Code: Select all

<?php
$to = ".$_POST['email'].";
$subject = "(subject)";
$body = "Your password is: "$rand"";
if (mail($to, $subject, $body))
?>
The error is this:

Code: Select all

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in c:\program files\eas1yphp1-8\www\registertmp123.php on line 84
Line 84 is this line :

Code: Select all

$to = ".$_POST['email'].";
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php
$to = $_POST['email'];
$subject = "(subject)";
$body = "Your password is: ".$rand;
if (mail($to, $subject, $body))
?>
Post Reply