문제 설명
내 메시지 입력이 데이터베이스에 들어가지 않습니다. (My message input doesn't get into database)
친구에게 메시지를 보내고 싶지만 내 데이터베이스에 들어가고 싶지 않지만 $_POST를 사용해도 나중에 URL에 표시됩니다. 나는 나의 currentuserID와 나의 recipientID(내가 보내야 할 사람)를 얻습니다. 왜냐하면 나는 그것들을 모두 var_dump하고 두 ID를 모두 가져오기 때문입니다. 내 SQL 쿼리가 잘못되어서는 안 됩니다. 여러 사람과 확인했다고 생각했지만(하지만 데이터베이스에서 테스트했을 때 오류가 발생했습니다. 하지만 작동하는 다른 쿼리를 입력하면 동일한 오류가 발생하므로 ' 문제라고 생각합니다.).
여기서 무엇이 문제인지 모르겠습니다.
My message.php:
<?php
session_start();
if (empty($_SESSION['user_id'])) {
header('Location: login.php');
}
//Hier mag enkel het gesprek te zien zijn tussen 2 users die met elkaar bevriend zijn
//tabel buddies >buddyID1 & buddyID2
include_once(__DIR__."/inc/header.inc.php");
include_once(__DIR__."/classes/Message.php");
include_once(__DIR__."/classes/User.php");
$userArray = $_SESSION['user_id'];
$userID = implode(" ", $userArray);
$currentUser = $userID;
//var_dump($_POST);
$recipientID = implode(" ",$_POST);//om het getal terug te krijgen van de recipientID
echo "currentUser: ";
var_dump($currentUser);
echo ". recipientID: ";
var_dump($recipientID);
//msg wordt in databank gestopt
if(!empty($_POST['message'])){
$msg = new Message();
$msg‑>setUserID($currentUser);
$msg‑>setRecipientID($recipientID);
$msg‑>setMessage(htmlspecialchars($_POST['message']));
$msg‑>messageSchrijven();
}
//msg wordt afgedrukt/gereturned
$msg2 = new Message();
$messages = $msg2‑>messagePrint();
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF‑8">
<meta name="viewport" content="width=device‑width, initial‑scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<form action="" methd="post">
<h1>Je chat nu met <?php echo $recipientID ?></h1>
<input type="text" name="message">
<input type="hidden" name="recipientID" id="" value="<?php echo $recipientID?>">
<input type="hidden" name="senderID" id="" value="<?php echo $currentUser?>">
<div class="">
<button type="submit" class="btn" style="width: 90px">Send</button>
</div>
</form>
</div>
<?php foreach($messages as $message): ?>
<div>
<p><?php echo $message["senderID"].": " . $message["content"]; ?></p>
</div>
<?php endforeach;?>
</body>
</html>
my class Message .php:
<?php
include_once (__DIR__ . "/Db.php");
class Message{
private $userID;
private $message;
private $recipientID;
//getter setter userID
public function getUserID()
{
return $this‑>userID;
}
public function setUserID($userID)
{
$this‑>userID = $userID;
return $this;
}
// getter setter recipientID
public function getRecipientID()
{
return $this‑>recipientID;
}
public function setRecipientID($recipientID)
{
$this‑>recipientID = $recipientID;
return $this;
}
//getter setter message
public function getMessage()
{
return $this‑>message;
}
public function setMessage($message)
{
$this‑>message = $message;
return $this;
}
// om je berichtje in de DB te steken
public function messageSchrijven(){
$conn = Db::getConnection();
$statement = $conn‑>prepare("INSERT INTO msg(senderID,recipientID,content) values(:senderID,:recipientID,:content)");
$statement‑>bindValue(":senderID", $this‑>getUserID()); // huidige user
$statement‑>bindValue(":recipientID", $this‑>getRecipientID()); // de user naarwaar het verstuurd wordt
$statement‑>bindValue(":content", $this‑>getMessage()); // het bericht
$result = $statement‑>execute();
$result = $statement‑>fetch(PDO::FETCH_ASSOC);
return $result;
}
// berichtje afprinten
public function messagePrint(){ //$senderID,$recipientID
$conn = Db::getConnection();
$statement = $conn‑>prepare("SELECT * from msg where senderID = :senderID AND recipientID = :recipientID"); //where senderID = :senderID AND recipientID = :recipientID
$statement‑>bindValue(":senderID", $this‑>getUserID());
$statement‑>bindValue(":recipientID", $this‑>getRecipientID());
$result = $statement‑>execute();
$result = $statement‑>fetchAll(PDO::FETCH_ASSOC);
return $result;
}
}
내 "친구"가 나열된 다른 페이지에서 내 recipientID를 얻었습니다. 그 중 하나를 누르면 "message.php" 페이지로 그/그녀의 사용자 ID가 표시됩니다.
그래서 내 쿼리에 문제가 있는지 아니면 사용자 ID를 잃어버렸을 때 보내려고 합니다. 그러나
$msg‑>setUserID("9");
$msg‑>setRecipientID("10");
로 사용자 ID를 수동으로 설정하여 테스트를 시도했지만 작동하지 않습니다. (현재 btw에서 currentUserID = 9, recipientID = 10). 메시지 인쇄와 동일한 문제 .. 나는 그들을 이해하지 못합니다. 데이터베이스가 연결되어 문제 없이 로그인 및 등록할 수 있습니다.
(currentUserID = 9, 현재 btw에서 recipientID = 10). 메시지 인쇄와 동일한 문제 .. 나는 그들을 이해하지 못합니다. 데이터베이스가 연결되어 문제 없이 로그인 및 등록할 수 있습니다. (currentUserID = 9, 현재 btw에서 recipientID = 10). 메시지 인쇄와 동일한 문제 .. 나는 그들을 이해하지 못합니다. 데이터베이스가 연결되어 문제 없이 로그인 및 등록할 수 있습니다.참조 솔루션
방법 1:
IMPLODE
First of all I think you misunderstand the implode method. In your example the $recipientID
$recipientID = implode(" ",$_POST); //om het getal terug te krijgen van de recipientID
must contain something like "10 9 This is my Message" and that's no id. Add some verifications and change it to
$recipientID = $_POST['recipientID'];
You find some informations about implode on php.net
PDO EXECUTE
If you are using PDO, the execute method returns a boolean (search on php.net PDP execute). So if you don't use the return value, just use it like this
$statement‑>execute();
Additionally remove the $result = $statement‑>fetchAll(PDO::FETCH_ASSOC);
line at the insert method.
ERRORS
After the execute()
of PDO you can use
print_r($statement‑>errorInfo());
to get errors of your query. You can get more Informations here
DATABASE
We can't see your database schema, could it be that you offend against unique keys of your table or the type of field?
HTML
From my point I would always write space between variable name and php declaration like this
<input type="hidden" name="recipientID" value="<?php echo $recipientID; ?>" />
(by Tishier、UfguFugullu)