<? //inbox.php
	$data = mysql_query("SELECT * FROM mailbox WHERE `index` <= $index_end and `index` > $index_start")
	or die(mysql_error());
	$info = mysql_fetch_array( $data );
		
	if($info['read'] == 0) {
		echo "<span class=\"tableStyle\">";
		$tabColor = "tableStyleNew";
	}
	else {
		$tabColor = "tableStyleOld";
	}
		//print table start
		echo "<table class=\"$tabColor\" bordercolor=\"#FFFFFF\" border=\"1\" cellpadding=\"0\" cellspacing=\"1\" align=\"center\"> <tr> <td width=\"109\">";
		echo $info['UserName'];
		echo "</td> <td width=\"462\">";

		//print subject and message preview
		$sub_msg = "<strong>" . $info['subject'] . "</strong> - " . $info['message'];
		if(!$info['attachment'] == NULL)
			echo "&nbsp;<img src=\"attachment.gif\" /> ";
		echo "<a href=\"view_message.php\"&nbsp;";
		echo "onclick=\" <?&nbsp;openmail(); ?> \"&nbsp;";
		echo ">" . substr($sub_msg,'',50) ."...</a>";
		//print value send
		
		//print table end
		echo "</td>  <td width=\"90\">";
		echo $info['time'];
		echo "</td>  </tr></table>";
?>


<?  //admin home page

	$index_start = 0;
	$index_end = 10;
	
	function increment() {
		echo $index_start = $index_start + 10;
		echo $index_end = $index_end + 10;
		//header("location:admin_home.php");
	}
	
	function decrement() {
		echo $index_start = $index_start - 10;
		echo $index_end = $index_end - 10;
		//header("location:admin_home.php");
	}
	
	function openmail() {
		//setcookie('1stmsg',$index_start);
		//return $_COOKIE['1stmsg'];
	}
	
	//$index_start = 0;					//1
	include ('inbox.php');
	
	$index_start = $index_start + 1;	//2
	include ('inbox.php');
	
	$index_start = $index_start + 1;	//3
	include ('inbox.php');
	
	$index_start = $index_start + 1;	//4
	include ('inbox.php');
	
	$index_start = $index_start + 1;	//5
	include ('inbox.php');
	
	$index_start = $index_start + 1;	//7
	include ('inbox.php');
	
	$index_start = $index_start + 1;	//8
	include ('inbox.php');
	
	$index_start = $index_start + 1;	//9
	include ('inbox.php');
	
	$index_start = $index_start + 1;	//10
	include ('inbox.php');

                setcookie('1stmsg',"2");
?>
<center><a href="#" onclick="<? decrement(); ?>"> Previous 10</a>  | <a href="#" onclick="<? increment(); ?>">Next 10</a></center>

<?
	include('db_conexn.php');
	/*if(isset($_COOKIE['userName']) {
		echo " cookie set ";
	}*/
	$index = $_COOKIE['1stmsg'];
	$data = mysql_query("SELECT * FROM mailbox WHERE `index` = $index ")
	or die(mysql_error()); 
	$info = mysql_fetch_array( $data );

echo $info['UserName']; 
echo $info['time']; 
echo $info['message']; 
$mail=mysql_query("UPDATE mailbox SET `read` = 1 WHERE `index` = $index");
?>

check it (admin)
user : admin
Pass : 123

client side
user : dan
pass : 123

Dani AI

Generated

As pointed out, the cleanest way to know which row was clicked is to give each row a stable identifier and send that id to the viewer script. Rather than repeating include() calls with manual index arithmetic and using cookies as the primary selector, generate the inbox in one loop and attach the row id to the clickable element. Also note ’s warning about publishing passwords — remove test credentials from public pages.

Example: build the listing with a safe DB API (PDO shown) and output a link that carries the primary key.

$pdo = new PDO($dsn, $user, $pass);
$stmt = $pdo->prepare('SELECT id, UserName, subject, message, attachment, time, `read` FROM mailbox LIMIT :offset, :count');
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->bindValue(':count', $count, PDO::PARAM_INT);
$stmt->execute();
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) {
    $preview = htmlspecialchars(substr($r['subject'].' - '.$r['message'],0,50));
    echo '<a href="message_view.php?mid='.$r['id'].'">'.$preview.'…</a>';
}

Server-side handling must validate the incoming id, use prepared statements, and mark the message read only after loading it:

$mid = isset($_GET['mid']) ? (int)$_GET['mid'] : 0;
if ($mid <= 0) { /* bad request */ }
$stmt = $pdo->prepare('SELECT * FROM mailbox WHERE id = :id');
$stmt->execute(['id' => $mid]);
$msg = $stmt->fetch(PDO::FETCH_ASSOC);
if ($msg) {
    $upd = $pdo->prepare('UPDATE mailbox SET `read`=1 WHERE id = :id');
    $upd->execute(['id' => $mid]);
}

Troubleshooting notes: the "headers already sent" warning means headers/cookies/session_start() were done after output. Put any session_start() or setcookie()/header() calls before echoing HTML; obstart() can mask the symptom but reorganizing code is better. Also migrate away from deprecated mysql* functions to PDO or mysqli and always escape output with htmlspecialchars to prevent XSS. See the PHP docs for PDO and header() for implementation details: PDO manual and header() manual.

Recommended Answers

All 4 Replies

I would not suggest you publish your password online. Also, can you be be more specific about your problem?

i want to select only one row
in that i want to know which row is clicked!

i saw this error on your admin.php page..

Warning: Cannot modify header information - headers already sent by (output started at /home/peipians/public_html/yackub/admin_home.php:23) in /home/peipians/public_html/yackub/admin_home.php on line 131

For this you simply add ob_start(); at stating lines of your php code...

And coming to your thread ,you can pass your primary key of your message on hyper link like:

<a href="some.php?viewid=code for id" >code for showing message</a>

i saw this error on your admin.php page..

Warning: Cannot modify header information - headers already sent by (output started at /home/peipians/public_html/yackub/admin_home.php:23) in /home/peipians/public_html/yackub/admin_home.php on line 131

For this you simply add ob_start(); at stating lines of your php code...

And coming to your thread ,you can pass your primary key of your message on hyper link like:

<a href="some.php?viewid=code for id" >code for showing message</a>

Thanks Shanti Chepuru,
Both of your sugestions worked correctly...
thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.