简体   繁体   English

我想使用PHP更新数据库表MySQL

[英]I want to update a database table MySQL using PHP

My table is called servicii and has 3 variables : id, nume, pret. 我的表称为servicii,具有3个变量: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 尝试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 或者如果您想知道error使用此

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. 假设$ nume和$ pret变量包含字符串。 then change $stmt->bind_param("ssi", $nume, $pret, $id); 然后更改$stmt->bind_param("ssi", $nume, $pret, $id); this line to $stmt->bind_param("sdi", $nume, $pret, $id); 这行到$stmt->bind_param("sdi", $nume, $pret, $id);

When you bind parameters, i stands for integer. 当您绑定参数时, i代表整数。 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 . 如果要解决此问题,则需要更改ssi的绑定中的iii (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 i是整数
  • s is for string. s是字符串。 ie: "John Doe" 即:“ John Doe”
  • d is for double d代表双倍
  • and b is for blob. b是blob。

You can find out more on the php.net website! 您可以在php.net网站上找到更多信息!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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