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
Related
I need to count the number of columns that have a specific value (1) in a specific row (250). The value of the row is variable according to the query.
I tried the code below, but it didn't work.
$total_r = mysqli_query($con, "SELECT * FROM registers WHERE ID = '$ID' AND 1 IN (column1, column2, column3, column4)";
$total = mysqli_num_rows($total_r);
echo $total;
I need the result as a number, lets say "2 columns" or something like that.
In MySQL Booleans become 0 or 1 in numerical context. So you could add expressions checking for a column to be equal to 1.
SELECT (column1 = 1)
+ (column2 = 1)
+ (column3 = 1)
+ (column4 = 1)
FROM registers
WHERE id = ?;
Check with below query
SELECT COUNT(*) TOT FROM registers WHERE ID = '$ID' AND column1='1' AND column2='1' AND column3='1' AND column4='1'
What finally did was a general query and then fetch and save every column value into a variable. Then i just added them, because the value of each column could be 0 or 1. Here is the code:
$query = mysqli_query($con, "SELECT * FROM registers WHERE ID = '$ID'");
$result = mysqli_fetch_array($query);
$column1 = $result['column1'];
$column2 = $result['column2'];
$column3 = $result['column3'];
$total = $column1 + $column2 + $column3;
If the column has a value = 0, will not be considered into the count.
I would like to update a column row inside MYSQL by adding 50 to the last current number.Means column named sum was 200 and after query, it will update to 250.
I can do this by using two query as follow:
$add1 = 50;
$sql = "SELECT sum FROM table_name WHERE id = '$id' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = mysqli_fetch_assoc($result);
$add2 = $row['sum'] + $add1 ;
}
$sql1 = "UPDATE tabel_name SET sum = '$add2' WHERE id = '$id' ";
$result1 = $conn->query($sql1);
However, is there a better way to do this with less code or by one go?
Thanks
You can calculate values directly in UPDATE queries.
$add1 = 50;
$sql1 = "UPDATE tabel_name SET sum = sum + $add1 WHERE id = '$id';";
In case the type of your column sum is not numeric, but contians a string of the given number, you can use the MySQL CAST function to cast it to a number, calculate it and cast it back to a string again:
$sql1 = "UPDATE tabel_name SET sum = CAST((CAST(sum as INT(11)) + $add1) as VARCHAR(11)) WHERE id = '$id';";
Why dont you try By
$sql1 = "UPDATE tabel_name SET sum = sum + 'YOUR VALUE' WHERE id = '$id'
You can do it using UPDATE for this, just change your query like:
$sql = "UPDATE table_name SET sum=sum+value_you_want WHERE id = '$id' ";
Please use this sql query:
$sql1 = "UPDATE tabel_name SET sum = sum + 50 WHERE id = '$id' ";
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";
$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.
I've got a message while i'm run following sql query...
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group = 'dfdfd' WHERE id = '39'' at line 1"
Sql query:
$sql_update = mysql_query("UPDATE addcontacts SET surename = '$surname_g', group =
'$g_g' WHERE id = '$id'");
Please use ` to enclose group, it is being treated as special (group by keyword of SQL) by mysql
Use the following:
UPDATE addcontacts SET surename = '$surname_g', `group` = '$g_g' WHERE id = '$id'
Note `group` and not group
Try:
$sql_update = mysql_query("UPDATE addcontacts SET surename = '".$surname_g."', `group` = '".$g_g."' WHERE id = '".$id."'");
Your id might be an integer and you are enclosing it with two single quotes (') and that would really produce the error.
$sql_update = mysql_query("UPDATE addcontacts SET surename = '{$surname_g}', group =
'{$g_g}' WHERE id = {$id}");
Thank you :)