Skip to content Skip to sidebar Skip to footer

Php - How Do I Insert Html Table Data Into Mysql

I have an HTML table with text and radio inputs that i want to insert each row into my MySQL table with PHP MySQL table looks like this: ================================ | Name

Solution 1:

Since the table is dynamically filled, you need to use an array as the name attribute

<table><tr><th>Name</th><th>Present</th><th>Excused</th><th>Unexcused</th><th>Ext</th></tr><?php$query = "select * from TbCard";
        $sql = mysqli_query($connect, $query);
        $count = 0;
            while ($data = mysqli_fetch_array($sql)) {
        ?><tr><td><inputname="tableRow[<?phpecho$count; ?>]['dataName']"id='name'type='text'value="<?phpecho$data['Name'];?>"readonlystyle='border:none;width:350px'></input></td><td><inputname="tableRow[<?phpecho$count; ?>]['status']"type="radio"value="Present"> Present
                </td><td><inputname="tableRow[<?phpecho$count; ?>]['status']"type="radio"value="Excused"> Excused
                </td><td><inputname="tableRow[<?phpecho$count; ?>]['status']"type="radio"value="Unexcused"> Unexcused
                </td></tr>;
        <?php$count++;
            }
        ?></table>

The php would be something like this, assuming that the data has values in it

$tableRow = $_POST['tableRow'];
foreach($tableRowas$row){
    echo$row['dataName'].' '.$row['status'].'<br/>';
}

That should show the values you chosen per row in the table, I don't use mysqli so I will not provide the the functions to insert it into the database, but the important thing is you now have the data needed

To see the content of the array, use print_r($tableRow)

NOTE: I removed the echo part in the table, I might have missed some quotes or some typos, just comment for clarifications

Solution 2:

Add name attribute to your radio button. However name should be unique for each table row, so you might end up with auto generated name. Something like this:

...
$index = 0;
while ($data = mysqli_fetch_array($sql)) {
    echo '<tr>';
    echo"<td><inputid='name'name='name_$index'type='text'value='" . $data['Name'] . "' readonlystyle='border:none;width:350px'></input></td>";
    echo"<td><inputtype='radio'name='status_$index'></td>";
    echo"<td><inputtype='radio'name='status_$index'></td>";
    echo"<td><inputtype='radio'name='status_$index'></td>";
    echo"<td><textareaname='Ext_$index'></textarea></td>";
    echo '</tr>';
    $index++;
}
...

As a matter of fact, you need to add a unique name to each of the fields, including text and textarea to make sure you insert values of each row separately.

Now to retrieve values for saving in the db, you could do something like this:

foreach($_POSTas$key => $val)
{
    if(strpos($key, 'name_') === 0 )
    {
        $criteria = explode("_", $key);
        $index = $criteria[2];
        //now you can get the rest of the fields$status = $_POST["status_$index"];
        $name = $_POST["name_$index"];
        $ext = $_POST["Ext_$index"];
        //assuming you have a SaveFunction:
        SaveFunction($name, $status, $ext);
    }
}

Solution 3:

first of all your are not sending your data properly

echo"<td><input id='name' type='text' value='" . $data['Name'] . "' readonly style='border:none;width:350px'></input></td>";
            echo"<td><input type='radio' name='status' value='Present' ".isset($data['Status']) && $data['Status']=='Present'? checked='checked':null."></td>";
            echo"<td><input type='radio' name='Status' value='Excused' ".isset($data['Status']) && $data['Status']=='Excused'? checked='checked':null."></td>";
            echo"<td><input type='radio' name='Status' value='Unexcused' ".isset($data['Status']) && $data['Status']=='Unexcused'? checked='checked':null."></td>";
            echo"<td><textarea name='Ext'></textarea></td>";

This is only the part for viewing and insertion properly. let me know if you need help in insert.php side

Post a Comment for "Php - How Do I Insert Html Table Data Into Mysql"