我正在做的是获取url id来分割聊天室内容,但现在我有错误
这是我的
chatMessage.php
include '../config.php';
include 'login.php';
$username = $_SESSION['id'];
$chatroomID =$_GET["chatroomID"];
$sql="SELECT * FROM chatroom_chat where chatroom_id ='$chatroomID'";
while ($row = mysqli_fetch_array($sql)) {
$chat = $row['chat_ID'];
$getChatData = "SELECT * FROM chat where id = $chat";
}
$sql1= "SELECT * FROM (
SELECT * FROM chat ORDER BY id DESC LIMIT 0,40
) sub
ORDER BY id ASC ";
$result = mysqli_query($connection, $sql1);
while($extract = mysqli_fetch_array($result)){
$color = ($extract['id'] == $username) ? '#FFFFFF' : '#66FFFF';
$position = ($extract['id'] == $username) ? 'right' : 'left';
$border = ($extract['id'] == $username) ? ' 1px solid black ' : ' none ';
echo "<div class='msg-dateandtime' style='text-align:$position; float:$position;'> <div class='left-username' style='color:blue;'>" . $extract['id'] ."</div>"
. "<div class='space'></div>"
. "<div class='right-date'> ". $extract['date'] ." </div></div>"
. "<div class='wrap-message' style='background-color:$color; border:$border; float:$position;'>"
. "<p style 'text-align=$position; margin:0; padding:0; text-align:left;'> ".$extract['chat']."</p></div>";
}
我不知道哪里出错了
答案 0 :(得分:1)
请使用如下:
$chatroomID = isset($_GET["chatroomID"]) ? $_GET["chatroomID"] : '';
原因:当您为变量$ chatroomID分配值并且GET请求没有 chatroomID 索引可用时,会出现错误。在分配给变量之前,您应首先检查此索引上是否有值。
警告:防止在SQL查询中直接使用用户输入变量。而是使用准备好的陈述。