• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 52
  • Last Modified:

Adding "mysqli_query" to an "href"

In next line:
$notification_list .= "<p><a href='user.php?u=$initiator'>$initiator</a> | $app<br />$note</p>";
I have a link: "href=....."

I need next code:
1.mysqli_query($db_conx, "UPDATE notifications SET did_read='1' WHERE username='$log_username' LIMIT 1");
to be executed when clicked in that link. How can it be  done?

So i might then display only notifications that have not yet been read:
2.$sql = "SELECT * FROM notifications WHERE username LIKE BINARY '$log_username' AND did_read='0' ORDER BY date_time DESC";

Below is all code.
Pleas check also 1 and  2 if you see some mistakes?

<?php
include_once("../php_includes/check_login_status.php");
// If the page requestor is not logged in, usher them away
//echo $log_username;
if($user_ok != true || $log_username == ""){
	header("location: http://www.xcv.com");
    exit();
}

$notification_list = "";
$sql = "SELECT * FROM notifications WHERE username LIKE BINARY '$log_username' ORDER BY date_time DESC";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows < 1){
	$notification_list = "You do not have any notifications";
} else {
	while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
		$noteid = $row["id"];
		$initiator = $row["initiator"];
		$app = $row["app"];
		$note = $row["note"];
		$date_time = $row["date_time"];
		$notification_list .= "<p><a href='user.php?u=$initiator'>$initiator</a> | $app<br />$note</p>";
	}
}

mysqli_query($db_conx, "UPDATE users SET notescheck=now() WHERE username='$log_username' LIMIT 1");

?>

Open in new window

0
Ivan Golubar
Asked:
Ivan Golubar
  • 7
  • 6
  • 3
2 Solutions
 
mohan singhWeb developerCommented:
can you show screenshot how it is display  
for batter understand your question

Thank you
Regards
Mohan Singh
0
 
Ivan GolubarAuthor Commented:
0
 
mohan singhWeb developerCommented:
your code is correct  

post your all data which you want to update  an ajax page
than execute Your update  query
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Ivan GolubarAuthor Commented:
I did this part of code :
function updateDid_read(setDid_read){

	var ajax = ajaxObj("POST", "notification.php");
	ajax.onreadystatechange = function() {
		
	}
	ajax.send("setDid_read="+ 1);
}

Open in new window




on base of:
function partnerReqHandler(action,reqid,user1,elem){
	var conf = confirm("Press OK to '"+action+"' this partner request.");
	if(conf != true){
		return false;
	}
	_(elem).innerHTML = "processing ...";
	var ajax = ajaxObj("POST", "../php_parsers/partner_system.php");
	ajax.onreadystatechange = function() {
		if(ajaxReturn(ajax) == true) {
			if(ajax.responseText == "accept_ok"){
				_(elem).innerHTML = "<b>Request Accepted!</b><br />Your are now partners";
			} else if(ajax.responseText == "reject_ok"){
				_(elem).innerHTML = "<b>Request Rejected</b><br />You chose to reject partnership with this user";
			} else {
				_(elem).innerHTML = ajax.responseText;
			}
		}
	}
	ajax.send("action="+action+"&reqid="+reqid+"&user1="+user1);
}

Open in new window


To make next code to execute?
But i am not sure if it is correct.
<?php
mysqli_query($db_conx, "UPDATE notifications SET did_read='1' WHERE username='$log_username' LIMIT 1");
?>
And next:
$notification_list .= "<p><a href='user.php?u=$initiator' onclick="updateDid_read(\'1\')">$initiator</a> | $app<br />$note</p>";

Open in new window

on base of:
$partner_requests .= '<button onclick="partnerReqHandler(\'reject\',\''.$reqID.'\',\''.$user1.'\',\'user_info_'.$reqID.'\')">reject</button>';

Open in new window


But i get next error for last line of code:
syntax error, unexpected T_string
0
 
Vijaya KumarCommented:
u can do by self processing page please clarify one thing

the code below

<?php
include_once("../php_includes/check_login_status.php");
// If the page requestor is not logged in, usher them away
//echo $log_username;
if($user_ok != true || $log_username == ""){
	header("location: http://www.xcv.com");
    exit();
}

$notification_list = "";
$sql = "SELECT * FROM notifications WHERE username LIKE BINARY '$log_username' ORDER BY date_time DESC";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows < 1){
	$notification_list = "You do not have any notifications";
} else {
	while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
		$noteid = $row["id"];
		$initiator = $row["initiator"];
		$app = $row["app"];
		$note = $row["note"];
		$date_time = $row["date_time"];
		$notification_list .= "<p><a href='user.php?u=$initiator'>$initiator</a> | $app<br />$note</p>";
	}
}

mysqli_query($db_conx, "UPDATE users SET notescheck=now() WHERE username='$log_username' LIMIT 1");

?>

Open in new window



Comes under user.php or someother name if it user.php then we will check isset otherwise we do ajax
clarify this i will make code for u
1
 
Ivan GolubarAuthor Commented:
It is notifications.php.
0
 
Vijaya KumarCommented:
let me do update u soon
0
 
mohan singhWeb developerCommented:
Dear ivan golubar
You should check the line on which the error occurs. You are missing a closing double quote on this line:

and please echo your query I think there is some error ..
0
 
Vijaya KumarCommented:
First of all u need to change the update query from

UPDATE notifications SET did_read='1' WHERE username='$log_username' LIMIT 1

to

UPDATE notifications SET did_read='1' WHERE id='$id' LIMIT 1

because it set did_read note for all notes belongs to the particular username
0
 
Vijaya KumarCommented:
Second thing....

SELECT * FROM notifications WHERE username LIKE BINARY '$log_username' and did_read =0 ORDER BY date_time DESC"

it will skip already readed note
0
 
Vijaya KumarCommented:
Finally i hot coded html u can change to sql what i comment /* */

it has sample only

notification.php
----------------------

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
</head>
<body>
<div id="notifications">
<?php
//include_once("../php_includes/check_login_status.php");
$user_ok = true;
$log_username = "abc";
if($user_ok != true || $log_username == ""){
	header("location: http://www.xcv.com");
    exit();
}

/* $notification_list = "";
$sql = "SELECT * FROM notifications WHERE username LIKE BINARY '$log_username' ORDER BY date_time DESC";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows < 1){
	$notification_list = "You do not have any notifications";
} else {
	while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
		$noteid = $row["id"];
		$initiator = $row["initiator"];
		$app = $row["app"];
		$note = $row["note"];
		$date_time = $row["date_time"];
		$notification_list .= "<p><a href='user.php?u=$initiator'>$initiator</a> | $app<br />$note</p>";
	}
}

mysqli_query($db_conx, "UPDATE users SET notescheck=now() WHERE username='$log_username' LIMIT 1"); */
echo "<p><a class='link' id='1'>link1</a> | app<br />note</p>";
echo "<p><a class='link' id='2'>iink2</a> | app1<br />note1</p>"	
?>
</div>
<script>
$(document).ready(function(){
	$(document).on('click','.link',function(){
		console.log($(this)[0].id);
		 request = $.ajax({
						type: 'POST',
						url: "user.php",
						data: {"id" : $(this)[0].id },
						});
					request.done(function (response){					
						$('#notifications').html(response);
					});
				
	});
});
</script>
</body>

Open in new window




<?php
//include_once("../php_includes/check_login_status.php");
/*$id=$_REQUEST["id"];
$sql = "UPDATE notifications SET did_read='1' WHERE username='$log_username' LIMIT 1";
$query = mysqli_query($db_conx, $sql); */
/* $notification_list = "";
$sql = "SELECT * FROM notifications WHERE username LIKE BINARY '$log_username' ORDER BY date_time DESC";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows < 1){
	$notification_list = "You do not have any notifications";
} else {
	while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
		$noteid = $row["id"];
		$initiator = $row["initiator"];
		$app = $row["app"];
		$note = $row["note"];
		$date_time = $row["date_time"];
		$notification_list .= "<p><a href='user.php?u=$initiator'>$initiator</a> | $app<br />$note</p>";
	}
}

mysqli_query($db_conx, "UPDATE users SET notescheck=now() WHERE username='$log_username' LIMIT 1"); 

echo $notification_list;

*/

echo "<p><a class='link' id='2'>iink2</a> | app1<br />note1</p>";	

?>

Open in new window

0
 
Vijaya KumarCommented:
@ivan golubar did my code works??
0
 
Ivan GolubarAuthor Commented:
I did underline part of code which are new.

$notification_list = "";
$sql = "SELECT * FROM notifications WHERE username LIKE BINARY '$log_username' AND did_read='0' ORDER BY date_time DESC";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows < 1){
      $notification_list = "You do not have any notifications";
} else {
      while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
            $noteid = $row["id"];
            $initiator = $row["initiator"];
            $app = $row["app"];
            $note = $row["note"];
            $date_time = $row["date_time"];
            $date_time = strftime("%b %d, %Y", strtotime($date_time));
            //$notification_list .= "<p><a href='user.php?u=$initiator'>$initiator</a> | $app<br />$note</p>";
            $notification_list .= "<p><a href='user.php?u=$initiator' onclick='updateDid_read('1')'>$initiator</a> | $app<br />$note</p>";
      }
}

And next is all new.
Can You help me with this pleas?
<script>
function updateDid_read(setDid_read){
	var ajax = ajaxObj("POST", "notification.php");
	ajax.onreadystatechange = function() {		
	}
	ajax.send("setDid_read");
}
</script>

Open in new window

0
 
Ivan GolubarAuthor Commented:
I did change my ajax to:
function updateDid_read(){
$.ajax({
  method:"POST",
  url: '/wp-content/themes/net4/ntF4/notification.php',
  data:  {
      "ifUpdateDid_read":1
   },
   datatype: "text",
  success: function(strdate){
           console.log("111");
         
     },
     error: function(error, txtStatus) {
      console.log(txtStatus);
      console.log('error');
    }
  });
}

Open in new window


And:
<?php 
   if (isset($_POST["ifUpdateDid_read"]) ) {  
     $sql = "UPDATE notifications SET did_read='1' WHERE username='$log_username' LIMIT 1";
     $query = mysqli_query($db_conx, $sql);
       }
?>

Open in new window

0
 
Ivan GolubarAuthor Commented:
I did add "$noteid" because of :$noteid = $row["id"];
$notification_list .= "<p><a href='user.php?u=$initiator' onclick='updateDid_read($noteid)'>$initiator</a> | $app<br />$note</p>";

Open in new window


and in php:
<?php 
   if (isset($_POST["ifUpdateDid_read"]) ) { 
        $rowID= mysqli_real_escape_string($db_conx, $_POST['rowID']);
     $sql = "UPDATE notifications SET did_read='1' WHERE username='$log_username'AND id='$rowID' LIMIT 1";
     $query = mysqli_query($db_conx, $sql);
       }
?>

Open in new window


then in ajax:
 data:  {
      "ifUpdateDid_read":1,
      "rowID":rowID
   },

Now I need help to debug it.
0
 
Ivan GolubarAuthor Commented:
All together is  as next:
$notification_list = "";
$sql = "SELECT * FROM notifications WHERE username LIKE BINARY '$log_username' AND did_read='0' ORDER BY date_time DESC";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows < 1){
	$notification_list = "You do not have any notifications";
} else {
	while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
		$noteid = $row["id"];
		$initiator = $row["initiator"];
		$app = $row["app"];
		$note = $row["note"];
		$date_time = $row["date_time"];
		$date_time = strftime("%b %d, %Y", strtotime($date_time));
		//$notification_list .= "<p><a href='user.php?u=$initiator'>$initiator</a> | $app<br />$note</p>";
		$notification_list .= "<p><a href='user.php?u=$initiator' onclick='updateDid_read($noteid)'>$initiator</a> | $app<br />$note</p>";
	}
}

mysqli_query($db_conx, "UPDATE users SET notescheck=now() WHERE username='$log_username' LIMIT 1");
?>
<?php 
   if (isset($_POST["ifUpdateDid_read"]) ) { 
        $rowID= mysqli_real_escape_string($db_conx, $_POST['rowID']);
     $sql = "UPDATE notifications SET did_read='1' WHERE username='$log_username'AND id='$rowID' LIMIT 1";
     $query = mysqli_query($db_conx, $sql);
       }
?>

<script>
function updateDid_read(rowID){
$.ajax({
  method:"POST",
  url: '/wp-content/themes/net4/netF4/notification.php',
  data:  {
      "ifUpdateDid_read":1,
      "rowID":rowID
   },
   datatype: "text",
  success: function(strdate){
           console.log("111");
         
     },
     error: function(error, txtStatus) {
      console.log(txtStatus);
      console.log('error');
    }
  });
}
</script>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 7
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now