简体   繁体   中英

How to update data from dynamically created table to database in php

I have create a table using php dynamically. But i need to update some of the cell value depending on user input. But i can't figure out how to do that. my codes are given below. I have found a way to make names an array is to use [] in names attribute from stackoverflow. but it didn't work for me.If i echo $_POST['std_name'] in while loop or outside of loop only the last row of student name is counted. Please Someone help. I am stuck here for 3 days.

<form action="" method="post">
        <table>
            <tr>
                <th>Student ID</th>
                <th>Student Name</th>
                <th>Student Father Name</th>
                <th>Student Mother Name</th>
                <th>Student Parents Mobile Number</th>
                <th>Student Mobile Number</th>
                <th>Student Batch Name</th>
                <th>Student College Name</th>
                <th>Student Gender</th>
                <th>Student Picture</th>
                <th>Student Admit Date</th>
                <th>January</th>
                <th>February</th>
                <th>March</th>
                <th>April</th>
                <th>May</th>
                <th>June</th>
                <th>July</th>
                <th>August</th>
                <th>September</th>
                <th>October</th>
                <th>November</th>
                <th>December</th>
            </tr>
            <tr>
                <?php
            $host="localhost";
            $username="root";
            $password="";
            $db="paragon_first_year";
            $qry="select * from `student_info`";
            $con=mysqli_connect($host,$username,$password);
            mysqli_select_db($con,$db);
            $res=mysqli_query($con,$qry);
            if(mysqli_num_rows($res)>0){
                while($dt=mysqli_fetch_array($res)){?>
                <td><input type="text" name="std_id[]" value="<?php echo $dt['std_id']?>"></td>
                <td><input type="text" name="std_name[]" value="<?php echo $dt['std_name']?>"></td>
                <td><input type="text" name="std_father_name[]" value="<?php echo $dt['std_father_name']?>"></td>
                <td><input type="text" name="std_mother_name[]" value="<?php echo $dt['std_mother_name']?>"></td>
                <td><input type="text" name="std_parent_mob[]" value="<?php echo $dt['std_parents_mob_no']?>"></td>
                <td><input type="text" name="std_mob[]" value="<?php echo $dt['std_mob_no']?>"></td>
                <td><?php echo $dt['std_batch_name'] ?></td>
                <td><?php echo $dt['std_college_name'] ?></td>
                <td><?php echo $dt['std_gender'] ?></td>
                <td><img style="height:100px;width:100px;" src="uploadImage/<?php echo $dt['std_name']."_".$dt['std_pic'] ?>" alt=""></td>
                <td><?php echo $dt['std_admit_date'] ?></td>
                <td><input type="text" name="jan[]" value="<?php echo $dt['january']?>"></td>
                <td><input type="text" name="feb[]" value="<?php echo $dt['february']?>"></td>
                <td><input type="text" name="mar[]" value="<?php echo $dt['march']?>"></td>
                <td><input type="text" name="apr[]" value="<?php echo $dt['april']?>"></td>
                <td><input type="text" name="may[]" value="<?php echo $dt['may']?>"></td>
                <td><input type="text" name="jun[]" value="<?php echo $dt['june']?>"></td>
                <td><input type="text" name="jul[]" value="<?php echo $dt['july']?>"></td>
                <td><input type="text" name="aug[]" value="<?php echo $dt['august']?>"></td>
                <td><input type="text" name="sep[]" value="<?php echo $dt['september']?>"></td>
                <td><input type="text" name="oct[]" value="<?php echo $dt['october']?>"></td>
                <td><input type="text" name="nov[]" value="<?php echo $dt['november']?>"></td>
                <td><input type="text" name="dec[]" value="<?php echo $dt['december']?>"></td>
            </tr><?php 

                }
            }
        ?>

        </table>
        <center>
            <input type="submit" name="submit" value="Update Data">
        </center>
</form>
</body>

my table is showing nicely

hope someone help. Thanks in advance.

You can use array keys to pass information. In this example, the first key is the database primary key, and the second key is the variable name.

The resulting array would look something like this:

$row[123][std_name] = 'joe';
$row[123][std_father_name] = 'bob';
... etc
$row[124][std_name] = 'sally';
$row[124][std_father_name] = 'john';
...etc

