Next code is looking for characters in an string from "note" column in all rows in a data table. Once it gets row "id", then it should update "did_read" column to value "1" in the same row. But no success. What corrections must I do to make it work?
<?php
if (isset($_POST['str'])){
$strid =$_POST['str'];
$sql="SELECT id FROM notifications WHERE note CONTEINS='$strid' ";
$query = mysqli_query($db_conx, $sql);
$statusnumrows = mysqli_num_rows($query);
if($statusnumrows> 0){
while ( $row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$statusid = $row["id"];
$sql = "UPDATE notifications SET did_read='1' WHERE id='$statusid'";
$query = mysqli_query($db_conx, $sql);
echo 'did_read_ok';
exit;
}
}
}
?>
Looking now I think that I should be much easier use next. But also is not working.
<?php
if (isset($_POST['str'])){
$strid= mysqli_real_escape_string($db_conx, $_POST['str']);
$sql = "UPDATE notifications SET did_read='1' WHERE note LIKE='$strid' ";
$query= mysqli_query($db_conx, $sql);
echo 'did_read_ok';
exit;
}
?>
I have made some conclusions with your help down in comments. The First part of code is working if I do uncomment it. But then I can't understand why other code in the condition loop is not working as I am getting "string(10)'status_218'" and "did_read_ok" ? I am sending 'str' variable with ajax.send().
<?php
//$strid= 'status_218';
// $sql = "UPDATE notifications SET did_read='1' WHERE note LIKE '%$strid%'";// not working:LIKE CONCAT('%', $strid, '%')
// $query= mysqli_query($db_conx, $sql);
if (isset($_POST['str'])){
$strid= $_POST['str'];
var_dump($strid);//I get: string(10)status_218
$sql = "UPDATE notifications SET did_read='1' WHERE note LIKE='%$strid%'";
$query= mysqli_query($db_conx, $sql);
echo 'did_read_ok';
exit;
}
?>