Click here to Skip to main content
15,922,407 members
Home / Discussions / Linux, Apache, MySQL, PHP
   

Linux, Apache, MySQL, PHP

 
PinnedForum Guidelines - PLEASE READ PinPopular
Chris Maunder28-Jul-09 2:39
cofounderChris Maunder28-Jul-09 2:39 
Questionexample paypal payments Pin
vic5321-Mar-24 10:18
professionalvic5321-Mar-24 10:18 
Questionphp Pin
Dancane Odiwuor18-Dec-23 3:49
Dancane Odiwuor18-Dec-23 3:49 
AnswerRe: php Pin
Richard MacCutchan18-Dec-23 5:01
mveRichard MacCutchan18-Dec-23 5:01 
QuestionPHP 8.2 - Unsupported operand types: string * int Pin
Aruna KN13-Jun-23 19:48
Aruna KN13-Jun-23 19:48 
AnswerRe: PHP 8.2 - Unsupported operand types: string * int Pin
Richard MacCutchan13-Jun-23 21:10
mveRichard MacCutchan13-Jun-23 21:10 
AnswerRe: PHP 8.2 - Unsupported operand types: string * int Pin
Member 102471327-Mar-24 23:38
Member 102471327-Mar-24 23:38 
QuestionChoosing an editor Pin
jpaxtons9-Jun-23 9:48
jpaxtons9-Jun-23 9:48 
AnswerRe: Choosing an editor Pin
Deepak Vasudevan23-Aug-23 1:57
Deepak Vasudevan23-Aug-23 1:57 
QuestionFatal error: Uncaught Error: Call to a member function prepare() on null in Pin
Nicolas Veloso25-Apr-23 9:47
Nicolas Veloso25-Apr-23 9:47 
AnswerRe: Fatal error: Uncaught Error: Call to a member function prepare() on null in Pin
Richard MacCutchan25-Apr-23 9:58
mveRichard MacCutchan25-Apr-23 9:58 
Question.htaccess Query String Redirect Pin
Aruna KN20-Apr-23 5:05
Aruna KN20-Apr-23 5:05 
QuestionPHP Warning: Attempt to read property "post_content" on null Pin
Aruna KN17-Apr-23 23:09
Aruna KN17-Apr-23 23:09 
AnswerRe: PHP Warning: Attempt to read property "post_content" on null Pin
Richard MacCutchan17-Apr-23 23:59
mveRichard MacCutchan17-Apr-23 23:59 
QuestionProper parameterised queries for database access Pin
Aruna KN11-Apr-23 6:35
Aruna KN11-Apr-23 6:35 
AnswerRe: Proper parameterised queries for database access Pin
Afzaal Ahmad Zeeshan11-Apr-23 10:11
professionalAfzaal Ahmad Zeeshan11-Apr-23 10:11 
GeneralRe: Proper parameterised queries for database access Pin
Aruna KN11-Apr-23 18:25
Aruna KN11-Apr-23 18:25 
After your supportive reply, I rewrote the code after studying the Manual, but it gives an error_log:

[12-Apr-2023 04:15:21 UTC] PHP Fatal error:  Uncaught TypeError: mysqli::prepare(): Argument #1 ($query) must be of type string, mysqli_stmt given in /home/student/public_html/friendsphp8/message.php:8
Stack trace:
#0 /home/student/public_html/friendsphp8/message.php(8): mysqli->prepare(Object(mysqli_stmt))
#1 {main}
  thrown in /home/student/public_html/friendsphp8/message.php on line 8


My code is as follows:
Please suggest me corrections Smile | :)

PHP
<?php 
        if(isset($_GET['id']) && $_GET['id'] != "")
		{
			 
			 $query = $conn->prepare("SELECT * FROM ".C_MYSQL_MESSAGES." WHERE (sender_id =? AND receiver_id=?) OR (sender_id =? AND receiver_id=?) ORDER BY `date_added` ASC");
			 $stmt = $conn->prepare($query);
			 $stmt->bind_param("ssss", $_SESSION['m'],(int)$_GET['id'],(int)$_GET['id'],$_SESSION['m']);
			 $stmt->execute();
			 $result = $stmt->get_result();
			 
		     while($j = mysqli_fetch_array($result))
			{
				$c = mysqli_query($conn,"UPDATE ".C_MYSQL_MESSAGES." SET status=1 WHERE id=".$j['id']);
				
				$class = "";
				$date_class = "";
				if($j['sender_id'] == $_SESSION['m'])
				{
					$class = "right";
					$date_class = "date_left";
				}
				else
				{
					$class = "left";
					$date_class = "date_right";
				}
					
				echo '<p class="msgtext '.$class.'">'.$j['date_added'].''.$j['message'].'</p>';	
			}
		}
   
  ?>


OLD CODE IS AS FOLLOWS:

PHP
<?php 
        if(isset($_GET['id']) && $_GET['id'] != "")
		{
			 $query = "SELECT * FROM ".C_MYSQL_MESSAGES." WHERE (sender_id =".$_SESSION['m']." AND receiver_id=". (int)$_GET['id'].") OR (sender_id =".(int)$_GET['id']." AND receiver_id=".$_SESSION['m'].") ORDER BY `date_added` ASC";
			 $result = mysqli_query($conn,$query) or die();
			 
		     while($j = mysqli_fetch_array($result))
			{
				$c = mysqli_query($conn,"UPDATE ".C_MYSQL_MESSAGES." SET status=1 WHERE id=".$j['id']);
				
				$class = "";
				$date_class = "";
				if($j['sender_id'] == $_SESSION['m'])
				{
					$class = "right";
					$date_class = "date_left";
				}
				else
				{
					$class = "left";
					$date_class = "date_right";
				}
					
				echo '<p class="msgtext '.$class.'">'.$j['date_added'].''.$j['message'].'</p>';	
			}
		}
   
  ?>

GeneralRe: Proper parameterised queries for database access Pin
Richard MacCutchan11-Apr-23 21:52
mveRichard MacCutchan11-Apr-23 21:52 
GeneralRe: Proper parameterised queries for database access Pin
Aruna KN11-Apr-23 22:13
Aruna KN11-Apr-23 22:13 
GeneralRe: Proper parameterised queries for database access Pin
Richard MacCutchan11-Apr-23 22:29
mveRichard MacCutchan11-Apr-23 22:29 
GeneralRe: Proper parameterised queries for database access Pin
Aruna KN12-Apr-23 1:34
Aruna KN12-Apr-23 1:34 
QuestionPHP Fatal error: Uncaught mysqli_sql_exception Pin
Aruna KN10-Apr-23 4:47
Aruna KN10-Apr-23 4:47 
AnswerRe: PHP Fatal error: Uncaught mysqli_sql_exception Pin
Richard MacCutchan10-Apr-23 6:04
mveRichard MacCutchan10-Apr-23 6:04 
GeneralRe: PHP Fatal error: Uncaught mysqli_sql_exception Pin
Aruna KN10-Apr-23 6:41
Aruna KN10-Apr-23 6:41 
GeneralRe: PHP Fatal error: Uncaught mysqli_sql_exception Pin
Richard MacCutchan10-Apr-23 6:56
mveRichard MacCutchan10-Apr-23 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.