如何更新选定的列

时间:2016-01-06 21:20:23

标签: php mysql

我点击保存按钮显示

时有这段代码

警告:在第19行的C:\ xampp \ htdocs \ wen \ up.php中从空值创建默认对象

警告:mysqli_query()要求参数2为字符串,对象在第24行的C:\ xampp \ htdocs \ wen \ up.php中给出 错误:

$ mysqli-> prepare =" UPDATE coursetaken set complated =?,passed = ?,       grade =?其中courseid =?&#34 ;;

$affected = 0;

$query=mysqli_query($cid, $mysqli) or die('Error: ' . mysqli_error($cid));

当它保存在一行中时,它是否总是更新所有行?即使我没有改变其他行的任何价值? 。我如何才能更新已保存的按钮并在下次显示而无需选择。

<body>
    <form action="up.php" name="frmAdd" method="post">
        <div align='center'>
            <table border='1' cellpadding='5' cellspacing='1' id='mytable'>
                <tr align='center'>
                    <th>courseid</th>
                    <th>cid = mysqli_connect($mysqli_hostname, $mysqli_user,    $mysqli_password,$mysqli_database) or die("Opps some thing went wrong");



                        if (mysqli_connect_errno()) {
                        die ("Connect failed: " . mysqli_connect_error());
                        }
                        if ($_SERVER['REQUEST_METHOD'] == "POST") { 


                        $SQL = "UPDATE coursetaken set 'completed' = ?, 'passed' = ? 'grade' = ? where 'courseid' = ?";


                        $stmt = mysqli_prepare($cid, $SQL);
                        $affected = 0;

                        $query=mysqli_query($cid, $SQL) or die('Error: ' . mysqli_error($cid));



                        $ids = $_POST['courseid'];
                        foreach($ids as $id) {
                        $complated = $_POST["complated_$id"];
                        $passed = $_POST["passed_$id"];
                        $grade = $_POST["grade_$id"];

                        mysqli_stmt_bind_param($query, "ssi", $complated, $passed, intval($id));
                        if(mysqli_stmt_execute($query)) {
                        $affected += mysqli_affected_rows();
                        } else {
                        echo "error: data has not been updated for course $id<br>";
                        }
                        }
                        echo "$affected records have been updated<br>";
                        mysqli_stmt_close($query);
                        }?></th>
                    <th>complate</th>
                    <th>passed</th>
                    <th>grade</th>
                    <th>Action</th>
                </tr>
                <?php
                include('config.php');
                $sql = "select * from coursetaken";
                $result = mysql_query($sql);
                while ($row = mysql_fetch_assoc($result)) { // use assoc
                    $id = $row['courseid'];
                    ?>
                    <tr  class='edit_tr' id='<?= $id ?>'>
                        <td class='edit_td'>
                            <input type="hidden" name="courseid[]" value="<?= $id ?>" />
                            <span class='text' id='id_<?= $id ?>' ><?= $id ?></span>
                        </td>
<td class='edit_td'>
                <span class='text' id='stduentid_<?=$id?>' ><?  =$row['stduentid']?></span>
            </td> 


                        <td class='edit_td'>
                            <select name='complated_<?= $id ?>' id='complated_<?= $id ?>'>
                                <option value='Yes' <?= $row['complated'] == 'Yes' ? 'selected' : '' ?> >Yes</option>
                                <option value='No' <?= $row['complated'] == 'No' ? '  selected' : '' ?>          >No</option>
                            </select>
                        </td>
                        <td class='edit_td'>
                            <select name='passed_<?= $id ?>' id='passed_<?= $id ?>'>
                                <option value='Yes' <?= $row['passed'] == 'Yes' ? 'selected' : '' ?> >Yes</option>
                                <option value='No' <?= $row['passed'] == 'No' ? 'selected' : '' ?> >No</option>
                            </select>
                        <td class='edit_td'>
                            <select name='passed_<?= $id ?>' id='grade_<?= $id ?>'>
                                <option value='Yes' <?= $row['grade'] == 'Yes' ? 'selected' : '' ?> >A</option>
                                <option value='No' <?= $row['grade'] == 'No' ? 'selected' : '' ?> >B+</option>
                                <option value='No' <?= $row['grade'] == 'No' ? 'selected' : '' ?> >B</option>
                                <option value='No' <?= $row['grade'] == 'No' ? 'selected' : '' ?> >C+</option>
                                <option value='No' <?= $row['grade'] == 'No' ? 'selected' : '' ?> >C</option>
                                <option value='No' <?= $row['grade'] == 'No' ? 'selected' : '' ?> >D+</option>
                                <option value='No' <?= $row['grade'] == 'No' ? 'selected' : '' ?> >D</option>
                                <option value='No' <?= $row['grade'] == 'No' ? 'selected' : '' ?>    >F</option>

                            </select>
                        <td>
                            <input type="submit" name="submit" value="save"      class=  "button">

                        </td>
                        </td> 
                    </tr>
    <?php
}
?>
            </table>
            <p align="center"><a href="year.html">Go Back</a> </p>
    </form>
</div>
</body>
</html>

更新查询。

   cid = mysqli_connect($mysqli_hostname, $mysqli_user, $mysqli_password,      $mysqli_database) or die("Opps some thing went wrong");



if (mysqli_connect_errno()) {
    die("Connect failed: " . mysqli_connect_error());
}
   if ($_SERVER['REQUEST_METHOD'] == "POST") { 

$mysqli->prepare = "UPDATE coursetaken set ` complated` = ?,` passed ` = ?,
  ` grade ` = ? where ` courseid ` = ?";

$affected = 0;

$query=mysqli_query($cid, $mysqli) or die('Error: ' . mysqli_error($cid));



$ids = $_POST['courseid'];
foreach($ids as $id) {
          $stduentid = $_POST["stduentid_$id"];

    $complated = $_POST["complated_$id"];
    $passed = $_POST["passed_$id"];
$grade = $_POST["grade_$id"];


$stmt->bind_param( "ssi",$stduentid, $complated, $passed,$grade);
    if(mysqli_stmt_execute($query)) {
         $affected += mysqli_affected_rows();
    } else {
        echo "error: data has not been updated for course $id<br>";
    }
}
echo "$affected records have been updated<br>";
mysqli_stmt_close($query);
}


 ?>

0 个答案:

没有答案