Php Ajax如何将数据同时插入两个表中

时间:2018-05-18 12:03:38

标签: php ajax

我正在尝试将数据插入两个不同的表中。我的问题是它只在一个表中插入“pm_imbox”,我不熟悉ajax所以无论如何我可以用ajax做到这一点吗?

这是我的代码

        <?php

    require_once"db.php";
    $to_username = $_POST['to_username'];
    $title = $_POST['title'];
    $message = $_POST['message'];
    $to_userid = $_POST['to_userid'];
    $userid = $_POST['userid'];
    $request_id = $_POST['request_id'];
    $from_username = $_POST['from_username'];
    $senddate = $_POST['senddate'];

    $stmt = $DBcon->prepare("INSERT INTO pm_outbox(userid,username,to_userid,to_username,title,content,ReqID,senddate)VALUES('$userid','$from_username','$to_userid','$to_username','$title','$message','$request_id','$senddate')");

    $stmt = $DBcon->prepare("INSERT INTO pm_imbox(userid,username,from_id,from_username,title,content,ReqID,recieve_date)VALUES('$to_userid','to_username','$userid','$from_username','$title','$message','$request_id','$senddate')");

    $stmt->bindparam(':to_username', $to_username);
    $stmt->bindparam(':title', $title);
    $stmt->bindparam(':message', $message);
    $stmt->bindparam(':to_userid', $to_userid);
    $stmt->bindparam(':userid', $userid);
    $stmt->bindparam(':request_id', $request_id);
    $stmt->bindparam(':from_username', $from_username);
    $stmt->bindparam(':senddate', $senddate);

    if($stmt->execute())
    {
      $res="Data Inserted Successfully:";
      echo json_encode($res);
    }
    else {
      $error="Not Inserted,Some Probelm occur.";
      echo json_encode($error);
    }



     ?>

我猜它可能是$stmt->bindparam,我不知道如何配置它们与查询链接 这是我到目前为止用我的ajax JavaScript文件所做的。

        <script type="text/javascript">

      function insertData() {
        var to_username=$("#to_username").val();
        var title=$("#title").val();
        var message=$("#message").val();
        var to_userid=$("#to_userid").val();
        var userid=$("#userid").val();
        var request_id=$("#request_id").val();
        var from_username=$("#from_username").val();
        var senddate=$("#senddate").val();


    // AJAX code to send data to php file.
            $.ajax({
                type: "POST",
                url: "reply_process.php",
                data: {to_username:to_username,title:title,message:message,to_userid:to_userid,userid:userid,request_id:request_id,from_username:from_username,senddate:senddate},
                dataType: "JSON",
                success: function(data) {
                 $("#message").html(data);
                $("p").addClass("alert alert-success");
                },
                error: function(err) {
                alert(err);
                }
            });

    }

      </script>

2 个答案:

答案 0 :(得分:0)

您的代码有一个变量$stmt来保存查询。 有两行将查询字符串分配给同一变量$stmt,因此最终查询$stmt变量将具有pm_imbox,因为它将替换{{1}的第一个分配的查询}

纠正它,你会很高兴:

pm_outbox

答案 1 :(得分:0)

下面的第二个声明会覆盖第一个声明,这就是为什么只有第二声明执行的原因:

$stmt1 = $DBcon->prepare("INSERT INTO pm_outbox(userid,username,to_userid,to_username,title,content,ReqID,senddate)VALUES('$userid','$from_username','$to_userid','$to_username','$title','$message','$request_id','$senddate')");

$stmt1->bindparam(':to_username', $to_username);
$stmt1->bindparam(':title', $title);
$stmt1->bindparam(':message', $message);
$stmt1->bindparam(':to_userid', $to_userid);
$stmt1->bindparam(':userid', $userid);
$stmt1->bindparam(':request_id', $request_id);
$stmt1->bindparam(':from_username', $from_username);
$stmt1->bindparam(':senddate', $senddate);

$stmt = $DBcon->prepare("INSERT INTO pm_imbox(userid,username,from_id,from_username,title,content,ReqID,recieve_date)VALUES('$to_userid','to_username','$userid','$from_username','$title','$message','$request_id','$senddate')");

$stmt->bindparam(':to_username', $to_username);
$stmt->bindparam(':title', $title);
$stmt->bindparam(':message', $message);
$stmt->bindparam(':to_userid', $to_userid);
$stmt->bindparam(':userid', $userid);
$stmt->bindparam(':request_id', $request_id);
$stmt->bindparam(':from_username', $from_username);
$stmt->bindparam(':senddate', $senddate);

if($stmt1->execute() && $stmt->execute())  // This will execute both the queries.
{
    ....
    //further code
    ....

所以,如果你需要一次执行两个查询,那么按如下方式更改逻辑。

 $stmt = $DBcon->prepare("INSERT INTO pm_outbox(userid,username,to_userid,to_username,title,content,ReqID,senddate)VALUES('$userid','$from_username','$to_userid','$to_username','$title','$message','$request_id','$senddate')");

 $stmt = $DBcon->prepare("INSERT INTO pm_imbox(userid,username,from_id,from_username,title,content,ReqID,recieve_date)VALUES('$to_userid','to_username','$userid','$from_username','$title','$message','$request_id','$senddate')");