简体   繁体   English

PHP已准备好的将数据插入除一个表以外的所有表中的语句(MySQL)

[英]PHP Prepared statements inserting data into all but one table (MySQL)

I'm working on an inbox system. 我正在使用收件箱系统。 On the front end, it uses jQuery and Ajax so the page doesn't refresh. 在前端,它使用jQuery和Ajax,因此页面不会刷新。 I've got that part handled. 我已经处理了那部分。 On the back end, there are 3 tables (for now) that get data inserted. 在后端,有3个表(目前)可插入数据。

Here is a basic rundown of the relation structures: 这是关系结构的基本概要:

conversations:
conversation_id int(11) primary key
conversation_subject varchar(128)

conversations_members:
conversation_id int(11)
user_id int(11)
conversation_last_view int(10)
conversation_deleted int(1)

conversations_messages:
message_id int(11) primary key
conversation_id int(11)
user_id int(11)
message_date timestamp
message_text text

There is an additional problem since the sender_id is always 0, but that will have to be for another question since it's off topic. 还有一个问题,因为sender_id始终为0,但这将是另一个问题,因为它不在主题之列。 The problem lies in the conversations_members table. 问题出在sessions_members表中。 Everything else gets entered into the conversations and conversations_messages tables. 其他所有内容都输入到对话和sessions_messages表中。 Here is the PHP. 这是PHP。 The issue is the very last SQL query at the bottom: 问题是底部的最后一个SQL查询:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

include('../inc/connect.php');
if (!isset($_SESSION['username'])) {
    session_start();
}
$recipient_username = "";
$sender_id = "";
$a = 0;
$b = 0;
if(isset($_POST['subject'], $_POST['msg_body']) &&     !empty($_POST['subject']) && !empty($_POST['msg_body'])) {

//get ID of sender
$sender_id_query = "SELECT id FROM `users` WHERE username = ?";
$stmt = $connection->prepare($sender_id_query);
$stmt->bind_param('s', $_SESSION['username']);
$stmt->execute();
$result = mysqli_query($connection, $sender_id_query);
if($result) {
    while($row = mysqli_fetch_assoc($result)) {
        //$row['id'] = $sender_id;           //neither of these work
        $sender_id = $connection->insert_id; //Always zero
    }
}
$stmt->close();

//get username of recipient
$recipient_name_query = "SELECT * FROM `users`";
$result = mysqli_query($connection, $recipient_name_query);
if($result) {
    while($row = mysqli_fetch_assoc($result)) {
        $row['username'] = $recipient_username;
    }
}
//define post variables
$msg_subject = $_POST['subject'];
$msg_body = $_POST['msg_body'];
$subject = $connection->real_escape_string(htmlentities($msg_subject));
$body = $connection->real_escape_string(htmlentities($msg_body));
$conversation_id = mysqli_insert_id($connection);


//GET RECIPIENT ID
$sql = "SELECT id FROM `users` WHERE username=?";
$stmt = $connection->prepare($sql);
$stmt->bind_param('s', $recipient_username);
$result = mysqli_query($connection, $sql);
if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        $recipient_id = $row['id'];
    }
}
$stmt->close();

//INSERT SUBJECT INTO CONVERSATIONS TABLE
$stmt = $connection->prepare("INSERT INTO `conversations` (conversation_subject) VALUES(?)");
$stmt->bind_param('s', $subject);
$stmt->execute();
$stmt->close();


//INSERT THE IDs AND TIMESTAMPS INTO MESSAGES TABLE
$stmt = $connection->prepare("INSERT INTO `conversations_messages` (conversation_id, user_id, message_date, message_text)
                              VALUES(?, ?, NOW(), ?)");
$stmt->bind_param('iis', $conversation_id, $sender_id, $body);
$stmt->execute();
$stmt->close();

/*
 THE FOLLOWING DATA DOES NOT GET INSERTED.....
*/
//INSERT IDs, LAST_VIEWED, AND DELETED INTO MEMBERS TABLE
$stmt = $connection->prepare("INSERT INTO `conversations_members` (conversation_id, user_id, conversation_last_view, conversation_deleted)
                              VALUES (?, ?, ?, ?)");
$stmt->bind_param('iiii', $conversation_id, $recipient_id, $a, $b);
$stmt->execute();
$stmt->close();
}

I get no errors, and I'm not seeing any typos. 我没有错误,也没有任何错字。 Where did I go wrong? 我哪里做错了?

Thanks to the suggestion of additional error checking, it led me to discover what was happening. 多亏了其他错误检查的建议,它使我发现了正在发生的事情。 I completely removed the while loops, and gave each $stmt variable it's own name since I discovered another error after removing the while loops. 我完全删除了while循环,并为每个$ stmt变量指定了自己的名称,因为在删除while循环后发现了另一个错误。 Every statement after the first was returning a FALSE value since the previous statement wasn't closed. 由于前一个语句未关闭,因此第一个语句之后的每个语句都将返回FALSE值。 This code works. 此代码有效。 I get no errors, and it inserts everything into the database as required. 我没有收到任何错误,它会根据需要将所有内容插入数据库。

$recipient_username = $_GET['username'];
$username = $_SESSION['username'];
$a = 0;
$b = 0;


//get ID of sender
$sender_id_query = "SELECT id FROM `users` WHERE username = ?";
$stmt = $connection->prepare($sender_id_query);
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($sender_id);
$val = $stmt->fetch()[$sender_id];
$stmt->close();

//define post variables
$msg_subject = $_POST['subject'];
$msg_body = $_POST['msg_body'];
$subject = $connection->real_escape_string(htmlentities($msg_subject));
$body = $connection->real_escape_string(htmlentities($msg_body));
$conversation_id = mysqli_insert_id($connection);

//GET RECIPIENT ID
$recipient_id_query = "SELECT id FROM `users` WHERE username=?";
$stmt2 = $connection->prepare($recipient_id_query);
$stmt2->bind_param('s', $recipient_username);
$stmt2->execute();
$stmt2->bind_result($recipient_id);
$val_2 = $stmt2->fetch()[$recipient_id];
$stmt2->close();

//INSERT SUBJECT INTO CONVERSATIONS TABLE
$stmt3 = $connection->prepare("INSERT INTO `conversations` (conversation_subject) VALUES(?)");
$stmt3->bind_param('s', $subject);
$stmt3->execute();
$stmt3->close();

//INSERT THE IDs AND TIMESTAMPS INTO MESSAGES TABLE
$stmt4 = $connection->prepare("INSERT INTO `conversations_messages` (conversation_id, user_id, message_date, message_text)
                              VALUES(?, ?, NOW(), ?)");
$stmt4->bind_param('iis', $conversation_id, $sender_id, $body);
$stmt4->execute();
$stmt4->close();


//INSERT IDs, LAST_VIEWED, AND DELETED INTO MEMBERS TABLE
$stmt5 = $connection->prepare("INSERT INTO `conversations_members` (conversation_id, user_id, conversation_last_view, conversation_deleted)
                              VALUES (?, ?, ?, ?)");
$q = $stmt5->bind_param('iiii', $conversation_id, $recipient_id, $a, $b);
$stmt5->execute();
$stmt5->close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM