Using RedBaeanPHP, I want the page to be updated and add + 1 to the "view".
Please help me, I don't understand it very well.
$post_id = $_GET['post_id'];
$posts = R::load('posts', $post_id);
if (isset($_SESSION['recent_posts'])) {
R::exec('UPDATE posts SET view = "view" + 1 WHERE id = $posts["id"]');
};
When a page is refreshed, it is not added to the database.
Since you have already loaded the rquested post from the database (by doing $posts = R::load('posts', $post_id);) you could use the store method instead of R::exec.... So just use $posts->view++; and R::store($posts);
Thanks, already solved the problem
$id = $posts['id'];
$view = "UPDATE `posts` SET `view` = `view` + 1 WHERE `id`= $id";
R::exec($view);
Related
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'")
The main code which i use is
while ($row = mysql_fetch_assoc($result1)) {
echo $row["user_id"];
$new = "[grade uid=\"{$row["user_id"]}\" value=\"{$groupgrade}\" format=\"{$groupformat}\" prv_comment=\"{$groupprivatecomment}\" pub_comment=\"{$grouppubliccomment}\"] ";
$sqq = "UPDATE wp_postmeta SET meta_value='$new' WHERE meta_key='grade' AND post_id= $post_id ";
$result = mysql_query($sqq);
echo "grade has been updated";
}
Which affects the rows in table wp_postmeta as shown in the picture on the link Where all the variables undefined here are defined before this para .
What i actually want that user id must be different in different rows.
The only thing happening in this loop is that each uid from the array gets written in all the rows one after the other and only the last uid persists finally which is 3 in the diagram but i only want that each uid should come separately i.e., differnt uid for diff. meta_id . Please help
REGARDING CODE
$result1 fetches an array of userids from a defined mysql function and rest variables are user defined.
I am kinda new to php and to programming so please help
The question is not very clear and it's WordPress related and you didn't add WordPress tag in your question. Anyways, WordPress has it's own function to handle post meta. In your loop you are trying to update post meta manually but it's possible to do that using WordPress' update_post_meta function. The syntax is given bellow
<?php update_post_meta($post_id, $meta_key, $meta_value, $prev_value); ?>
In this case you can use following code
<?php update_post_meta($post_id, 'grade', $new); ?>
Here $post_id will be your post id which you want to update.
OK, now that I understand your problem (you confused me by saying that "all rows" were being updated, you just mean all rows with this post_id), I think this is the answer:
$sql2 = "SELECT meta_id FROM wp_postmeta where meta_key='grade' AND post_id = $post_id";
$result2 = mysql_query($sql2);
while (($row = mysql_fetch_assoc($result1)) && ($row2 = mysql_fetch_assoc($result2))) {
echo $row2["meta_id"], ' => ', $row["user_id"];
$new = "[grade uid=\"{$row["user_id"]}\" value=\"{$groupgrade}\" format=\"{$groupformat}\" prv_comment=\"{$groupprivatecomment}\" pub_comment=\"{$grouppubliccomment}\"] ";
$sqq = "UPDATE wp_postmeta SET meta_value='$new' WHERE meta_key='grade' AND post_id= $post_id AND meta_id = {$row2["meta_id"]}";
$result = mysql_query($sqq);
echo " grade has been updated";
}
The new $sql2 query gets each meta_id, and updates each row with a different user_id from $result11.
your code is to work around this $row["user_id"]
if it same user who posted then the result will be true 3
if this variable is from other query then u must look to this query how u getting this user_id or , publish it in your question , to see why it comes only 3.
its better to make a column for user_id to insert data specialy for that user.
$row["user_id"] is beeing a warning variable to sql injection , so it must be escaped by mysql_real_escape_string()
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'";
I have the code below:
$do_id=$_GET['id'];
$new_workorder=$_GET['workorder'];
//get current workorder on file for record that will be updated
$query = "SELECT workorder FROM jvltodos WHERE id = '$do_id' LIMIT 1";
$bla = mysql_query($query);
$workorder_old = mysql_fetch_row($bla);
//get record for workorder on file that will have the new workorder
$query = "SELECT id FROM jvltodos WHERE workorder = '$new_workorder' LIMIT 1";
$bla = mysql_query($query);
$id_fix = mysql_fetch_row($bla);
if (isset($new_workorder) && $new_workorder!=$workorder_old) {
//update the changed record
$query = "UPDATE jvltodos
SET workorder={$new_workorder}
WHERE id = {$do_id}";
$result = mysql_query($query);
//update the record that had the old workorder
$query = "UPDATE jvltodos
SET workorder={$workorder_old}
WHERE id = {$id_fix}";
$result = mysql_query($query);
}
I pass the 'id' and 'workorder' into the url, so I GET them first. With that I update the record with that 'id' to the new 'workorder'. So far so good.
But I want to find the record that has that 'workorder' and update that with the 'workorder' from the old 'id'. (In simple terms: when I increase/decrease the 'workorder', I want to switch two records around)
To me it looks pretty straightforward, but it doesn't work. The first thing (update the id with the new workorder works.. but the other record stays the same.. it keeps the workorder that was already in there)
What am I missing? (must be something simple that I'm just overlooking.)
#andrewsi Thank you for the suggestion! I took the code out, copied it over to another page and did some echo's to see what was going on and that way I figured out easily what the problem was.
Example:
$bla = mysql_query($query);
$workorder_old = mysql_fetch_row($bla);
The $workorder_old that I wanted to have should've been a number. However, this didn't happen, but returned.. an array.
When I changed the code to...
$bla = mysql_query($query);
$bla = mysql_fetch_row($bla);
$workorder_old = $bla[0];
... it worked like a charm.
tiny note: I'm so used to grabbing the data in 1 step (another language), that I forgot that I needed to make a few steps in PHP. Guess it's time to throw it in a function perhaps.. :-)
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;