Please help with the code. There are 2 tables in MYSQL: messages

Imgur

and dialogs

Imgur

The script below should add the data about the sender by id and recipient, as well as a message and a couple of parameters, but problems with adding messages to the table.

<?php session_start(); include('config.php'); if(isset($_POST['send'])){ $result = mysql_query("SELECT * FROM `messages` WHERE `from_id` = '".$_SESSION['id']."' AND `to_id` = '".$_GET['mes']."' LIMIT 5",$mysql_connect) or die(mysql_error()); $count = mysql_num_rows($result); if($count=0) { ### mysql_query(" INSERT INTO `messages` (`from_id`,`to_id`) VALUES ('".$_SESSION['id']."','".$_GET['mes']."') ",$mysql_connect) or die("ERROR: ".mysql_error()); $result = mysql_query("SELECT * FROM `messages` WHERE `from_id` = '".$_SESSION['id']."' AND `to_id` = '".$_GET['mes']."' LIMIT 1",$mysql_connect) or die(mysql_error()); while($data = mysql_fetch_array($result)){ $dialog_id=$data['dialog_id']; } mysql_query(" INSERT INTO `dialogs` (`dialog_id`,`messages`,`date`) VALUES ('".$dialog_id."','".$_POST['messages']."','30.09.1923') ",$mysql_connect) or die("ERROR: ".mysql_error()); } else{ $result = mysql_query("SELECT * FROM `messages` WHERE `from_id` = '".$_SESSION['id']."' AND `to_id` = '".$_GET['mes']."' LIMIT 1",$mysql_connect) or die(mysql_error()); while($data = mysql_fetch_array($result)){ $dialog_id=$data['dialog_id']; } mysql_query(" INSERT INTO `dialogs` (`dialog_id`,`messages`,`date`) VALUES ('".$dialog_id."','".$_POST['messages']."','30.09.1923') ",$mysql_connect) or die("ERROR: ".mysql_error()); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Документ без названия</title> </head> <body> <form action="test.php" method="post"> <textarea name="messages" cols="" rows=""></textarea> <input type="submit" name="send" id="send" value="Отправить" /> </form> </body> </html> 

    1 answer 1

    1. A bunch of obsolete mysql_query is not good. For obsolete. Use PDO with its interesting feature, as pre-prepared requests are prepare.
    2. All can be sent in one request, google UNION.

    The code will be reduced 4 times.

    • At this stage it is difficult for me, I realized that the error was that I had $ _GET and $ _POST requests, but I don’t know how to fix it. Help me please. - golomazov
    • Is it possible to use get and post requests in the same handler? - golomazov
    • @golomazov only in theory. More precisely, you can get both GET and POST parameters in PHP at the same time, but in fact it’s just a huge red siren light bulb, which tells you that everything is bad. - etki
    • @golomazov nobody Ba does not prohibit the use of both GET and POST requests in the script. But, in order to avoid conflicts, it is worth using different functions for GET and POST requests. But it’s still not very good. I repeat, your code can be reduced by 3-4 times, using the same UNION, believe me, there is nothing complicated there. After all, with the SQL syntax you are already familiar, it means that you will understand it too. And yet decide on the method of data transfer and bring everything to one. Fewer problems come out. - Vitaly RS
    • I figured it out, I solved the problem. Thanks to all. - golomazov