0

Okay, so I have a page that calls a list of information from a msyql database. say, i have a table called TABLE with columns id, name, phone numbers, and a radio button group (home,cell,work) etc. I've created a script that prints out in a table the name, phone number, and "status (not sure what best to call this, but it's the value of the radio buttons) the radio buttons. What I would like to do is make it so that for each row, if i assign a value to the radio buttons, it will edit the status part. so say, on row 1, i click home, and row 2, cell, and row 3 home... and so forth, and i hit a submit button it will update the database.

So, this is my code for the radio button section in the table. This is under a php tag

What I ended up doing basically is calling each group of radio buttons, status_type1, status_type2, status_type3... basically radio name='status_type".$info['id']."

I want to do an if statement to see if there are values assigned from any clicks on the buttons, then submit them for entry. This is what I have so far.

//Radio Button Template
  <fieldset>
<input type='radio' id='status_type_home' name='status_type".$info['id']."' value='home'  />
    <label for='status_type_home'>home</label>
<input type='radio' id='status_type_work' name='status_type".$info['id']."' value='work'  />
    <label for='status_type_work'>work</label>
<input type='radio' id='status_type_cell' name='status_type".$info['id']."' value='cell'  />
    <label for='status_type_cell'>cell</label>
</fieldset>

 </td>";


//If statement to submit radio buttons

    if ($_POST['status_type "$info['id']"']) 
{
$edit_status_sql="UPDATE status SET type = 'status_type".$info['id'] . "' WHERE id = '" . $id . "'";

$edit_status_res=mysqli_query($connection, $edit_status_sql) or die(mysqli_error($connection));
}

The problem I have is basically, I've declared the name of the radio buttons as status_type[variable based on the id]. Unfortunately, this doesn't work. I was wondering if anyone could help me figure out how i'm supposed to format it or if it's even possible to declare a variable within a variable. Thanks!

2 Answers2

0

Your PHP code is not syntactically correct. Assuming that $info has the correct values, it should read

if ($_POST["status_type$info[id]"]) 

Notice:

  • The space was removed to match the HTML
  • The double quotes were removed from inside the string and now delimit it instead
  • The single quotes within the array index for $info were removed

You can also use the equivalent $_POST["status_type".$info['id']].

However, there are also numerous other issues with that code:

  • It appears that you are using the same HTML ids for multiple radio buttons in your page (ids are reused for each row). That is incorrect; ids should be unique.
  • The if should prevent an E_NOTICE if the variable is not defined; i.e. it should read if(isset($_POST[...])).
  • The code is vulnerable to SQL injection. This should be fixed right now.
Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
0

should be

if ($_POST["status_type".$info['id'].""]){
    .....
}

you have:

  if ($_POST['status_type "$info['id']"']) {
    .....
  }

also i would do smth like this: $statusType = "status_type".$info['id']; and use it like $_POST["$statusType"]

AND

$edit_status_sql="UPDATE status SET type = '."$statusType".' WHERE id = '".$id."'";