dalia
dalia

Reputation: 1

passing a php variable in $_POST

i have a question regarding passing a php variable in the $_POST knowing that i named my buttons using the same variable because i want the buttons to have unique names.

while($row = mysql_fetch_array($query)){

        $friend_id = $row['friend_id'];

    $result = mysql_query("SELECT username FROM users WHERE user_id = '$friend_id'");
    if (mysql_num_rows($result) > 0) {
    $friendname = mysql_result($result,0,"username");
    $friendname = sanitize($friendname);
    echo '<input type = "submit"  id='. $friend_id .'  name ='.$friend_id.' class = "member" value ='. $friendname.' /><br>';
    }   

here where i am trying to pass it but it is not working

print_r($_POST);

if(isset($_POST['name'])){


    $signers =  mysql_query("SELECT friend_id  FROM friends WHERE user_id = $session_user_id ");
    $count =    mysql_num_rows($signers);
    if($count == 0){
        echo "<p>you need to add team members</p>";
    }

else{
    while($row = mysql_fetch_array($signers)){

        $signer_id .= $row['friend_id'];

}
echo '<p>'.$signer_id . '</p>';
}
$request = mysql_query("INSERT INTO requests VALUES ('','$user_id','$fid','$fname','$signer_id')");
}
else {

    echo '<p> not working </p>';
}

both of those sections are in the same php page

Upvotes: 0

Views: 135

Answers (3)

Christopher Shaw
Christopher Shaw

Reputation: 734

You should look into predefining any variables you intend to use.

function input_post ($value, $default) {
   return isset($_POST[$value]) ? $_POST['value'] : false;
}

Then use the post as so, this would prevent any not set errors

 $friend_id = input_post('friend_id');

 if ($friend_id) {
    // If friend_id is set, do this
 }
 else {
    // If friend_id is false or unset
 }

Upvotes: 0

Jay Blanchard
Jay Blanchard

Reputation: 34406

You're not passing a variable around, you're passing a value so this line -

if(isset($_POST["'$friend_id'"])=== true){

needs to be changed to this -

if(isset($_POST['name'])){ 

The name attribute (along with the value) of each input is what is passed in a POST. You're just checking to see if the name parameter has a value, if it does then you can act on it with other code.

In addition please stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO.

Upvotes: 2

pavel
pavel

Reputation: 27072

The condition in the second piece of code should be without quotes:

if (isset($_POST[$friend_id])) {... 

The part === true isn't necessary in this case, I've removed it.

Upvotes: 0

Related Questions