PHP MySqli Delete error message - php

This code does delete a record from the table correctly but the second time run on the same table it still reports "Record deleted successfully". Why is that?
// sql to delet a record
$sql = "DELETE FROM setidata WHERE SETIData_ID=834";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
echo "<br> Finished";
?>

Because mysqli_query returns true not matter if the record exists or not.
use mysqli_affected_rows(); to see how many rows have been deleted.
if(mysqli_affected_rows($conn)==0){
print "Nothing done";
}

Because mysqli_query($conn, $sql) is returning true since since there were no errors triggered by the query or in the function mysqli_query(). If you want to do something redundant, mysqli_query() will not stop you.

As per your code If query is succesful then it will echo as opted. You can run this query in phpmyadmin->SQL. First time it will show deleted rows 1. Next time it will show 0.

Related

Record not getting updated in PHP/MySQL

I am trying to change some values in a table but for some reason the values are not getting updated. Here is the php :
$title=mysqli_escape_string($conn,$_POST['title']);
echo $title."<br>"; //Test
$note=mysqli_escape_string($conn,$_POST['jnote']);
echo $note."<br>"; //Test
$query="UPDATE articles SET return_j='1' AND j_note='$note' WHERE article_name='$title'";
if ($conn->query($query) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
Problem is I am getting no error. 'Record updated successfully' is what I get. So the query is being executed successfully but the table is not getting updated. I don't understand why.
When I am updating other columns of the table (in some other scripts), I am having no problem.
Swap out the AND for a comma.
UPDATE articles SET return_j='1', j_note='$note' WHERE article_name='$title'
and I don't think you need
===TRUE
just leave it out of the condition statement.

PHP MySQL record update error

I am wondering what mistakes has been made in this pretty simple update statement using old version of PHP. If I echo the statement it says update statement is getting form submitted properly.
Here is the code:
<?php
echo $q = "UPDATE notice SET FromDate = $notice_fromdate, ToDate = $notice_todate, VacType ='$notice_vactype',NoticeDetail ='$notice_detail',Status ='$notice_status' WHERE ID=$id";
if (mysql_query($link, $q)) {
echo "Record updated successfully";
} else {
echo "<h3>Error updating record</h3>". mysql_error($link)."-". mysql_errno($link). "\n";
}
?>
and the output returns this
UPDATE notice SET FromDate = 2017-01-08, ToDate = 2017-01-09, VacType ='May Day',NoticeDetail ='Testing',Status ='Enabled' WHERE ID=3
Error updating record
-0
I know its a pretty simple thing, I guess I have not made any mistake in the update statement but instead it is showing Error update record. I copied the output SQL statement and run at phpmyadmin, it has worked properly. It would be nice if you can help me. Thank in advance
Note: Clients website built on old version of PHP, I know that few functions got deprecated so it would be better if you do not discuss or criticize about the version.
Apply quotes to dates it will work
<?php
echo $q = "UPDATE notice SET FromDate = '$notice_fromdate', ToDate = '$notice_todate', VacType ='$notice_vactype',NoticeDetail ='$notice_detail',Status ='$notice_status' WHERE ID=$id";
if (mysql_query($link, $q)) {
echo "Record updated successfully";
} else {
echo "<h3>Error updating record</h3>". mysql_error($link)."-". mysql_errno($link). "\n";
}
?>

Update query using php mysqli_query (procedural style)

I am using this code to update rows and the value of $result is coming true. The if condition is executing. But the data in the table isnt updated. So, No errors yet the updation isnt happening.
What is the problem? Any help appriecated. :) Happy Diwali
for($i=0;$i<$len-1;$i++){
$sqlquery="UPDATE items_data SET item_price='".$price[$i]."' WHERE item_name='".$items[$i]."'";
$result=mysqli_query($connection,$sqlquery);
if($result){
echo "Working";
}
else
{
echo mysqli_error($connection);
}
}

Delete Query returning Value even when there is no record in database

<?php
include "conn.php";
include "session.php";
$name_enterd=$_GET['Name'];
$sql = "DELETE FROM myDB.Mynew WHERE firstname='$name_enterd' OR lastname='$name_enterd'";
echo "<br>";
$result=$conn->query($sql);
if($result==1)
{
echo "<br> Data deleted successfully";
}
else
{
echo "No Data Found<br>";
}
?>
when I run this code 1st time it works properly by deleting the data. But when i run it again it still gives me the same answer" Data Deleted Successfully" even there is no data with that value exists.
i.e $result still gets value1.
Your code should look more like this:
<?php
include "conn.php";
include "session.php";
$name_enterd=$_GET['Name'];
$sql = "DELETE FROM myDB.Mynew WHERE firstname='$name_enterd' OR lastname='$name_enterd'";
echo "<br>";
$result=$conn->query($sql);
if($result->rowCount() > 0)
{
echo "<br> Data deleted successfully";
}
else
{
echo "No Data Found<br>";
}
?>
Specifying rowCount gives you just the number of rows affected by the query
Even when the query only affects 0 rows it has still completed successfully, so you would expect $result to be 1.
You are getting the correct output. When doing that query, you're asking the database to check if there is data with that firstname or lastname and delete it. Even if there is no data with that matches it, the query has still run successfully.
You need to do use
$result->rowCount() == 1
instead of
$result == 1
It really depends what you want to use the result for. If you simply want to tell the user it has been deleted, using what you have is fine. However, if you want to let the user knows if anything has actually been deleted, you need to use my suggestion above or an alternate method to determine if this is the case.
Actually it looks like you might be using mysqli in this code, so maybe you could try using affected_rows instead of rowCount:
see http://php.net/manual/en/mysqli.affected-rows.php.
What does
$result->affected_rows
give you?

In yii how to get success message after executing sql

i need to know how to get success message after successful execution of sql or failure message of wrong execution.my example is below
`
public function actionSql()
{
$table_no='1';
$employee='1';
$status='1';
$connection=Yii::app()->db;
$sql="INSERT INTO orders_transaction (table_no,employee,status) VALUES(:table_no,:employee,:status)";
$command=$connection->createCommand($sql);
$command->bindParam(":table_no",$table_no,PDO::PARAM_STR);
$command->bindParam(":employee",$employee,PDO::PARAM_STR);
$command->bindParam(":status",$status,PDO::PARAM_STR);
$command->execute();} `
after executing i need to know is the row successfully inserted or not.
i used below one but no use its only echoing successfully not failure
if($command->execute())
{
echo "Successful";
}
else {
echo "ERROR";
}
so i tried this one its giving permission denied error for localhost with password" "
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
}
else {
echo "ERROR";
}
i hope you under stand my problem.please give any suggestion or answer.
Execute()
returns the number of affected rows(for INSERT, DELETE, UPDATE etc).
$num = $command->execute();
here $num will contain the affected number of rows.

Categories