I am trying to increase the views of every user by 100 but it updates all users view by adding the view in the last data of the table.
$views = '100';
$res_query = "SELECT * FROM inventory_test WHERE status='0' AND role='0'";
$result = mysqli_query($conn, $res_query);
foreach($result as $res){
$visiter = $res['visiter'];
$update_visiter = "UPDATE inventory_test SET visiter = '$visiter'+'$views'";
$run_update=mysqli_query($conn, $update_visiter);
}
$msg="Views has been successfully updated.";
Try this code:
$sql = "UPDATE inventory_test SET visiter = visiter + 100 WHERE status='0' AND role='0'";
mysqli_query($conn, $sql);
No need to perform a select before the update in that case.
Related
In several PHP codes I have to just increment a field value from a MySQL DB.
Tipically, I use this snippet:
$sql = "SELECT IDpage, numPages FROM Pages WHERE IDpage=".$page;
$result = mysqli_query( $conn,$sql)
$row = mysqli_fetch_array($result);
$num = $row['numPages'] + 1;
$sql = "UPDATE Pages SET numPages=".$num." WHERE IDpage=".$page;;
$result = mysqli_query( $conn,$sql)
Is there any more elegant and concise method?
You don't need to fetch the data first, just do the update.
$sql = "UPDATE Pages SET numPages = numPages + 1 WHERE IDpage = ".$page;
$result = mysqli_query($conn, $sql);
Also, your snippet is missing a few semicolons.
I want to show the count of users which have the status 1 (see code) within PHP MySQL.
<?php
// something like this
$result = mysqli_query($mysqli, "SELECT COUNT(*) FROM users WHERE status = '1'");
echo "$result";
?>
Try this:
$query = "SELECT COUNT(*) as countvar FROM users where status = '1'";
$result = mysqli_query($con,$query);
$row = mysqli_fetch_array($result);
$count= $row['countvar '];
<?php
require_once "config.php";
$sql = "SELECT * FROM charges";
$results = mysqli_query($link, $sql);
$row = mysqli_fetch_array($results);
$count = mysqli_num_rows($results);
for ($i=0; $i <=$count ; $i++) {
$id = $row['c_id'];
$newBalance = $row['charge']+$row['balance'];
$query = "UPDATE charges SET balance = $newBalance WHERE c_id = $id";
mysqli_query($link, $query)
}
?>
that's my php code
and the below pic is my database tables
database table
Your query is:
UPDATE charges SET balance = balance + charge
With php it is:
$sql = "UPDATE charges SET balance = balance + charge";
$results = mysqli_query($link, $sql);
I am looking to count the number of times 'yes' in present for a user in a table, then post the result into anther table for that same user. Both tables have the username. I would like this done for each user. I have the following but it is not working.
$sql = $item_count = "SELECT SUM(if(strike='yes',1,0)) AS strike_total FROM weekpicks WHERE username = 'username'";
// execute SQL query and get result
$sql_result = mysql_query($sql) or die (mysql_error());
if (!$sql_result) {
echo "Something has gone wrong!";
}
else {
//loop through record and get values
while ($row = mysql_fetch_array($sql_result)) {
$item_result = ($row = #mysql_query($item_count)) or die(mysql_error());
$strike_total = ($row = #mysql_result($item_result,"strike_total"));
$strikes = ($row = $strike_total ['strike_total']);
$username = $row["username"];
// the following will insert number of strikes into table for each user.
$sql = "UPDATE authorize SET strikes = '($strikes)' WHERE username='$username'";
//mysql_query(" UPDATE authorize SET " . "strikes = '" . ($strikes) . "' WHERE username='$username' ");
$result = mysql_query($sql) or die (mysql_error());
Just one query should be enough
Update for single user..
UPDATE authorize SET strikes = (select count(*) from weekpicks WHERE username = '$username' and strike='yes') WHERE username='$username';
For bulk update all users
UPDATE authorize as A SET strikes = (select count(*) from weekpicks B WHERE strike='yes' and A.username=B.username group by B.username)
Isn't that simple.
Hello I have a Database named as "admin" in which i have two tables
Table 1 Name = "register"
Table 2 Name = "noti"
In Register Table i've approx more than 10+ User entries which comes through Registration Page
In Noti Table, its empty at this time (Column Name is also "noti")
I want to perform this thing
First I want to count the total no. of records in "register" table
and it checks, if the records are greater than ZERO then it runs the INSERT query otherwise it runs the UPDATE Query
And i want to INSERT and UPDATE that count value into "noti" table
Here's my code
<?php
include('config.php');
$sql2 = "SELECT count(*) as count FROM register";
$result2 = mysqli_query($con, $sql2);
if($result2->num_rows>0)
{
while($rw1=$result2->fetch_array())
{
$value1 = $rw1['count'];
$result = mysqli_query($con, "SELECT count(*) as count FROM register ");
if(!empty($value1)) {
mysqli_query($con, "UPDATE noti SET noti = '$value1' ");
}
else
{
mysqli_query($con, "INSERT INTO noti(noti) VALUES ('$value1') ");
}
}
}
?>
Use this:
include('config.php');
$sql2 = "SELECT count(*) as count FROM register";
$result2 = mysqli_query($con, $sql2);
$count = $result->num_rows;
if($count != 0)
{
mysqli_query($con, "UPDATE noti SET noti = '$count' ");
}
else
{
mysqli_query($con, "INSERT INTO noti(noti) VALUES ('$value1') ");
}