Update query using php mysqli_query (procedural style) - php

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

Related

PHP If Else statement not working for database

Ok, I'm confused. I have some code that searches a database table for a username, and then uses an if else statement to run some code depending on if the user is found or not. My code is below. The problem is that the code isn't even seeing the if else statement, and I have no idea why. Any help is appreciated.
$sqluser = "select * from users where username='" . $user ."'"; //Searching to see if the user is in the database
echo $sqluser . "<br><br>"; //writes out the select statement to make sure it is correct
$query = mssql_query($sqluser); //returns the results
$num_rows = mssql_num_rows($query); //gets the number of rows returned
echo $num_rows; //writes out the number of rows
if ($num_rows==0) //determines what happens next if the user exists or not
{
//displays an error box if the user doesn't exist
echo "<script type=text/javascript>";
echo "alert('That user doesn't exist. Please try again.')";
echo "</script>";
}
else
{
//will be code to run if the user does exist
echo "<script type=text/javascript>alert('Testing.')</script>";
}
I couldn't add a comment. So I will write this as an answer instead.
Since you state that the alert JavaScript is showing in the page source, this mean that the IF/ELSE statement in PHP is working fine. The problem is with the single quote. You have a single quote inside a single quoted alert function. Hence the JavaScript alert function cannot be executed.
echo "alert('That user doesn't exist. Please try again.')";
Try using this instead
echo "alert('That user doesn\'t exist. Please try again.');";

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";
}
?>

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?

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.

sql not inserting record

I am trying to insert a record into my database. here is my php code for doing that:
mysql_query("INSERT INTO answers(answer)
VALUES('answer')");
if(mysql_affected_rows() > 0) {
echo "Reply successful!";
} else {
echo "We were not able to add your reply.";
exit();
}
i made sure that i am connected to my db. i tried this on another page where i knew my sql would run, and it still didn't insert the record...
add
echo mysql_error();
to see the problem
Try passing $answer instead of answer. Also you should read up on SQL injection.

Categories