Then, as you iterate through the rows, you get the key ( 123 ) to use to update the database, as in where std_id=123 .

Note: I have used prepared statements to show updating the database. This is essential. Never put variables in a query using concatenation ( ie, "select * from table where id='$id'" )

Lastly, note the structure. Start with PHP, no print or echo statements. This allows you to work with user input and then redirect. First initialize, then work with user input, then do page logic, and lastly get out of php and output the view (html)

This example is not copy and paste; it has things that need to be filled in, and probably debugged. It is merely intended to show the big picture.

<?php

// initialize
$host="localhost";
$username="root";
$password="";
$db="paragon_first_year";
$con=mysqli_connect($host,$username,$password);

// a simple wrapper to make the prepared query more manageable
// see https://phpdelusions.net/mysqli/mysqli_connect#helper
function prepared_query($mysqli, $sql, $params, $types = "")
{
    $types = $types ?: str_repeat("s", count($params));
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    return $stmt;
}

// work with user input
if($_SERVER['REQUEST_METHOD'] === 'POST') {

    $dataRows = (array)$_POST['data'];
    foreach($dataRows as $id=>$data) {

        $sql = "INSERT INTO student_info SET std_name=?, std_father_name=?, " .
               // etc  . 
               "WHERE std_id=?";
        $params = array(
                    $data['std_name'], 
                    $data['std_father_name'], 
                    // etc... 

                    $id
                  );
        // this does the prepare, bind, and execute... Yes, I could just do the 
        // bind and execute here, but the time savings isn't worth the trouble.
        $stmt = prepared_query($con, $sql, $params); 

    }

    // always redirect after a POST
    header('Location: /path/to/next/page/or/this/page');
    die;
}

// no user input; ok to use plain old query.
$qry="select * from `student_info`";
mysqli_select_db($con,$db);
$res=mysqli_query($con,$qry);

?>
<html>

...

<form action="" method="post">
    <table>
        <tr>
            <th>Student ID</th>
            <th>Student Name</th>
            <th>Student Father Name</th>
            <th>Student Mother Name</th>
            <th>Student Parents Mobile Number</th>
            <th>Student Mobile Number</th>
            <th>Student Batch Name</th>
            <th>Student College Name</th>
            <th>Student Gender</th>
            <th>Student Picture</th>
            <th>Student Admit Date</th>
            <th>January</th>
            <th>February</th>
            <th>March</th>
            <th>April</th>
            <th>May</th>
            <th>June</th>
            <th>July</th>
            <th>August</th>
            <th>September</th>
            <th>October</th>
            <th>November</th>
            <th>December</th>
        </tr>
        <!-- don't need mysqli_num_rows; the while won't loop if no results -->
        <?php while($dt=mysqli_fetch_array($res)): ?>
        <tr>
            <td><?= $dt['std_id'] ?></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_name']" value="<?= $dt['std_name'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_father_name']" value="<?= $dt['std_father_name'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_mother_name']" value="<?= $dt['std_mother_name'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_parent_mob']" value="<?= $dt['std_parents_mob_no'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_mob']" value="<?= $dt['std_mob_no'] ?>"></td>
            <td><?= $dt['std_batch_name']  ?></td>
            <td><?= $dt['std_college_name']  ?></td>
            <td><?= $dt['std_gender']  ?></td>
            <td><img style="height:100px;width:100px;" src="uploadImage/<?= $dt['std_name']."_".$dt['std_pic']  ?>" alt=""></td>
            <td><?= $dt['std_admit_date']  ?></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['jan']" value="<?= $dt['january'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['feb']" value="<?= $dt['february'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['mar']" value="<?= $dt['march'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['apr']" value="<?= $dt['april'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['may']" value="<?= $dt['may'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['jun']" value="<?= $dt['june'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['jul']" value="<?= $dt['july'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['aug']" value="<?= $dt['august'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['sep']" value="<?= $dt['september'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['oct']" value="<?= $dt['october'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['nov']" value="<?= $dt['november'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['dec']" value="<?= $dt['december'] ?>"></td>
        <?php endwhile; ?>
        </tr>
    </table>
    <center>
        <input type="submit" name="submit" value="Update Data">
    </center>
</form>
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM