I'm so stupid!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
John Rowe
Forum Newbie
Posts: 14
Joined: Thu Nov 20, 2003 7:53 pm

I'm so stupid!

Post by John Rowe »

Hello,

I've been looking through my PHP &MySQL for Dummies for the last 3 hours tying to figure out how to do this. Anyone willing to help me out will be my hero! I hope what I write makes sense!

I have a little script called putmail.php and I've put part of the code below. The script takes information submitted in a form and puts it into a DB table called invitedusers.

Here is the form:

<form action="/refer/putmail.php" method="post" onsubmit="return FormValidator(this)">
<input type="text" name="friendid" size="40" maxlength="45" value="">
<input type="hidden" name="id" value="##$userid##">
<input type="submit" name="submit" value="Go!"></form>

putmail.php first checks the DB to see if the 'friendid' from the form is already in the field friendid. If it's NOT there it puts it there. And it also puts the 'userid' from the form into the field userid. That part is OK.

But...

If 'friendid' is already in the DB the script does nothing to the DB and simply redirects the visitor.

Here's what I want to happen.

If 'friendid' sent by the form is already in the DB, I want the script compare the 'userid' from the form with what is already in the userid field next to that friendid.

If it is the same, it's okay for the script to just redirect the visitor, just like it already does.

If it is NOT the same, I would like the old 'userid' updated with the one the sent by the form.

Hers is the code:

// Make sure friend is not already in database
$query = "SELECT friendid FROM invitedusers WHERE friendid='$friendid' ";
$result = @mysql_query ($query);
if (mysql_num_rows($result) == 0)
{ // Available. Add to database

$query="INSERT INTO invitedusers (userid, friendid) VALUES('$userid', '$friendid')";
$result =@mysql_query ($query);

if ($result)
{
// added now forward to signup page
header ("Location: $redir_path&email=$friendid");
//echo 'You did it.';

//free_rs($rs);
mysql_close();
exit();
}
}
else
{ // Exists, forward to signup
header ("Location: $redir_path&email=$friendid");

mysql_close();
exit();

}


}

mysql_close();
exit();

?>

Thanks again if anyone can help me with this!!!

kindly,
John
User avatar
llanitedave
Forum Commoner
Posts: 78
Joined: Thu Jan 15, 2004 11:24 am
Location: Las Vegas, NV.

Post by llanitedave »

Looks to me like you need another query inside your "else" block, and before the "header" call.

How about something like:

Code: Select all

else   { // Exists, check userID
 
    $sql = "SELECT userid, friendid FROM invitedusers WHERE friendid='$friendid' and userid = '$userid'";
    $result = mysql_query($sql);
    if (mysql_num_rows($result) == 0) { // Not the same

        $sql = UPDATE invitedusers SET userid = '$userid' WHERE friendid='$friendid'";
        $result = mysql_query($sql);

        // error management code here
    }
               
    header ("Location: $redir_path&email=$friendid"); 
                    
     mysql_close(); 
     exit(); 
                
}
This will update the userid field if necessary, and THEN redirect the user.

It's not debugged, but it's the kind of idea I think you're looking for.
Post Reply