问题
I am trying to create a private messaging system in which user sends message to another user and that content is inserted into database..Iam using a random number called hash to identify a conversation between two people..table for that is "message_group" and table for saving messages is "messages"..here comes the problem..when I type something in text area it is supposed to enter into database table messages but it doesnot..This code is supposed to display the messages and also reply to a message by typing in textarea..someone please help me..This is the final error in my project..I will get A grade if this succeeds..Thanks
here's the code
<html>
<head>
<title>convo</title>
</head>
<body>
<?php include 'connect.php';?>
<?php include 'message_title_bar.php';?>
<?php include 'functions.php';?>
<?php include 'title_bar.php'; ?>
<?php
$my_id = $_SESSION['user_id'];
?>
<br />
<div>
<?php
if(isset($_GET['hash']) && !empty($_GET['hash'])) {
$hash=$_GET['hash'];
$connect = mysqli_connect('localhost','root','','php_mysql_login_system');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$message_query=mysqli_query($connect,"SELECT from_id,message FROM messages WHERE group_hash='$hash'") or die(mysqli_error($connect));
while($run_message=mysqli_fetch_array($message_query)){
$from_id=$run_message['from_id'];
$message=$run_message['message'];
$connect = mysqli_connect('localhost','root','','php_mysql_login_system');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$user_query=mysqli_query($connect,"SELECT username FROM users WHERE id='$from_id'") or die(mysqli_error($connect));
$run_user=mysqli_fetch_array($user_query);
$from_username=$run_user['username'];
echo "
<p>
<b>$from_username</b>
<br />$message
</p>";
}
?>
<form method='post'>
<?php
if(isset($POST['message']) && !empty($_POST['message'])){
$new_message=$_POST['message'];
mysql_query("INSERT INTO messages VALUES('','$hash','my_id','new_message')");
header('location:conversations.php?hash='.$hash);
}
?>
Enter message:
<br />
<textarea name='message' rows='7' cols='60'></textarea>
<br />
<br />
<input type='submit' value="send message" />
</form>
<?php
}else {
echo"
<b>select conversation</b>";
$connect = mysqli_connect('localhost','root','','php_mysql_login_system') or die(mysqli_error($connect));
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query_string="SELECT `hash`,`user_one`,`user_two` FROM message_group WHERE user_one='$my_id' OR user_two='$my_id'";
$get_con=mysqli_query($connect,$query_string) or die(mysqli_error($connect));
while($run_con = mysqli_fetch_array($get_con))
{ $hash=$run_con['hash'];
$user_one=$run_con['user_one'];
$user_two=$run_con['user_two'];
if($user_one==$my_id){
$select_id = $user_two;
}else{
$select_id = $user_one;
}
$connect = mysqli_connect('localhost','root','','php_mysql_login_system');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT username FROM users WHERE id='$select_id'";
$user_get=mysqli_query($connect,$query) or die(mysqli_error($connect));
$run_user=mysqli_fetch_array($user_get);
$select_username = $run_user['username'];
echo "
<p>
<a href='conversations.php?hash=$hash'>$select_username</a>
</p>";
}
}
?>
</div>
</body>
I strongly somehow feel that the error is in this part of the above code
Note: Below code is a section of above code
?>
<br />
<form method='post'>
<?php
if(isset($POST['message']) && !empty($_POST['message'])){
$new_message=$_POST['message'];
$connect = mysqli_connect('localhost','root','','php_mysql_login_system');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($connect,"INSERT INTO messages VALUES('','$hash','my_id','new_message')");
header('location:conversations.php?hash='.$hash);}
?>Enter message: <br />
<textarea name='message' rows='6' cols='50'></textarea>
<br /><br />
<input type='submit' value="send message" />
</form>
<?php
回答1:
Try this:
<html>
<head><title>convo</title></head>
<body>
<div>
<?php include 'connect.php'; ?>
<?php include 'message_title_bar.php'; ?>
<?php include 'functions.php'; ?>
<?php include 'title_bar.php'; ?>
<?php
$my_id = $_SESSION['user_id'];
$connect = mysqli_connect('localhost', 'root', '', 'php_mysql_login_system');
if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
if (isset($POST['message']) && !empty($_POST['message']) && isset($_SESSION['hash']) && !empty($_SESSION['hash'])) {
$hash = $_SESSION['hash'];
$new_message = $_POST['message'];
mysqli_query($connect, "INSERT INTO messages VALUES('', '$hash', '$my_id', '$new_message')");
header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . "/conversations.php?hash=".$hash);
exit;
}
if (isset($_GET['hash']) && !empty($_GET['hash'])) {
$_SESSION['hash'] = $_GET['hash'];
$hash = $_SESSION['hash'];
$message_query = mysqli_query($connect, "SELECT from_id, message FROM messages WHERE group_hash='$hash'");
while ($run_message = mysqli_fetch_array($message_query)) {
$from_id = $run_message['from_id'];
$message = $run_message['message'];
$run_user = mysqli_fetch_array(mysqli_query($connect, "SELECT username FROM users WHERE id='$from_id'"));
$from_username = $run_user['username'];
echo "<p><b>$from_username</b><br />$message</p>";
}
?>
<form method='post'>
<label for="message">Enter message:</label>
<textarea name='message' id="message" rows='7' cols='60'></textarea>
<br/><br/>
<input type='submit' value="send message"/>
</form>
<?php
} else {
echo "<b>select conversation</b>";
$query_string = "SELECT `hash`, `user_one`, `user_two` FROM message_group WHERE user_one='$my_id' OR user_two='$my_id'";
$get_con = mysqli_query($connect, $query_string);
while ($run_con = mysqli_fetch_array($get_con)) {
$hash = $run_con['hash'];
$user_one = $run_con['user_one'];
$user_two = $run_con['user_two'];
if ($user_one == $my_id) {
$select_id = $user_two;
} else {
$select_id = $user_one;
}
$query = "SELECT username FROM users WHERE id='$select_id'";
$run_user = mysqli_fetch_array(mysqli_query($connect, $query));
$select_username = $run_user['username'];
echo "<p><a href='?hash=".$hash."'>$select_username</a></p>";
}
}
?>
来源:https://stackoverflow.com/questions/31093413/data-entered-in-text-area-wont-enter-into-database-designed-using-php-and-mysqli