如何使用 PHP 变量更新数据库?
How to update database using PHP variables?
$Createdby=$_SESSION['adminlog'];
$total =$_POST['total'];
$due =$_POST['due'];
$date =$_POST['issedate'];
$invoiceno =$_POST['invno'];
$CmpnyName =$_POST['CmpnyName'];
$itemdetails =$_POST['item_details'];
$itemname =$_POST['itemname'];
$amtpaid =$_POST['paid'];
$query = "UPDATE billdata SET Total='$total' Due='$due' WHERE InvoiceNo=$invoiceno";
$result = mysql_query($query);
这是我用来获取 HTML 变量值并使用新数据更新特定发票编号的代码。
$query = "UPDATE billdata SET Total='$total', Due='$due' WHERE InvoiceNo=$invoiceno";
各组值之间应该有一个逗号。
按原样使用 $_POST()
中的值不是一个好主意,最好执行一些验证检查。
首先,永远不要使用 已弃用 mysql_*
API.
切换到 PDO
或 mysqli
,两者都有 prepared statements
,这会使您的代码在 SQL-Injections
时更加安全(您的代码非常开放) ).
当查询失败时,mysql_error()
全局函数将 return 最新的 mysql 错误。
获取有关失败查询的信息的最简单方法是在查询执行后添加 or die(mysql_error());
。
您的代码示例:
$result = mysql_query($query) or die(mysql_error());
这将报告您的错误并停止执行脚本。
您的 sql 代码略有错误(如 RST 所述),您在尝试设置的值之间缺少一个逗号。
使用 mysqli
和 prepared statements
,您的代码可能类似于:
// Using the mysqli object oriented style.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
// Prepare the statement.
$statement = $mysqli->prepare('UPDATE billdata SET Total=?, Due=? WHERE InvoiceNo=?');
// The question marks is placeholders for the input that will be added in a while.
// Bind your parameters (ssi tells mysqli what type of params it is, s = string, i = int).
$statement->bind_param('ssi', $total, $due, $invoceno);
// Execute the statement.
$statement->execute();
// Cleanup.
$statement->close();
$mysqli->close();
$Createdby=$_SESSION['adminlog'];
$total =$_POST['total'];
$due =$_POST['due'];
$date =$_POST['issedate'];
$invoiceno =$_POST['invno'];
$CmpnyName =$_POST['CmpnyName'];
$itemdetails =$_POST['item_details'];
$itemname =$_POST['itemname'];
$amtpaid =$_POST['paid'];
$query = "UPDATE billdata SET Total='$total' Due='$due' WHERE InvoiceNo=$invoiceno";
$result = mysql_query($query);
这是我用来获取 HTML 变量值并使用新数据更新特定发票编号的代码。
$query = "UPDATE billdata SET Total='$total', Due='$due' WHERE InvoiceNo=$invoiceno";
各组值之间应该有一个逗号。
按原样使用 $_POST()
中的值不是一个好主意,最好执行一些验证检查。
首先,永远不要使用 已弃用 mysql_*
API.
切换到 PDO
或 mysqli
,两者都有 prepared statements
,这会使您的代码在 SQL-Injections
时更加安全(您的代码非常开放) ).
当查询失败时,mysql_error()
全局函数将 return 最新的 mysql 错误。
获取有关失败查询的信息的最简单方法是在查询执行后添加 or die(mysql_error());
。
您的代码示例:
$result = mysql_query($query) or die(mysql_error());
这将报告您的错误并停止执行脚本。
您的 sql 代码略有错误(如 RST 所述),您在尝试设置的值之间缺少一个逗号。
使用 mysqli
和 prepared statements
,您的代码可能类似于:
// Using the mysqli object oriented style.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
// Prepare the statement.
$statement = $mysqli->prepare('UPDATE billdata SET Total=?, Due=? WHERE InvoiceNo=?');
// The question marks is placeholders for the input that will be added in a while.
// Bind your parameters (ssi tells mysqli what type of params it is, s = string, i = int).
$statement->bind_param('ssi', $total, $due, $invoceno);
// Execute the statement.
$statement->execute();
// Cleanup.
$statement->close();
$mysqli->close();