MySQL add data to row after existing data - php

I am trying to add new data right after the current data. This works well,
mysql_query("UPDATE uyeler SET shoots=shoots + 1 WHERE nick='admin'")
However, its only for numbers. I tried that with normal text value
$places = "london";
mysql_query("UPDATE uyeler SET shootplaces= shootplaces + '".$places."' WHERE nick='admin'")
But all it does is that it makes the shootplaces to 0. What is the correct way to add new value to a row without deleting the current value in it ?

You have to use Mysql CONCAT function.
$places = "london";
mysql_query("UPDATE uyeler SET shootplaces= CONCAT(shootplaces , '".$places."') WHERE nick='admin'")

Is your shootplaces has INT as datatype ?
Anyway, you can use concat() function to add new data into new cell.
UPDATE your_table
SET your_column = CONCAT(your_column, 'your new value') WHERE id=..
See this link for further help

try this.
$places = "london";
mysql_query("UPDATE uyeler SET shootplaces= concate(shootplaces , '$places') WHERE nick='admin'")

Related

adding a passed in number to a MySQL cell with PHP

I am trying to add a number to a number already in a MySQL table. I am using POST to pass in the number.
<?php
require "connection.php";
$id = $_POST["id"];
$number = $_POST["number"];
Do I have to make 2 separate queries, one to retrieve the current value and set is as a variable and one to insert the new value by adding the number to the variable or can I accomplish this with one query with something like this:
$update_query = "update Table set value = (value + $number) where id like '$id';"
Yes, you can use one query UPDATE tbl_name SET value_col = value_col + :number WHERE id = :id
Notes
ID is unique, so you need id = :id to achieve faster queries and only update one field.
Use prepared statements instead of putting values directly to query
update field to add value to existing value:
UPDATE table SET value = value + $number WHERE id = $id;
update field to contact value to existing value:
use the CONCAT function :
UPDATE table SET value= CONCAT('$number',value) WHERE id=$id;

MySQL table row will not UPDATE

I am trying to update a column in a row in a MySQL table. The column is the 'votes' column and when someone submits an HTML form there is a hidden input with a value of "1" that gets submit and posted. This is the code I am using to try to update the vote count:
if(isset($_POST['image_id']) && isset($_POST['vote'])){
$image_id = $mysqli->real_escape_string($_POST['image_id']);
$vote = $mysqli->real_escape_string($_POST['vote']);
$sql_users_vote = "SELECT * FROM users WHERE id='$image_id'";
$result_users_vote = $mysqli->query($sql_users_vote);
$row_vote = mysqli_fetch_array($result_users_vote);
$votes_count = $row_vote['votes'];
$new_votes = $votes_count + $vote;
$sql_vote = "UPDATE users WHERE id='$image_id' SET votes=$new_votes";
$result_vote = $mysqli->query($sql_vote);
}
I have echo'ed out the variable up until $sql_vote and $image_id, $vote, $votes_count and $new_votes all echo out the correct values. I'm guessing that there is a problem in the UPDATE syntax. I've checked it over and over but can't seem to find anything. I know that I don't have quotes around $new_votes in the UPDATE because I believe that is correct syntax. I've tried it with quotes and it doesn't work that way either.
Can someone help me identify the problem? Thanks!
Doesn't the SET come before the WHERE?
$sql_vote = "UPDATE users SET votes = $new_votes WHERE id = '$image_id'"
Or does it not matter?
$sql_vote = "UPDATE users SET votes=$new_votes WHERE id='$image_id'";

how UPDATE mysql row plus one and get the value

I updated with success
$result = mysql_query("UPDATE $table SET `queue2` = `queue2` + 1 WHERE `id` = '$getid'");
but how can I get the "queue2" value without opening a new request to MySQL
I can simply get the new value with this command
$selresult = mysql_query("SELECT * FROM $table WHERE `id` = '$getid'") or die(mysql_error());
but I'm afraid that the database can get new update again and i will get higher number
Any idea how to do it ?
you can use query to update the value.
mysql_query("UPDATE user_profile SET userpoints = userpoints + 1 WHERE user_id = '".$user_id."'");
See URL:-
PHP + MySQL transactions examples
Try this:-
printf ("Updated records: %d\n", mysql_affected_rows());
mysql_query("COMMIT");
You will need to use a transaction between the queries to be certain.
The docs for transactions are here. A good SO question that covers it in detail: PHP + MySQL transactions examples
Edit:
Looking at it from a different angle, why don't you do it in reverse though? It might save the need for a transaction (thought it is possible that you get multiple reads before a write):
Get the value for your queue2 value to display in the page from this:
mysql_query("SELECT * FROM $table WHERE `id` = '$getid'");
You have the true value now, so you can run:
$result = mysql_query("UPDATE $table SET `queue2` = `queue2` + 1 WHERE `id` = '$getid'");
No transaction and you know the value of the data before the update.

Adding the value of a variable to a database field

I have a MySQL database table called submission with a field called points with a type of bigint(100).
On a PHP file, I have a variable called $commentpoints with a numerical value.
In the PHP file, how could I add $commentpoints to points?
It's a matter of adding it in your SQL update query:
$sql = 'UPDATE submissions
SET points = points + ' . (int) $commentpoints . '
WHERE id = ' . (int) $id;
That will add the value of $commentpoints to the points in the database; just make sure to use the where clause so you don't add it to every record in the submissions table.
UPDATE table SET points = '[your value]'
mysql_query("UPDATE submission SET points = '$commentpoints'");
$sql = "UPDATE submission
SET points = points + ?
WHERE commentid=?";
$q = $conn->prepare($sql);
$q->execute(array($commentpoints,$commentid));

page count won´t add +1 to count field

I´ve been trying to make "most popular article" script
retrieve the most popular is no problem.... but I have tried every "add count+" methods
but the count field in my mysql always shows 0
This is my script
$add = "1";
$counter=mysql_query("SELECT * FROM news WHERE newsid = '".$newsid."'");
while ($ntcounter=mysql_fetch_array($counter)) {
mysql_query("UPDATE news SET count = '".$ntcounter[count]+$add."' WHERE newsid = '".$newsid."'")
}
I´m starting to think if the database is not updateable
Is there something I´m missing here?
You can do this in one go:
UPDATE news SET `count` = `count`+1 WHERE newsid = '".$newsid."'
EDIT:
<?php
//TURN ON ERROR REPORTING!!!
error_reporting(E_ALL);
//Type cast the variable to an integer, despite where its set
(int)$newsid=1;
//or
(int)$newsid=$_GET['id'];
//$newsid="1"; is setting 1 as a string
mysql_query("UPDATE news SET `count` = `count`+1 WHERE newsid=".$newsid);
?>
Note if your not checking or casting type. always remember to use mysql_real_escape_string()
You shouldn't be doing it like that at all. MySQL has built-in functionality for increasing values, you can just do:
UPDATE news SET count = count+1 WHERE newsid = '$newsid'
Yes. Your $add should be an integer.
$add = 1;

Categories