简体   繁体   中英

Edit Value in MySQL Database

I am trying to edit the value of an integer in my database through php, Here is the code I am using

 $query = "UPDATE Bremners WHERE stud_name = john SET stud_goal = stud_goal + 1";

Bremners is the Table and in the table there is a column with the name of a person (stud_name) so I tried making it so that if the stud_name = john then change the int which represents the number of goals that john has, the column that has the number of goals is stud_goal. I tried increasing that value by 1.

Try this:

$query = "UPDATE Bremners SET stud_goal = stud_goal + 1 WHERE stud_name = 'john'";

in PHP you could start with something like this:

<?php
$mysqli = new mysqli("localhost", "root", "", "test");

$name = "Bill";
$increment = 1;

if ($stmt = $mysqli->prepare("UPDATE Bremners SET stud_goal = stud_goal + ? WHERE stud_name = ?"))
{
    $stmt->bind_param("is", $increment, $name); 
    $stmt->execute();
    printf("%d Row affected.\n", $stmt->affected_rows);

    $stmt->close();
}
$mysqli->close();
?>

if you need to change multiple values by multiple amounts, you should use write your query this way:

$mysqli->prepare("UPDATE Bremners
                  SET
                    stud_goal = stud_goal + ?,
                    stud_assist = stud_assist + ?
                  WHERE stud_name = ?")

? are placeholders, then you have to bind your parameters this way:

$stmt->bind_param("iis", 1, 5, "john");
  • this will replace the first ? with 1 of type "i" (integer)
  • the second ? with 5 of type "i" (integer)
  • the third ? with "john" of type "s" (string)

Please have a look at this link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM