I'm trying to insert data into two different tables. My problem is that it only inserts into one table the "pm_imbox",I am not familiar with ajax so is there anyway that i can do this using ajax ?
Here is my code
<?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);
}
?>
I am guessing it might be the $stmt->bindparam
, I do not know how to config them to link with the query
This is what i have done so far with my ajax JavaScript file.
<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>