在 php 页面中编辑 sql 数据



我需要使用提交的数据编辑我的数据库表。

这是形式:

mysql_query("set names 'utf8'");
$query = "SELECT * FROM sec1octa"; 
$result = mysql_query($query);
?>
<div align="center">
    <form method="get" action="edit_data.php">
        <table width="104" border="1" class="center1">
            <tr>
                <th width="94">first</th>
                <th width="94">second</th>
                <th width="94">status</th>
            </tr>
            <tr>
                <?php
                if (mysql_num_rows($result) > 0) {
                    while ($row = mysql_fetch_array($result)) {
                        ?>
                        <tr>
                            <td><input type="text" name="id" value="<?php echo $row ['stu_no']; ?> " size=10></td>
                            <td><input type="text" name="name" value="<?php echo $row ['stu_name']; ?> " size=10></td> 
                            <td><?php 
                                echo '<select name="status">';   {
                                    echo '<option value="open">'.$row['stu_status'].'</option>';
                                    echo '<option value="close">'.prevent.'</option>';
                                }
                                echo '</select>';
                            ?></td>
                        </tr>
                        <?php
                    }
                }
                ?>
            </tr>
        </table>
        <input type="submit" name="submit"  value="done" />
    </form>

问题出在edit_data.php页面中。我不能UPDATE.

我使用此代码,但它不起作用。

require_once('../Connections/config.php');
$id= $_GET['id'];
$status= $_GET['status'];
$query=  mysql_query("UPDATE  `goh`.`sec1octa` SET  `stu_status` =  '$status' 
WHERE stu_no='".$id."'") or die (mysql_error ());
if($query){echo $status ."done ";}

您只获取edit_data.php $_GET中的最后一个值的原因是您没有将输入/选择名称设置为数组。

<input type="text" name="id" value="some_stu_no">

一遍又一遍地发生,每个新的都会覆盖以前的。

相反,您应该使用:

<input type="text" name="id[]" value="some_stu_no">

这将允许您在单个表单提交中传递多个 id。

您的表格:

<form method="POST" action="edit_data.php">
....
echo "<tr>";
    echo "<th>id</th>";
    echo "<th>name</th>";
    echo "<th>status</th>";
echo "</tr>";
if(mysql_num_rows($result)>0){
    while($row=mysql_fetch_array($result)){
        echo "<tr>";
            echo "<td><input type="text" name="id[]" value="{$row['stu_no']}" size="10"></td>";
            echo "<td>{$row['stu_name']}</td>"; 
            echo "<td>";
                echo "<select name="status[]">";  // I don't like your option set up here, but I don't fully understand it either.
                    echo "<option value="open">{$row['stu_status']}</option>";
                    echo "<option value="close">.prevent.</option>";
                echo "</select>";
            echo "</td>";
        echo "</tr>";
    }
}
....
<input type="submit" value="Submit All">
</form>

edit_data.php

// create a mysqli connection called $db
if(isset($_POST['id'])){
    $tally=0;
    // build all queries for the batch
    foreach($_POST['id'] as $index=>$id){
        $queries[]="UPDATE `goh`.`sec1octa` SET `stu_status`='".mysqli_real_escape_string($db,$_POST['status'][$index])."' WHERE `stu_no`='".mysqli_real_escape_string($db,$id)."'";
    }
    // run all queries
    if(mysqli_multi_query($db,implode(';',$queries)){
        do{
            $tally+=mysqli_affected_rows($db);
        } while(mysqli_more_results($db) && mysqli_next_result($db));
    }
    // assess the outcome
    if($error_mess=mysqli_error($db)){
        echo "Syntax Error: $error_mess";
    }else{
        echo "$tally row",($tally!=1?"s":"")," updated";
    }
    mysqli_close($con);
}

最新更新