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

时间:2013-12-19 05:58:21

标签: javascript php jquery mysql forms

我一直试图同时处理多个mySQL更新。我有4个select / optiion框,用于从db表中提取条目。我希望能够使用JQuery更新db onChange。我已经成功地使用了一个选择模块,但是只要我添加更多它就会旋转。我知道主要的错误代码是在 db_submit.php 中,但实际上不确定如何编写它。我知道必须有一个更清洁的方法来做到这一点。

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>

处理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);
?>

我知道不断设置$ query变量会导致问题,但我不知道我怎么能在一页中做到这一点。任何帮助将不胜感激。

谢谢!

2 个答案:

答案 0 :(得分:3)

首先确保连接$queries,然后用分号终止每个查询。在这些之后,您可以使用mysqli_multi_query在一次调用中执行所有四个更新。

$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 :(得分:0)

我认为这可能会有所帮助:)但您的代码中只有一些变化:

<html>
<head>
<script src = "js/jquery-1.10.1.js"></script>
<script> 
function updateDb() 
{
 // this var id will store all your 4 combobox values in an array
  var id = [{val1: $("#frame1").val()},
           {val1: $("#frame2").val()},
           {val1: $("#frame3").val()},
           {val1: $("#frame4").val()}];

 //this .post will submit all data to db_submit.php
   $.post("db_submit.php",{id:id}, function(data)
  {
           alert(data);
  });
</script>
<?php
  include 'db_connect.php';
?>
</head>
<body>
<select id="frame1" name="frame1">
    <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">
    <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">
    <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">
    <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>

<input type="button" value="Submit" onClick="updateDb()"/>
</body>
<?php
   mysqli_close($con);
?>
</html>

在你的DB_SUBMIT.PHP

 <?php
 include 'db_connect.php';   
 $frame1= mysqli_escape_String($_POST['id'][0]['val1']);
 $frame2= mysqli_escape_String($_POST['id'][1]['val1']);
 $frame3= mysqli_escape_String($_POST['id'][2]['val1']);
 $frame4= mysqli_escape_String($_POST['id'][3]['val1']);

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

 echo "Data was Successfully updated";
 mysqli_close($con);
 ?>

我只是为了方便而在那里添加一个按钮,但是如果你不想要它只是删除它并在你拥有的每个组合框上放回onChange:)