I create some site and now I create a message system. For it I create mysql inbox_msg base (from,to,subject,date,time,message,file1,file2,file3,unread).
And now I don’t know, how create PHP-script to print it’s message’s.
Now it looks like this:
<?
session_start();
$user = $_SESSION['login'];
$mysql_connect = mysql_connect("localhost", "root", "");
$mysql_select_db = mysql_select_db("site");
$sql = "SELECT * FROM `msg_inbox` WHERE `to` = '$user'";
$result = mysql_query($sql) or die(mysql_error());
$sql1 = "
UPDATE `msg_inbox`
SET `unread` = 0
WHERE `to` = '" . mysql_real_escape_string($user) . "'
";
$result1 = mysql_query($sql1) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
<br>
From: <?= $row['from'] ?>
<br>
Date: <?= $row['date'] ?> at <?= $row['time'] ?>
<br>
<br>
<?= $row['message'] ?>
<br>
<HR>
}
?>
But it’s no good: I have a problem to add delete button and other. Can you help me?
Thank you!
To begin with, the code inside the while loop is not valid PHP so the script will definitely not run as posted. What you need to do is either mark that code as non-PHP (i.e. HTML markup by closing the PHP script tag after the while loop and reopening before the end of the while) or use an output mechanism such as the
echofunction in PHP.Here’s how you could rewrite the while loop using echo:
As far as the delete button, you could add that to your page as an HTML input element of type submit, and place it inside an HTML
formelement. When the user clicks the button then, the form will do perform a an HTTP POST, or GET (depending on themethodattribute you put on theformelement). The page it POSTs or GETs is controlled by yet another attribute of theformelement, namedaction.Here’s an example:
In the example above, when the user clicks the button that says “Delete Message” the browser will perform a POST HTTP request to the delete_message_script.php script.
You may wish to read more about HTML form elements, and how a form works (i.e. difference between a POST and a GET request, request variables, and the different types of input elements; also, read about how PHP allows you to process HTTP request variables). In the PHP script that the form posts to you may need to track which button the user clicked to get there (if there is more than one) and which message needs to be deleted.
Finally, once you get the idea, read about form post-back. That is the mechanism of posting back to the same page (script).