3

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      | Status | Ext |
================================

HTML table

===============================================================
|     Name      | Present | Excused | Unexcused |     Ext     | 
===============================================================
|  text input1  |    o    |    o    |     o     |  textarea   |
---------------------------------------------------------------
|  text input2  |    o    |    o    |     o     |  textarea   |
and so on...
*o's are radios

it gets values from a MySQL query loop and i want it to be only one of the radios can be clicked and it sends the clicked one into the Status column

Code

 <form method="post" action="insert.php" enctype="multipart/form-data">
    <table>
        <tr>
            <th>Name</th>
            <th>Present</th>
            <th>Excused</th>
            <th>Unexcused</th>
            <th>Ext</th>
        </tr>
        <?php         
        echo "<tr>";
        $query = "select * from TbCard";
        $sql = mysqli_query($connect, $query);
            while ($data = mysqli_fetch_array($sql)) {
                echo '<tr>';
                echo"<td><input id='name' type='text' value='" . $data['Name'] . "' readonly style='border:none;width:350px'></input></td>";
                echo"<td><input type='radio'></td>";
                echo"<td><input type='radio'></td>";
                echo"<td><input type='radio'></td>";
                echo"<td><textarea></textarea></td>";
                echo '</tr>';
            }
        }
        echo "</tr>";
        ?>
    </table>
<input type="submit" value="Insert">
</form>

EDIT: The table is in a form tag and has the action to another .php and i want a submit button to insert it

4
  • so you want to insert this all data in 1 column in your mysql table? Commented Oct 10, 2017 at 7:05
  • no, the text input inserts into the 'Name' column, the selected radio out of the 3 radios inserts into the 'Status' column, the textarea inserts into the 'Ext' column and all of them results in one row so it inserts multiple rows Commented Oct 10, 2017 at 7:07
  • You want to use a submit button? Is the submit button separate from the form? Commented Oct 10, 2017 at 7:15
  • @Swellar no, the submit button is in the form tag Commented Oct 10, 2017 at 7:17

3 Answers 3

5

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>
                    <input name="tableRow[<?php echo $count; ?>]['dataName']" id='name' type='text' value="<?php echo $data['Name'];?>" readonly style='border:none;width:350px'></input>
                </td>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Present"> Present
                </td>
                <td>
                    <input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Excused"> Excused
                </td>
                <td>
                    <input name="tableRow[<?php echo $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($tableRow as $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

Sign up to request clarification or add additional context in comments.

Comments

0

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><input id='name' name='name_$index' type='text' value='" . $data['Name'] . "' readonly style='border:none;width:350px'></input></td>";
    echo"<td><input type='radio' name='status_$index'></td>";
    echo"<td><input type='radio' name='status_$index'></td>";
    echo"<td><input type='radio' name='status_$index'></td>";
    echo"<td><textarea name='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($_POST as $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);
    }
}

2 Comments

and how do you insert the data into the database?
@Swellar from another table i probably didn't choose the best thing to show the text so i just went to put the text into a readonly input text
0

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

3 Comments

yeah sure how do you make the insert.php?
sorry for the late reply, can you please update your question showing your efforts in insert.php?
nevermind, i already found the answer from another guy, thanks though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.