just everything together for a number - php

I should have made ​​such that it is +1 up ​​database instance every time you write a status on its away
problem: right now when writing +1 then the whole time 1
solved: this is how I want when I have 1 point in advance so it must just above the old numbers thus making it for 2 and 3, etc.
if ($stmt = $this->mysqli->prepare('UPDATE bruger SET point=? WHERE id=?')) {
$stmt->bind_param('si', $point, $id);
$point = +1;
$id = $_SESSION["id"];
$stmt->execute();
$stmt->close();
}

You should either do a select query first, then add 1 using PHP and then do an update query, or change the update query so that it automatically adds 1 without using PHP, which is better.
Changing the query would result in the following code:
if ($stmt = $this->mysqli->prepare('UPDATE bruger SET point=point + 1 WHERE id=?')) {
$stmt->bind_param('si', $id);
$id = $_SESSION["id"];
$stmt->execute();
$stmt->close();
}

Related

update query not working in prepared statement with multiple where clause

I have the following update statement which does execute successfully but with no value change in the table.
$name = "John Doe"; //to update into John Stack
$chenna = "Mz"; $reg = 25; $km = 3;
$dbh = PDO Object
$stmt = $dbh->prepare("UPDATE `hl_customer` SET `name`=:hming, `address`=:chenna
WHERE `regd`=:regd AND `kum`=:km");
$stmt->bindParam(':hming', $name, PDO::PARAM_STR);
$stmt->bindParam(':chenna', $hmun, PDO::PARAM_STR);
$stmt->bindParam(':regd', $reg, PDO::PARAM_INT);
$stmt->bindParam(':km', $km, PDO::PARAM_INT);
$stmt->execute();
$affected = $stmt->rowCount();
Another tested code:
$stmt = $dbh->prepare("UPDATE `hl_customer` SET `name`=?, `address`=?
WHERE `regd`=? AND `kum`=?");
$stmt->execute([$name, $hmun, $reg, $km]);
$affected = $stmt->rowCount();
$stmt = $dbh->query("UPDATE `hl_customer` SET `name`='$name', `address`='$chenna'
WHERE `regd`='$reg' AND `kum`='$km'");
In order to update I kept changing the $name variable, yet there was no affected row. The row count always return 0. I did tested in both phpmyadmin(latest version) and mysql Workbench(latest) and the problem is still there. Then I tested again in mysql console, and it works as expected. But why is it not working in the code shown above, phpmyadmin and workbench. What could be the problem? Is my code wrong? I used mysql 8.0.12, php 5.6.* and php 7.1.*.
I did test it again without parameterized query, still it did not work. Now I begin to think that it is a kind of bug in php.
Thanks
Well i don't see anything wrong with your code try and verify if the number of columns in your table matches the number of paramaters you have because you said it works when you drop the last parameter

SQL UPDATE Statement does not affect any rows (PHP)

I'am currently working on a project and want to make a simple page where I can edit groups. I had everything working fine in XAMPP and tried uploading it to the server, but it won't affect any rows in the database.This is the statement:
UPDATE user_groups
SET name = 'TEST',
name_short = 'test',
color = 'green',
category = 'MMORPG'
WHERE id = 2
and:
Affected rows (UPDATE): 0
Is the answer. Creating new groups works fine (Local creating and editing works and I did not change anything in the statements since I uploaded both)
This is what the row looks like that I am trying to affect
EDIT:
$sql_update_info = "UPDATE user_groups SET name = '$new_title', name_short = '$new_short', color = '$new_color', category = '$new_cat' WHERE id = $group_id";
$query_update_info = mysqli_query($mysqli, $sql_update_info);
printf("Affected rows (UPDATE): %d\n", mysqli_affected_rows($mysqli));
echo '<br><span style="color:white;">'.$sql_update_info.'</span>';
Is what the PHP part looks like when clicked on the button.
1st : Try to use prepared statement to avoid sql injection.
2nd : Execute() will return true or false so based on that you need to handle the error like below.
$stmt = $mysqli->prepare("UPDATE user_groups SET name = ?, name_short = ?, color = ?, category = ? WHERE id = ?");
$stmt->bind_param('ssssi', $new_title, $new_short, $new_color, $new_cat, $group_id);
//The argument may be one of four types:
//i - integer
//d - double
//s - string
//b - BLOB
//change it by respectively
$r = $stmt->execute();
if(!$r){
echo $stmt->error;
}else{
$row_count= $stmt->affected_rows;
}
$stmt->close();
$mysqli->close();

update mysqli query with set values

I can update my columns dynamically, but I dont know how to update it with set values (stupid I know)
This is my sql code that updates the columns with set values:
if (isset($_POST['delete'])) {
$sql = 'UPDATE users SET user_deletion_date = NOW(), user_deleted_by = '.$_SESSION['id'].', deleted = Y
WHERE user_id = ?';
if ($stmt->prepare($sql)) {
// bind the query parameters
$stmt->bind_param('i', $_GET['user_id']);
// bind the result to variables
$stmt->bind_result($user_id, $user_deletion_date, $user_deleted_by, $deleted);
// execute the query, and fetch the result
$done = $stmt->execute();
$stmt->fetch();
}
}
if ($done) {
header('Location: update_users_confirm.php');
exit;
}
this doesn't update the table at all, I know that the issue is with my bind_param, could someone please help
Michael B's answer is mostly likely the solution. Change the $_GET to $_POST

update multiple rows in table mysql

im wondering if someone could point me in the right direction as im trying to update multiple rows but for some reason everytime time i do the update, variable id_employee is set with the same value for all the rows.. that´s what i figured out when i var_dumped $_POST. The rest of fields are fine.. Here is a complete view of my code. http://pastie.org/5478920, also this is what i get when i var_dump http://pastie.org/5478980
$query = "update project_staff
set
id_employee=?
where
id_project=?
and
id_projectemployee=?
";
$stmt = $this->dbh->prepare($query);
for ($i = 0; $i < count($_POST['id_employee']); $i++){
$employee = $_POST['id_employee'][$i];
$stmt->bindValue(1, $employee, PDO::PARAM_INT);
$stmt->bindValue(2, $_POST["id"], PDO::PARAM_INT);
$stmt->bindValue(3, $_POST["idstaff"], PDO::PARAM_INT);
$stmt->execute();
}
echo("Project" . " " . $_POST["nom"] . " ". "has been updated");
You are setting the bind param to an explicit data type PDO::PARAM_INT, but values FROM $_POST are always string.
You should change and validate your POST input to be INT first and change it to INT or use PDO::PARAM_STR
//UPDATE
Looking at your code you set the value of the where clause to the content of $_POST['id'] AND $_POST["idstaff"]. So in every run trough your foreach you are requesting the same value. So the $employee is update on every run, but the where clause is always the same. this would look like:
//example
//$_POST['id'] = 20;
//$_POST['idstaff'] = 40;
//$_POST['id_employee'][0] = 1;
//$_POST['id_employee'][1] = 2;
//$_POST['id_employee'][2] = 3;
//run 1
$employee = $_POST['id_employee'][0]; //1
update project_staff set id_employee=1 where id_project=20 and id_projectemployee=40
//run 1
$employee = $_POST['id_employee'][1]; //2
update project_staff set id_employee=2 where id_project=20 and id_projectemployee=40
//run 1
$employee = $_POST['id_employee'][2]; //3
update project_staff set id_employee=3 where id_project=20 and id_projectemployee=40
so you are overwriting previous changes on every run.

mysql_affected_rows returns 0

I have a problem with mysql_affected_rows() function in PHP. I use MySQL update, in phpMyAdmin I can see, that 'confirmed' changes from 0 to 1, but mysql_affected_rows still returns 0! I cannot find a solution. My code is:
$query = "UPDATE visits
SET confirmed = 1
WHERE id = ? AND confirmed = 0 AND expire > now() - INTERVAL 10 MINUTE;";
$stmt = $this->conn->stmt_init();
if($stmt->prepare($query)) {
$stmt->bind_param('i',$id); //$id is a function parameter
$res = $stmt->execute();
$stmt->close();
echo mysql_affected_rows();
}
It seems you're using PDO, not the mysql_* functions.
Therefore, you should uso PDOs rowCount function:
$query = "UPDATE visits
SET confirmed = 1
WHERE id = ? AND confirmed = 0 AND expire > now() - INTERVAL 10 MINUTE;";
$stmt = $this->conn->stmt_init();
if($stmt->prepare($query)) {
$stmt->bind_param('i',$id); //$id is a function parameter
$res = $stmt->execute();
echo $stmt->rowCount();
$stmt->close();
}
Use affected_rows to get the number of affected rows when using an UPDATE statement :
$stmt = $this->conn->stmt_init();
if($stmt->prepare($query)) {
$stmt->bind_param('i',$id); //$id is a function parameter
$res = $stmt->execute();
echo $stmt->affected_rows;
$stmt->close();
}
It also needs to be before the close() statement
Since everyone seems to think you using PDO, whereas it looks more like MySQLi to me, here is the MySQLi way:
$query = "
UPDATE visits
SET confirmed = 1
WHERE id = ?
AND confirmed = 0
AND expire > now() - INTERVAL 10 MINUTE
";
$stmt = $this->conn->stmt_init();
if ($stmt->prepare($query)) {
$stmt->bind_param('i', $id); //$id is a function parameter
$res = $stmt->execute();
echo $stmt->affected_rows; // Here's the good stuff
$stmt->close();
}
Use this http://www.php.net/manual/en/pdostatement.rowcount.php
The PDOStatement::rowCount return the number of rows affected by the query
You need to pass the connection as a parameter to the function.
echo mysql_affected_rows($this->conn);
http://php.net/manual/en/function.mysql-affected-rows.php

Categories