1

I have been strongly with php issues and i thought i should ask a question, hopefully i get help here.

I have dynamic html form with radio with php variable as input name but am unable to get the value of the radio button. Below are the both php and html codes:

if(isset($_POST["title"])) {            
$count = count($_POST["title"]);
for($i=0;$i<$count;$i++) {
    if(!empty($_POST["title"][$i])) {
        $pid = intval($_POST["pid"][$i]);
        $title = $_POST["title"][$i];
        $available = $_POST["{$pid}"];
        //exit;
        $sql = "UPDATE tracks SET title='$title', available='$available', WHERE id='$pid'"; 
        $query = mysqli_query($db_connect, $sql);
    }
}
$count++;

}

<table>
 <tr>
   <td><input name="<?php echo $pid; ?>[]" type="radio" 
   id="sell<?php echo $pid; ?>" value="Sell"></td>
   <td width="27%">
   <label for="sell<?php echo $pid; ?>">Sell </label></td>

   <td width="4%"><input name="<?php echo $pid; ?>[]" 
   id="pvcy<?php echo $pid; ?>" type="radio" value="Private"></td>
    <td width="17%">
    <label for="pvcy<?php echo $pid; ?>">Private</label>
    </td>

    <td width="4%">
    <input name="<?php echo $pid; ?>[]" id="pblc<?php echo $pid; ?>"
     type="radio" value="public"></td>
   <td width="43%">
   <label for="pblc<?php echo $pid; ?>"> Free </label></td>
  </tr>
</table>

I am not able to get the value of the radio button from the input name:

name="<?php echo $pid; ?>[]"

Any assistance would be appreciated.

Harrison O
  • 1,119
  • 15
  • 20
  • Please elaborate: your need is not clear at all. Is your PHP code preceding the HTML part, or at the opposite using what comes from it? Anyway there are some strange points, like `$_POST["{$pid}"]`. In the other hand, do you mean that `name="[]"` doesn't currently give the expected HTML attribute? – cFreed Dec 06 '16 at 18:09
  • What is the value of `$_POST["title"]`? On second thought, what do you get when you `print_r($_POST)`? – cck Dec 06 '16 at 18:12
  • Thanks for your time. I want to retrieve the value of the radio button from the variable name – Harrison O Dec 06 '16 at 18:12
  • i was able to get the values of all other inputs excepts the value of the radio button because that is the only input i added php variable on its name – Harrison O Dec 06 '16 at 18:14
  • Thanks for your assistance Rajdeed, here is the error am getting: Undefined offset: 28 in E:\\----index.php on line 120 – Harrison O Dec 06 '16 at 18:28
  • @HarrisonOziegbe Where are you setting this `$pid`? Make sure you set this value before the form processing, otherwise you will get this error. Moreover, I've given an answer below. – Rajdeep Paul Dec 06 '16 at 18:35
  • Thanks, i was able to figure it out. the error was due to the radio button not selected or checked. any idea on how to validate it again unselected button? – Harrison O Dec 06 '16 at 18:40
  • @HarrisonOziegbe What do you mean by *... how to validate it again unselected button?* – Rajdeep Paul Dec 06 '16 at 18:41
  • @Rajdeep Paul, thanks, i have taken care of that now. Thanks. – Harrison O Dec 06 '16 at 18:48
  • @HarrisonOziegbe Please *accept* the answer if it resolved your issue. Furthermore, I've updated my answer to clarify your above query, please see the **Update** section of my answer. – Rajdeep Paul Dec 06 '16 at 18:52

1 Answers1

0

There are few things you need to change in your code, such as:

  • Change this statement

    $available = $_POST["{$pid}"];
    

    to

    $available = $_POST[$pid][0];
    
  • Your UPDATE query is wrong.

    $sql = "UPDATE tracks SET title='$title', available='$available', WHERE id='$pid'";
                                                                    ^ see this comma
    

    Remove this comma from your query.

  • There's no point using this $count++; after the for loop, so remove this statement as well.

Update:

From your comment,

... the error was due to the radio button not selected or checked. any idea on how to validate it again unselected button?

There are two things you need to change in your code, such as:

  • Change this statement

    $available = $_POST["{$pid}"];
    

    to

    $available = isset($_POST[$pid][0]) ? $_POST[$pid][0] : "";
    
  • Incrementally construct your UPDATE query, like this:

    $sql = "UPDATE tracks SET title='$title'";
    if(!empty($available)){
        $sql .= ", available='$available'";
    }
    $sql .= " WHERE id='$pid'";
    

Sidenote: Learn about prepared statements because right now your query is susceptible to SQL injection attacks. Here's a good read on how you can prevent SQL injection in PHP.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • Injecting $_POST values directly into your query string makes your database vulnerable to attacks and unexpected failures. Prepared statements should be used. – mickmackusa Apr 09 '22 at 06:35