Page 1 of 1

Need basic syntax help: INSERT/WHERE not working...

Posted: Fri Aug 22, 2008 2:20 pm
by JAB Creations
What am I doing wrong here? I just want to insert the IP address in to the database on the row where the username = $user. I have all the correct things selected but it's the syntax I'm messing up somewhere at the end...?

Code: Select all

//$mysql_register = mysql_query("INSERT INTO public_accounts ip_0 VALUES  inet_aton('".$ipaddress."') WHERE username = '".$user."'");
//$mysql_register = mysql_query("INSERT INTO public_accounts (ip_0) VALUES (inet_aton('".$ipaddress."')) WHERE username = '".$user."'");
 
$mysql_register = mysql_query("INSERT INTO public_accounts (date_0, ip_0) VALUES (NOW(), inet_aton('".$ipaddress."')) WHERE username = '".$user."'");

Re: Need basic syntax help: INSERT/WHERE not working...

Posted: Fri Aug 22, 2008 2:39 pm
by jaoudestudios
If you are doing that surely you mean UPDATE?

It is worth being more specific with your queries too, i.e.

Code: Select all

 
$q = "UPDATE public_accounts SET date_0 = '".time()."', ip_0 = '".$ipaddress."' WHERE username = '".$user."'";
 
I would put some protection on there too, just in case of sql injection

Re: Need basic syntax help: INSERT/WHERE not working...

Posted: Fri Aug 22, 2008 4:31 pm
by JAB Creations
This isn't doing anything when the script is executed...not sure why.

Code: Select all

$query2 = mysql_query("UPDATE public_accounts SET ip_0 = inet_aton('".$ipaddress."') WHERE username = '$username'");
ip_0 / int(11) for IP addresses. The registration works fine and I've tried to emulate that however...

Code: Select all

$mysql_register = mysql_query("INSERT INTO public_accounts (username, password, email, activation_key, date_0, ip_0, status) VALUES ('$username', '$password', '$email', '$activation_key', NOW(), inet_aton('".$ipaddress."'), 'Activation Pending')");
...of course I don't want to update anything save the IP address when they authenticate. I'm really stuck on this and I'm not getting any MySQL errors or anything. :banghead:

Re: Need basic syntax help: INSERT/WHERE not working...

Posted: Fri Aug 22, 2008 5:19 pm
by jaoudestudios
Are you using a linux server? Dont send the query to the mysql server through php, echo it out to the screen and then copy and paste it to the mysql command line and it will give you more feedback on errors etc

Re: Need basic syntax help: INSERT/WHERE not working...

Posted: Fri Aug 22, 2008 5:23 pm
by JAB Creations
Not sure how else to do that but here I go...

Code: Select all

$query2 = mysql_query("UPDATE public_accounts SET ip_0 = inet_aton('".$ipaddress."') WHERE username = '$username'");
echo $query2;
1

Code: Select all

$query2 = "UPDATE public_accounts SET ip_0 = inet_aton('".$ipaddress."') WHERE username = '$username'";
echo $query2;
UPDATE public_accounts SET ip_0 = inet_aton('6x.xx.xxx.xxx') WHERE username = ''

Re: Need basic syntax help: INSERT/WHERE not working...

Posted: Fri Aug 22, 2008 5:34 pm
by jaoudestudios
There you go...

$username is empty!

what is the function inet_aton?

Re: Need basic syntax help: INSERT/WHERE not working...

Posted: Fri Aug 22, 2008 5:35 pm
by jaoudestudios
Also I would not parse $username, no need.

Re: Need basic syntax help: INSERT/WHERE not working...

Posted: Fri Aug 22, 2008 5:41 pm
by JAB Creations
I kept changing the syntax and I'm still getting used to it.

This works...

Code: Select all

$query2 = mysql_query("UPDATE public_accounts SET ip_0 = inet_aton('".$ipaddress."') WHERE username = '".mysql_real_escape_string($_POST['username'])."'");
Thanks. :mrgreen:

Re: Need basic syntax help: INSERT/WHERE not working...

Posted: Fri Aug 22, 2008 5:45 pm
by jaoudestudios
That looks better.

No problem, glad it works.