0

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

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
dalia
  • 1
  • 2
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 05 '15 at 11:48
  • Change `$_POST["'$friend_id'"]` to `$_POST['$friend_id']` you have double and single quotes. probably a Copy/Paste hiccup – RiggsFolly May 05 '15 at 11:55
  • i actually tried that and it also did not work – dalia May 05 '15 at 11:58

3 Answers3

2

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.

Community
  • 1
  • 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • thank you so much but again it did not enter the if statement, and i will keep in mind the other information. – dalia May 05 '15 at 11:57
  • Just before your `if` do a `print_r($_POST)` so you can see what is in the POST array @dalia – Jay Blanchard May 05 '15 at 11:58
  • it actually printed out the user id and name as such Array ( [7] => lolita ) – dalia May 05 '15 at 12:02
  • So it must be getting into the `if` statement @dalia. You're not doing any error checking on your queries, perhaps there is a problem there? Add error checking, such as `or die(mysql_error())` to your queries. You can also find the issues in your current error logs. – Jay Blanchard May 05 '15 at 12:04
  • but it directly goes to the else statement . thank you so much for your help – dalia May 05 '15 at 12:11
  • The `while` in the `else` is trying to get data from the query run in the first part of the `if`. That will not work. – Jay Blanchard May 05 '15 at 12:13
  • i did not mean the else inside the major if i was talking about the else that maens in $_post is not set – dalia May 05 '15 at 12:17
  • I only see one `else`. Now that I have fixed indenting I can see what you're doing and your code flows OK. Until you add MySQl error checking we may never know what is happening. How are you setting `$session_user_id`? – Jay Blanchard May 05 '15 at 12:18
  • i did and i don't see anything the last error is hours ago – dalia May 05 '15 at 12:33
  • Hmmmm, then I am not sure what to tell you. Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard May 05 '15 at 12:34
  • thank you you have been very helpful just when i did the error displaying i got an error saying i need to use mysqli – dalia May 05 '15 at 12:42
  • Either MySQLi or PDO @dalia. Check the links in my answer. – Jay Blanchard May 05 '15 at 12:43
  • but could that be ever the reason of the problem i thought it cant be – dalia May 05 '15 at 12:44
  • Yes, because your POST would be blank. – Jay Blanchard May 05 '15 at 12:45
  • i will change it but i am just trying to get my mind around it when we print it out it contained something – dalia May 05 '15 at 12:47
0

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.

pavel
  • 26,538
  • 10
  • 45
  • 61
0

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
 }
Christopher Shaw
  • 763
  • 6
  • 19