简体   繁体   中英

I want to update a database table MySQL using PHP

My table is called servicii and has 3 variables : id, nume, pret. I have this code, but doesn't work. Thank you.

if(isset($_POST['btnUpdate2']))
{
    $stmt = $conn->prepare("UPDATE servicii SET nume =?, pret=? WHERE id=?");
    $nume = $_POST['txtNume'];
    $pret = $_POST['txtCantitate'];
    $id = $_POST['selectProd'];
    $stmt->bind_param("iii", $nume, $pret, $id);
    $stmt->execute();
    $stmt->close();
    $_SESSION['msg'] = "Product successfuly updated!";
}

This is my database strong text

Try to echo error

try this

if ($stmt->execute()) { 
  $_SESSION['msg'] = "Product successfuly updated!";
} else {
   $_SESSION['error'] = "Product not updated!";
}

or if you want to know error use this

if(!$stmt->execute()) echo $stmt->error;

Use this

if(isset($_POST['btnUpdate2']))
{
    $stmt = $conn->prepare("UPDATE servicii SET nume =?, pret=? WHERE id=?");
    $nume = $_POST['txtNume'];
    $pret = $_POST['txtCantitate'];
    $id = $_POST['selectProd'];
    $stmt->bind_param("ssi", $nume, $pret, $id);
    $stmt->execute();
    $stmt->close();
    $_SESSION['msg'] = "Product successfuly updated!";
}

assuming $nume & $pret variables contain string. then change $stmt->bind_param("ssi", $nume, $pret, $id); this line to $stmt->bind_param("sdi", $nume, $pret, $id);

When you bind parameters, i stands for integer. Which is one of the reasons your query to the database is not working.

If you want to fix this you need to change iii in the binding to ssi . (This is of course assuming that the first two arguments(?) are strings...

$stmt = $conn->prepare("UPDATE servicii SET nume = ?, pret= ? WHERE id = ?");
$stmt->bind_param("ssi", $nume, $pret, $id); // ssi
if($stmt->execute()){
    echo "Success!";
} else {
    echo 'Failed.';
}

Here are a list of argument types for future reference:

  • i is for integer
  • s is for string. ie: "John Doe"
  • d is for double
  • and b is for blob.

You can find out more on the php.net website!

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