Error updating mysql - php

I am sending data from an android app to save in my mysql database.
My Codes are
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = $time, transaction_status = 2 , remark = $rem WHERE transaction_id = $t_id LIMIT 1");
And my dabase is as,
remark varchar(500) latin1_general_ci.
The problem is whenever I have $rem as a numbric value then database is updated but if I use any text then it wont.
Please help me.

Make sure wrapping string data with single quote.
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = '$time', transaction_status = 2 , remark = '$rem' WHERE transaction_id = $t_id LIMIT 1")

Text should be wrapped by single quotes: ''
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = $time, transaction_status = 2 , remark = '$rem' WHERE transaction_id = $t_id LIMIT 1");

Related

Feedback for php code

hello guys I am subtracting one unit from a variable called Eppl which resides in a Database and this is working perfectly. However I have added a condition which is that the value of the variable Eppl must be greater than 0 before the subtraction takes place. My code is as follows:
$id = $_GET["id"];
$result = $mysqli->query("SELECT `CreatedBy` AND `Eppl` FROM `events` WHERE `Eid` = '$id'");
$row = $result->fetch_assoc();
$sessionid = $row['CreatedBy'];
$notlesszero = $row['Eppl'];
$zero = 0;
//echo $result;
session_start();
if ((!($_SESSION['login_user'] == $sessionid)) && ($notlesszero>0)){
$mysqli->query("UPDATE `events` SET `Eppl` = `Eppl` - 1 WHERE `Eid` = '$id'");
$mysqli->query("INSERT INTO `eventusers` (EventID, UserID) VALUES ('{$id}', '{$_SESSION['login_user']}')");
header("location:view.php");
//}
}
The if branch works without the second condition $notlesszero>0...
What is the problem here?
Thanks for your help
Instead of querying the database to get the value of the field and then checking it, why not just add a condition on the UPDATE?
$mysqli->query("UPDATE `events` SET `Eppl` = `Eppl` - 1 WHERE `Eid` = '$id'");
to
$mysqli->query("UPDATE `events` SET `Eppl` = `Eppl` - 1 WHERE `Eid` = '$id' AND `Eppl` > 0");
The first query is wrong after you added Eppl to it. Fields to select should not be separated by AND. So
SELECT `CreatedBy` AND `Eppl` FROM
should be
SELECT `CreatedBy`, `Eppl` FROM
This is something you could have caught by inspecting the error information after executing the query, or at least by inspecting $row after changing the query. :-p

Increment of field default value in mysql/php

$saa = "update aspirantdt set vote = 'vote'+1 where 'post_id' = '$id' ";
When i check the database the value didnt increase.
Help me out.
yuo have a sintax mistake in your update query, you are using quotest instead of backtick. Use backticks for column names and quotes for values try to change as follow
$saa = "update `aspirantdt` set `vote` = (`vote`+1) where `post_id` = '".$id."' ";
Just remove quotes or use Backtics for column names.
$saa = "UPDATE aspirantdt SET vote = vote + 1 where post_id = '$id' ";
Or with backticks
$saa = "UPDATE `aspirantdt` SET `vote` = `vote` + 1 where `post_id` = '$id' ";
$saa = "update aspirantdt set vote = 'vote'+1 where 'post_id' = '$id' ";
means that 'vote' and 'post_id' are literal strings, not table names (that is, it will compare $id to the actual string post_id instead of the value of the post_id column).
What you want is backticks to quote them as a column/table name instead;
$saa = "update `aspirantdt` set `vote` = `vote`+1 where `post_id` = '$id' ";
You don't need quotes around vote as its a column name, not a string.
Your SQL is probably trying to put vote1 in an int column.

UPDATE mysql_query not updating table

Im having trouble with the UPDATE command.
Im trying to update my db but its just not happening. Ive been trying to get this to work for the last 10 days and its driving me nuts.
Here is the code:
$a = mysql_query("UPDATE `findacab` SET `lat` = ".$ads['Latitude']." , `long` = ".$ads['Longitude']."
WHERE `eeventendtime` = ".$ads['Postcode']." ");
Table:
$q = mysql_query("SELECT Postcode, Latitude, Longitude FROM postcodes");
while($ads = mysql_fetch_array($q))
{
mysql_query("UPDATE findacab SET lat = '".$ads['Latitude']."' , long = '".$ads['Longitude']."' WHERE eeventendtime = '".$ads['Postcode']."' ");
echo $ads['Latitude']." ".$ads['Longitude']." ".$ads['Postcode']."</br>";
//$query = "select count(*) from findacab where eeventendtime = '".mysql_real_escape_string($ads['Postcode'])."'";
}
Unless your complete table consists of only integer you should add quotations around your strings
$a = mysql_query("UPDATE `findacab` SET
`lat` = '".$ads['Latitude']."' ,
`long` = '".$ads['Longitude']."'
WHERE
`eeventendtime` = '".$ads['Postcode']."' ");
$query = "select count(*) from findacab where eeventendtime = '".mysql_real_escape_string($ads['Postcode'])."'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo $row[0]
If it returns 0 then you just don't have records to update.
Another possible reason - you are trying to update table with same values as stored. In this case update will not change the data.

MYSQL Query not working as it should

$sql = "UPDATE `shows` SET `title` = '$title', `tagline` = '$tagline', `desc` = '$desc' , `img_src = '$imgsrc' WHERE id = $showid";
The query above does not want to work, I simply get a mysql_error saying error at '' on line 1;
Any idea where I am going wrong?
You're missing a tick:
`img_src = '$imgsrc' WHERE id = $showid";
should be:
`img_src` = '$imgsrc' WHERE id = $showid";

Php mysql, variable help query help

I have this mysql query:
UPDATE `table`.`wp_12_postmeta`
SET `meta_value` = 'yyy'
WHERE `wp_12_postmeta`.`meta_id` =5
LIMIT 1 ;
How do i incorporate this:
instead of wp_12_ i want a variable $prefix (variable holds wp_4_, wp_3_, etc)
instead of yyy i want a value $perf (variable is a name )
instead of 5 i want a value $meta_id (variable is a nr)
Thank u!
P.S.
here is what i use and it works:
$wpdb->query("UPDATE ".$prefix."postmeta SET meta_value = '".$perf."' WHERE meta_id = '".$meta_id."' LIMIT 1 ");
Problem is, when i execute this query, severl post meta fields are updated, instead of just one.
Ty
Use:
$query = sprintf("UPDATE `table`.`%s`
SET `meta_value` = '%s'
WHERE `%s`.`meta_id` = %d
LIMIT 1 ",
mysql_real_escape_string($prefix),
mysql_real_escape_string($perf),
mysql_real_escape_string($prefix),
mysql_real_escape_string($meta_id));
Here's how I would write this with PDO:
$prefix = "wp_4_";
$sql = "UPDATE `table`.`{$prefix}postmeta` SET `meta_value` = ?
WHERE `{$prefix}postmeta`.`meta_id` = ? LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($perf, $meta_id));

Categories