Record not getting updated in PHP/MySQL - php

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.

Related

I have a form that updates some values in mysql, how do I avoid to update the cell when an input is empty?

I have a script in PHP that performs a multyquery update after taking some values in different inputs form. Everything works fine if I fill all these forms and press save. However if I leave one field open I receive an error as that value can't be empty.
Now, what I'd like to do is that when the php/html form has an empty input field the record shouldn't be changed and keep the value is currently in the database.
Here's part of my current code
$sql = "UPDATE task SET name='$aName', surname='$aSurname', htbTotal='$ahtbtotal' WHERE id=1;";
$sql .= "UPDATE task SET name='$fName', surname='$fSurname', htbTotal='$fhtbtotal' WHERE id=2;";
if ($conn->multi_query($sql) === TRUE) {
echo "Record updated successfully";
$risposta= "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
$risposta= "Error updating record: " . $conn->error;
}
This is an input example
<input id="aName" name="aName" type="text" placeholder="">
Any suggestion?
Thanks in advance!
Update: Just to make more clear, I don't need a validation. I want the user to leave some input empty if they don't want to fill but this shouldn't rewrite the related row value inside the database
$sql = "UPDATE task SET";
if(strlen($_POST['aName'])>0)
{
$sql .= " name='$aName',";
}
if(strlen($_POST['aSurname'])>0)
{
$sql.=" surname='$aSurname',";
}
if(strlen($_POST['htbTotal'])>0)
{
$sql.=" htbTotal='$htbTotal'";
}
$sql.=" WHERE id=1;";
this way you can solve by comparison

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);
}
}

PHP MySqli Delete error message

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.

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