douxin9135 2013-12-19 05:58
浏览 27
已采纳

通过PHP和JQuery提交多个数据库更新

I'm stuck with trying to process multiple mySQL updates at the same time. I have 4 select/optiion boxes that pull entries from a db table. I want to be able to update the db onChange using JQuery. I have managed to get this working with one select module but as soon as I add more it spins out. I know that the main bad code is in db_submit.php but really not sure how else to write it. I know there has to be a cleaner way to do this.

FORM PAGE- INPUT.PHP

<html>
<head>
<script src="../assets/scripts/jquery-2.0.3.min.js"></script>
<script> 
    function updateDb() {
     $.post("db_submit.php", $("#console").serialize());
    }
</script>
<?php
include 'db_connect.php';
?>
</head>

<body>
<form id="console">
    <select id="frame1" name="frame1" onChange="updateDb()">
        <option value="">Select Channel</option>
            <?php
            $result = mysqli_query($con,"SELECT * FROM feedContent");
            while ($row = mysqli_fetch_array($result)) {
                echo "<option value='" . $row['url'] . "'>" . $row['name'] . "</option>";
            }
            ?>
    </select>
    <select id="frame2" name="frame2" onChange="updateDb()">
        <option value="">Select Channel</option>
            <?php
            $result = mysqli_query($con,"SELECT * FROM feedContent");
            while ($row = mysqli_fetch_array($result)) {
                echo "<option value='" . $row['url'] . "'>" . $row['name'] . "</option>";
            }
            ?>
    </select>
    <select id="frame3" name="frame3" onChange="updateDb()">
        <option value="">Select Channel</option>
            <?php
            $result = mysqli_query($con,"SELECT * FROM feedContent");
            while ($row = mysqli_fetch_array($result)) {
                echo "<option value='" . $row['url'] . "'>" . $row['name'] . "</option>";
            }
            ?>
    </select>
    <select id="frame4" name="frame4" onChange="updateDb()">
        <option value="">Select Channel</option>
            <?php
            $result = mysqli_query($con,"SELECT * FROM feedContent");
            while ($row = mysqli_fetch_array($result)) {
                echo "<option value='" . $row['url'] . "'>" . $row['name'] . "</option>";
            }
            ?>
    </select>
</form>
</body>
<?php
mysqli_close($con);
?>
</html>

PROCESSING PAGE- DB_SUBMIT.PHP

<?php
include 'db_connect.php';   
$frame1= mysqli_escape_String($con,$_POST['frame1']);
$frame2= mysqli_escape_String($con,$_POST['frame2']);
$frame3= mysqli_escape_String($con,$_POST['frame3']);
$frame4= mysqli_escape_String($con,$_POST['frame4']);

$query = "UPDATE frameContent SET url='".$frame1."' WHERE name='frame1'";
$query = "UPDATE frameContent SET url='".$frame2."' WHERE name='frame2'";
$query = "UPDATE frameContent SET url='".$frame3."' WHERE name='frame3'";
$query = "UPDATE frameContent SET url='".$frame4."' WHERE name='frame4'";
mysqli_query($con,$query);

mysqli_close($con);
?>

I know that constantly setting the $query variable is causing problems but I'm not sure how else I can do this in the one page. Any help would be much appreciated.

Thanks!

  • 写回答

2条回答 默认 最新

  • dongzou7134 2013-12-19 06:01
    关注

    First of all make sure the $queries are concatenated, then terminate each query with a semi-colon. After these you can use mysqli_multi_query to execute all four updates in one call from php.

    $query = "UPDATE frameContent SET url='".$frame1."' WHERE name='frame1';";
    $query .= "UPDATE frameContent SET url='".$frame2."' WHERE name='frame2';";
    $query .= "UPDATE frameContent SET url='".$frame3."' WHERE name='frame3';";
    $query .= "UPDATE frameContent SET url='".$frame4."' WHERE name='frame4';";
    mysqli_multi_query($con,$query);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染