[SOLVED] 2 queries at the same time.

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
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

[SOLVED] 2 queries at the same time.

Post by tim »

ok give me my options here.

I have a sign-up form, it has a checkbox beside the email field (to either show or hide the email address on the users profile page, etc) but also, it will add the username, email, password, etc to the database as well.

So the problem is, I can verify the checkbox is checked or not checked, but the way i'm doing this is I have a field set to 0 default, Via the UPDATE command I will change it to one (to hide the email, 0 to show, get it?)


I dont think you need any code, but heres a cheap example of what I got going:

Code: Select all

<?php
$sql = "SELECT username FROM users WHERE username='$username'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$user = $row['username'];
 if ($user == "") {
$sql = mysql_query("INSERT INTO users (username, password, email, aim) VALUES ('$username', '$password', '$email', '$aim')");
echo "Thank you for signing up";
  if ($showemail) { //$showemail is the name of the checkbox input
$sql = "UPDATE users SET semail=1 WHERE username='$user'";
$result = mysql_query($sql) or die(mysql_error);
  }
} 
...
?>
i get no errors, but I dont know if I can not only add the row of data (username, password, etc) and then UPDATE the row.

how can I go about doing this?





***marked solved : by infolock***
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

well, that above code has a few errors... none that would spit out, but a few logical errors. But it doesn't matter. What i would do is do your checking before... maybe something like:

Code: Select all

isset($showmail) ? $show=1 : $show=0;
$sql = "INSERT INTO users (username,password,email,aim,semail) VALUES ('$username','$password','$email','$aim','$show')";
@mysql_query($sql) or die("ERROR: ".mysql_error());
echo "Thank you for signing up!";
somethign liek thta would make more sense to me!
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

thanks ill, but unfortnately that code doesnt work either. I did have something to that effect, but I figured the above code would be more clear to what I was trying to do.

Well maybe I need to give some more info. All this is done on a page with my form (inputs for username, password, etc) and the action is $PHP_SELF?confirm=confirm

then I have a

if(isset($confirm)) {
then it checks for blank inputs/a username using ':_ and such characters.

this is the exact code, with your suggested appraoch:

/edit - nvm, your code works great. you had $showmail, my var was $showemail. Changed it and it works like a charm, wonder why my version of that didnt work. wish I had it to compare, oh well! :roll:

Thank you muchas.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

lol! its all good! Glad you got it working and i could be of some help! ;)
Post Reply