gpt4 book ai didi

php - 如何根据单击的按钮重用受影响的不同行的 SQL 查询

转载 作者:行者123 更新时间:2023-11-29 10:36:56 26 4
gpt4 key购买 nike

所以有下面的一段代码,我试图将代码的顶部部分削减为只有一个 SQL 查询,该查询每次运行时都会影响不同的数据库记录(只需更改 id)每次)。

感谢任何帮助,或者链接到我可以学习如何自己执行此操作的来源,因为我似乎无法通过谷歌搜索正确的内容:(

<!DOCTYPE html>
<html>
<head>
<?php
require_once 'db.php';

if(isset($_POST['cyanxerox'])){
$sth = $conn->prepare("UPDATE supplies SET quantity = quantity + 1 WHERE Id=1");
$sth->execute();
header('Location: index.php');
die("Posted, now redirecting");

}
if(isset($_POST['magentaxerox'])){
$sth = $conn->prepare("UPDATE supplies SET quantity = quantity + 1 WHERE Id=2");
$sth->execute();
header('Location: index.php');
die("Posted, now redirecting");

}
if(isset($_POST['blackxerox'])){
$sth = $conn->prepare("UPDATE supplies SET quantity = quantity + 1 WHERE Id=3");
$sth->execute();
header('Location: index.php');
die("Posted, now redirecting");

}
if(isset($_POST['yellowxerox'])){
$sth = $conn->prepare("UPDATE supplies SET quantity = quantity + 1 WHERE Id=4");
$sth->execute();
header('Location: index.php');
die("Posted, now redirecting");

}

?>
<title>Homepage</title>ss" href="style/
<link rel="stylesheet" type="text/cmain.css">
</head>
<body>
<h1>ICT Support Printer Supplies Inventory</h1>
<form method="POST" action="index.php">
<input type="submit" name="cyanxerox" value="Cyan Xerox"/>
</form>
<form method="POST" action="index.php">
<input type="submit" name="magentaxerox" value="Magenta Xerox"/>
</form>
<form method="POST" action="index.php">
<input type="submit" name="blackxerox" value="Black Xerox"/>
</form>
<form method="POST" action="index.php">
<input type="submit" name="yellowxerox" value="Yellow Xerox"/>
</form>

最佳答案

尝试以完整的方式处理准备好的语句,例如通过使用适当的验证和 exception handling 。只有阅读documentation,您才能实现这一目标。您正在使用的每个 PHP 函数。特别是有关数据库访问操作的内容,尤其是文档的“返回值”部分。

您只需要一张带有四个提交按钮的表单。每个按钮包含相应的Id值(value)。所有按钮都具有相同的名称(我选择“xerox”)。

我还添加了三个 <meta>应该出现在 <head> 中的标签您的所有网页。

请注意,<title> 附近有一个错误放置的字符串。标签。

祝你好运!

<?php
require_once 'db.php';

if (isset($_POST['xerox'])) {
$xeroxId = $_POST['xerox'];

try {
// The sql statement - it will be prepared.
$sql = 'UPDATE supplies
SET quantity = quantity + 1
WHERE Id = :Id';

/*
* Prepare and validate the sql statement.
* If the database server cannot successfully prepare the statement, PDO::prepare()
* returns FALSE or emits PDOException (depending on error handling settings).
*/
$statement = $conn->prepare($sql);
if (!$statement) {
throw new UnexpectedValueException('The sql statement could not be prepared!');
}

// Bind and validate the binding of the input parameter.
$bound = $statement->bindValue(':Id', $xeroxId, PDO::PARAM_INT);
if (!$bound) {
throw new UnexpectedValueException('An input parameter could not be bound!');
}

/*
* Execute the prepared statement.
* PDOStatement::execute returns TRUE on success or FALSE on failure.
*/
$executed = $statement->execute();
if (!$executed) {
throw new UnexpectedValueException('The prepared statement could not be executed!');
}

/*
* If the form resides in index.php, then you don't need to do redirect,
* but just to print a success message.
*/
// header('Location: index.php');
// exit();

$message = 'Data successfully updated';
} catch (PDOException $exc) {
echo $exc->getMessage();
// Only in development phase !!!
// echo '<pre>' . print_r($exc, TRUE) . '</pre>';
exit();
} catch (Exception $exc) {
echo $exc->getMessage();
// Only in development phase !!!
// echo '<pre>' . print_r($exc, TRUE) . '</pre>';
exit();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
<meta charset="UTF-8" />
<!-- The above 3 meta tags *must* come first in the head -->

<title>Homepage</title>

<link rel="stylesheet" type="text/cmain.css">
</head>
<body>

<?php
if (isset($message)) {
?>
<div class="post-message">
<?php echo $message; ?>
</div>
<?php
}
?>

<h1>ICT Support Printer Supplies Inventory</h1>
<form action="index.php" method="POST">
<button type="submit" name="xerox" value="1">
Cyan Xerox
</button>
<button type="submit" name="xerox" value="2">
Magenta Xerox
</button>
<button type="submit" name="xerox" value="3">
Black Xerox
</button>
<button type="submit" name="xerox" value="4">
Yellow Xerox
</button>
</form>

</body>
</html>

关于php - 如何根据单击的按钮重用受影响的不同行的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46247615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com