douhutongvm382381 2015-10-19 04:53
浏览 17

SQL:在php前端更新多行

I am trying to update "row2" when the checkbox is ticked. everything works fine when I am only updating 1 row at a time but when selecting multiple rows ALL checked rows get the same value.

I am looking for a way to add an id to the textarea tag so multiple rows can be updated with unique values.

I think the issue is with $new_price in db.config or the way I am updating the database in action2.php

Thanks for any help or anything that can point me in the right direction.

Index.php:

<form action="action2.php" id="bulk_action_form" method="post" name=
"bulk_action_form" onsubmit="return deleteConfirm();"></form>
<table class="bordered tablesorter" id="myTable">
    <thead>
        <tr>
            <th class="th-class check-box"><input id="select_all" name=
            "select_all" type="checkbox" value=""></th>
            <th class="th-class">ID</th>
            <th class="th-class">row2</th>
            <th class="th-class">row3</th>
        </tr>
    </thead><?php

                                if(mysqli_num_rows($query) > 0){

                                    while($row = mysqli_fetch_assoc($query)){

                            ?>
    <tr class="">
        <td align="center" class="checkbox-td"><input class="checkbox"
        height="30" name="checked_id[]" type="checkbox" value=
        "<?php echo $row['id']; ?>" width="30"></td>
        <td class="td-class"><?php echo $row['id']; ?></td>
        <td class="td-class reg-anote">
        <textarea class="text a-note" cols="10" id="confirmationText" name=
        "confirmationText" rows="1">
<?php echo $row['row2']; ?>
</textarea></td>
        <td class="td-class"><?php echo $row['row3']; ?></td>
    </tr><?php } }else{ ?>
    <tr>
        <td colspan="5">No records found.</td>
    </tr><?php } ?>
</table>
<form>
    <input class="btn btn-danger" name="bulk_delete_submit" type="submit"
    value="Delete"> <button name="price" type="submit">Submit</button>
</form>

action2.php:

<?php
session_start();
include_once('dbConfig.php');  
if(isset($_POST['bulk_delete_submit'])){
    $idArr = $_POST['checked_id'];
    foreach($idArr as $id){
        mysqli_query($conn,"DELETE FROM $TableName2 WHERE id=".$id);
    }
}  
if(isset($_POST['confirmationText'])){
    $idArr = $_POST['checked_id'];
    foreach($idArr as $id){
        mysqli_query($conn,"UPDATE $TableName2 SET Column2 WHERE id=".$id);
    }
}
?>

dbConfig.php:

<?php   
$new_price = $_POST['confirmationText'];
$TableName2 = 'Table2';
$dbHost = 'localhost';  //database host name
$dbUser = 'username';       //database username
$dbPass = 'password';           //database password
$dbName = 'databasename'; //database name
$conn = mysqli_connect($dbHost,$dbUser,$dbPass,$dbName);
if(!$conn){
    die("Database connection failed: " . mysqli_connect_error());
}
?>
  • 写回答

1条回答 默认 最新

  • duanouyong4228 2015-10-19 05:04
    关注

    you have to make change of textarea fields as like below-

    <textarea class="text a-note" cols="10" id="confirmationText" name=
            "confirmationText[]" rows="1">
    

    and In action2.php make the necessary changes like below-

    <?php
    session_start();
    include_once('dbConfig.php');  
    if(isset($_POST['bulk_delete_submit'])){
        $idArr = $_POST['checked_id'];
        foreach($idArr as $id){
            mysqli_query($conn,"DELETE FROM $TableName2 WHERE id=".$id);
        }
    }  
    
    if(isset($_POST['confirmationText'])){
        $idArr = $_POST['checked_id'];
        foreach($idArr as $key => $id){
            mysqli_query($conn,"UPDATE $TableName2 SET Column2='".$new_price[$key]."' WHERE id=".$id);
        }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度