Increment of field default value in mysql/php - 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.

Related

update mysql table with same row but different cell names

how i can update mysql table with php?
example
i have:
$sql = mysql_query("UPDATE table_name SET numbers = 'null' where no = '1'") or die(mysql_error());
$sql = mysql_query("UPDATE table_name SET numbers = 'null' where no = '2'") or die(mysql_error());
$sql = mysql_query("UPDATE table_name SET numbers = 'null' where no = '3'") or die(mysql_error());
i need one mysql request, i try this example but it doesn't work.
$sql = mysql_query("UPDATE table_name SET numbers = 'null' WHERE no IN ('1, 2, 3')") or die(mysql_error());
The solution you have come up with only has one item in the IN, and so would be equivalent to:
UPDATE table_name SET numbers = 'null' WHERE no = '1, 2, 3'
You need to use separate strings for each value, i.e.:
UPDATE table_name SET numbers = 'null' WHERE no IN ('1', '2', '3')
If you remove the single quotes around your WHERE no IN ('1, 2, 3') it will fix your problem
This is assuming that your no column is an integer column

Not able to input string number into sql database using php

I have the following code:
if(isset($_POST['regKitsForm'])){
$kitsiteID = $_POST['kitsiteID'];
$sql = "SELECT patientID FROM patient WHERE patientNum=".$_POST['kitpatientID'];
$connect->execute($sql);
$get = $connect->fetch();
$kitpatientID = $get[0];
if(is_numeric($_POST['kitNum1'])) {
$kitNum1 = str_pad($_POST['kitNum1'], 5, '0', STR_PAD_LEFT);
$kitForm = $_POST['kitForm'];
$sql = "UPDATE form$kitForm SET v0".$kitForm."_dd_kitNum1=$kitNum1 WHERE patientID = $kitpatientID AND siteID = $kitsiteID";
This should be inputing e.g.: 00001 from $kitNum1, but it isn't... it's just inputing 1.
Please help
M
Make sure, that your database column is of a string type like varchar(5) and not of an integer type. In addition, put quotes around the value in your query so that it isn't interpreted as a number, but as a string instead:
$sql = "UPDATE form$kitForm SET v0".$kitForm."_dd_kitNum1='$kitNum1' WHERE patientID = $kitpatientID AND siteID = $kitsiteID";

Error updating mysql

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

Unable to use a PHP variable column name to update a column in SQL

mysql_query("UPDATE students SET '. $rollno .' = '1'
WHERE Faculty_id = $id AND date = $date");
$ROLLONO is the name of the column which doesn't get updated.
Put date in quotes and setup the correct quotes before $rollno:
mysql_query("UPDATE students SET ". $rollno ." = '1'
WHERE Faculty_id = $id AND date = '$date'");
You have double quote before UPDATE: "
And single quote after SET: '
Which makes your query incorrect.
Date must be in quotes, otherwise it will be treated as integer with minuses.
You are using your PHP variable in magic quotes ("). So no need to keep .
Also in query column name should be either wrapped with ` or nothing.
And values for column name should be in ' or " if they are not int type.
Remove "." and also '. Add ' in date condition
mysql_query("UPDATE students SET $rollno = '1'
WHERE Faculty_id = $id AND date = '$date'");
You are using single quotes around the column name. You probably don't need them, but if you do, them use a backtick ` caharacter instead for mysql.
mysql_query("UPDATE students SET ". $rollno ." = '1'
WHERE Faculty_id = $id AND date = $date");
or
mysql_query("UPDATE students SET `". $rollno ."` = '1'
WHERE Faculty_id = $id AND date = $date");
Variables names are case-sensitive.
It's better to use the concat operator (".")
The variable must contain the name of the column, not the number
I think you are dynamically using column name and you want :
mysql_query("UPDATE students SET ". $rollno ." = '1' WHERE Faculty_id = $id AND date = $date");
$sql = "UPDATE students SET rollno = 1 WHERE Faculty_id = $id AND date = $date "
mysql_query($sql);

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