I have one drop down and lists are aMan, bMan, cMan.I am selecting any one of them from drop down. So whatever I am selecting from drop down I want to update that records according to list. Below update query is updating all my records because i added '$action_points' for each.
For example. If I selected bMan from the drop down then in update table will update only bMan records according to user_id.If I select aMan then update table it will update only aMan with 10.It will not effect on other.
I am getting the issue on update query.Would you help me with update query?
$result = $conn->query($sql_user);
if (isset($result->num_rows) > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$action_type=$row['action_type'];// Value will be aMan,bMan, cMan
$action_points=$row['action_points']; //10, 20, 30
}
}
$sql = "UPDATE man SET aMan='$action_points',bMan='$action_points', cMan='$action_points' where user_id='$user_id'";
$result = $conn->query($sql);
Update table
You are selecting from select drop down it means it will pass the value, that you have either aMan, bMan or cMan.
so you can do it like this,
$action_type = $_GET['action_type'];
$sql = "update man set `$action_type` = '$action_value' where id = $user_id";
Above is an example.
Firstly, you should replace if (isset($result -> num_rows) >0 ) with if(isset($result)) && ($result->num_rows>0)) .The first condition returns the number of rows (which at the least is 0) and then checks if it is set. Thus, isset will always return true, even when $result is not set. The second condition solves this problem
You have the type of list to update, why don't you use it?
For eg:
$result = $conn->query($sql_user);
if(isset($result)) && ($result->num_rows>0)) {
// output data of each row
while($row = $result->fetch_assoc()) {
$action_type=$row['action_type'];// Value will be aMan,bMan, cMan
$action_points=$row['action_points']; //10, 20, 30
}
}
$sql = "UPDATE man SET $action_type = $action_points WHERE user_id='$user_id'";
$result = $conn->query($sql);
This shall automatically update the required column
U should use AND in your query
UPDATE man SET aMan='$action_points' AND bMan='$action_points' AND cMan='$action_points' where user_id='$user_id'"
Or use several update query it means once update aMan row then a query for bMan row and so on.
The issue is because you are updating the three column at a time, you have to make it conditional like:
$action_type=$row['action_type'];// Value will be aMan,bMan, cMan
$action_points=$row['action_points']; //10, 20, 30
$column = '';
if($action_type == 'aMan'){
$column = 'aMan';
}
else if($action_type == 'bMan'){
$column = 'bMan';
}
else if($action_type == 'cMan'){
$column = 'cMan';
}
$sql = "UPDATE man SET ".$column." = '".$action_points."' where user_id='$user_id'";
Related
My variables in the mysql table are "usersEmail" "usersRefer and "usersPoint". I want to check the whole table if usersEmail = usersRefer on different rows then UPDATE the integer usersPoint on the row of both usersEmail and usersRefer. I want to check for one specific user, that is the value of the usersRefer checks the entire column of usersEmail to check if it matches.
Here's something that I tried out:
$sql = "SELECT * FROM walitlist";
if($result = $conn->query($sql)){
while ($row = $result->fetch_assoc()){
$pointSys = $row ["usersPoint"];
if ($row ["usersEmail"] = $row ["usersRefer"]){
$pointSys = "UPDATE waitlist SET usersPoint = usersPoint+100";
}
}
}
How can I make this work?
I am looping through the rows of the database with fetch_assoc() and I am selecting two columns of it. I check the values of these two columns of each row. If they are between 2 values then I updated a third row as 1(TRUE) if a statement is true. I am creating two connections because there are two statements, the one that selects the information and the other that updates the column that I want to be updated. When I try to print the information in the screen it seems that the SELECT statement works but when I try to UPDATE the column that I want the UPDATE statement does not update the database. Here is my code:
$conn= mysqli_connect("localhost","root","root");
$variablech = 1 ;
$query = "SELECT Client, Info FROM DataTable";
if ($result = mysqli_query($conn, $query)) {
while($row=mysqli_fetch_row($result)) {
if((($row['Client']>=54.055) && ($row['Client']<=54.117) ) && (( $row['Info']>=-4.827) && ( $row['Info']<=-4.317)))
{
$variablech = 0;
$sql= " UPDATE DataTable SET InfoData='$variablech' WHERE Client='".$row['Client']."' AND Info='".$row['Info']."'";
$conn->query($sql);
echo "yes";
}
else
{
$variablech = 1;
$sql= " UPDATE DataTable SET InfoData='$variablech' WHERE Client='".$row['Client']."' AND Info'".$row['Info']."'";
$conn->query($sql);
echo "no";
}
}
}
This should do the trick:
...
$sql= "UPDATE DataTable SET InfoData='".$variablech."' WHERE Client='".$row['Client']."' AND Info='".$row['Info']."'";
mysqli_query($conn, $sql)
...
Suggestion:
Not sure, if you have an auto_increment field in your table. If you do, then select it in select query and then use the same to update the data in update query. That will speed up the database stuff and so is your project.
I want to have a row with a column called: 'seen', then this column will have either a default of no value or a 'yes' value.
So..
Grab one row where it hasn't been 'seen' before.
Update the above row 'seen column' to say 'yes'.
If all rows have the value of 'yes' then a notice/error displays:
You have successfully completed all numbers.
I've tried the best I can do achieve it, but it's not working. I think my logic in tackling this may be incorrect?
include 'DB.php';
$con = mysqli_connect($host,$user,$pass);
$dbs = mysqli_select_db($databaseName, $con);
// Grabs one row where it hasn't been seen before
$query = mysqli_query("SELECT number, association, image_file, skeleton, sound, colour, comments FROM num_image WHERE seen='' ORDER by rand() LIMIT 1");
// Updates the above row with the 'seen' column saying 'yes''
$query = mysqli_query("UPDATE num_image SET seen = yes");
// Fetches Result
$thestuff = mysqli_fetch_row($query);
$seenme=$_POST['seen']; // get value of 'seen' column
$result = mysqli_query("SELECT * FROM num_image where seen=$seenme");
// Trying to delivery a message if the enitre 'seen' column is ALL yes.
while($row = mysqli_fetch_row($result))
{
if($row['seen'] == 'yes')
{ // All numbers seen
echo 'You have successfully completed all numbers.';
echo json_encode($thestuff);
}
else
{ // Show numbers
echo json_encode($thestuff);
}
}
Does the SELECT and UPDATE row also have to be an if statement?
Cheers
You have to escape the value in the Update sentence:
$query = mysqli_query("UPDATE num_image SET seen = 'yes' ");
I am updating a row in the mysql table. After updating the row, I want to show the user the information such as "This information is updated now"
SO this is my code:
$result->$mysqli->query("UPDATE table SET status ="updated" WHERE id = '$id'");
if($result->num_rows > 0) {
///I WANT TO ECHO/ALERT THE UPDATE INFORMATION HERE
}
Thanks in advance:
No, its not ->num_rows since you didn't select any rows so it doesn't make sense to know how many rows did it yield. If you wan't to know if it did make an update on a row, use affected_rows:
$sql = "UPDATE table SET status = 'updated' WHERE id = ?";
$update = $mysqli->prepare($sql);
$update->bind_param('s', $id);
$update->execute();
if($update->affected_rows > 0) {
echo 'yeah it updated that row!';
}
MySQLi query with update query returns FALSE on failure and TRUE on success.
So, basically, if $result is true then it has been updated. You can just echo "Success".
$result = $mysqli->query("UPDATE table SET status ="updated" WHERE id = '$id'");
if ($result)
{
echo "Success";
}
If you need to write which data has been changed you need to add SELECT queries before and\or after UPDATE query and output it.
You can show the number of updated rows for example:
$result->$mysqli->query("UPDATE table SET status ="updated" WHERE id = '$id'");
if($result->num_rows > 0) {
printf("Affected rows (UPDATE): %d\n", $mysqli->affected_rows);
}
http://php.net/manual/es/mysqli.affected-rows.php
I want to update some entries in my database, basically I am counting the number of checkboxes that have been selected on the previous page and multiplying them with 25 then adding that value to the current value in the DB.
This is my code:
<?php
if($_POST['code_approve'])
{
for($i=0;$i<count($_POST['checkbox']);$i++)
{
$approval_id = $checkbox[$i];
$checkboxCount = count($_POST['checkbox']);
$countx25 = $checkboxCount * 25;
$sql = "UPDATE table SET status='approved', used='processed' WHERE id='$approval_id'";
$sql2 = "UPDATE members SET balance = balance+'$countx25'";
$result2 = mysql_query($sql2);
$result = mysql_query($sql);
}
if($result)
{
echo "$countx25";
}
}
?>
It seems, that for some reason it is multiplying $countx25 with the number of checkboxes before inserting it into MySQL. This if($result){echo "$countx25";}} always shows me the right value though.
If i select 1 it prints 25, 2 prints 50, 3 prints 75 and so on, but for the MySQL part, if i select 1 it adds 25 to current value, 2 adds 100, 3 adds 225 ?!
What's the error here ?
In your SQL query:
$sql2 = "UPDATE members SET balance = balance+'$countx25'";
You don't tell the database which row to update, so all rows are updated. While you test, you first test once, then again and again, so it might add to fields you don't expect it to. Probably this is your problem.
To specify which row to update, use a WHERE clauseDocs.
To prevent updating the same field more than once, execute the query only once.
As I stated in my comment. You are running this for each checkbox you get via $_POST. Why do you even use the for loop if you use count to count the checkboxes. Remove the for loop and it will work as you intend it to.
The for loop in your code is the problem. I guess here you are trying to use all checkboxes in the previous page, for your code you loop for all the checkboxes, so if there are 4 checkboxes, the for loop will run 4 times. So please identify what you want to do.
This is your code.
<?php
if($_POST['code_approve'])
{
for($i=0;$i<count($_POST['checkbox']);$i++)
{
$approval_id = $checkbox[$i];
$checkboxCount = count($_POST['checkbox']);
$countx25 = $checkboxCount * 25;
$sql = "UPDATE table SET status='approved', used='processed' WHERE id='$approval_id'";
$sql2 = "UPDATE members SET balance = balance+'$countx25'";
$result2 = mysql_query($sql2);
$result = mysql_query($sql);
}
if($result)
{
echo "$countx25";
}
}
?>