gpt4 book ai didi

PHP创建下拉菜单,插入数据库

转载 作者:行者123 更新时间:2023-11-29 18:08:07 25 4
gpt4 key购买 nike

我正在尝试创建一个下拉菜单,供用户将他们的订单插入我的数据库系统。

我在创建下拉菜单时遇到问题,但我大致了解了我应该做什么。我对此很陌生,所以请耐心等待。这是我的 uml 如果有帮助的话:enter image description here

<?php

$databaseName = 'pizza_db';
$databaseUser = 'root';
$databasePassword = 'root';
$databaseHost = '127.0.0.1';
$conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databaseName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected sucessfully";
if(isset($_POST['submit'})){

$sql = "SELECT size FROM size";
$result = mysqli_query($sql);
$opt = "<select name='size'>";
while($row = mysqli_fetch_assoc($result)) {
$opt .= "option value='{$row['size']}' >{$row['size']}></option>";
}

$opt .="</select>";

if ($conn->query($sql)){
$msg = "Data inserted successfully";
} else{
$msg = "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>


<!DOCTYPE html>
<html>
<head>
<title>Pizza</title>
</head>
<body>
<form action="size.php" method="POST">
<select name="size">

</select>
<button type ="submit" name="submit"> Submit</button
</form>
</body>
</html>

我知道我的 html 代码是错误的或不完整的。如何创建多个下拉菜单以及如何在一个脚本中使用多个 $sql insert 语句以便将数据提交到数据库中?任何帮助将不胜感激。

最佳答案

  1. 您的拼写错误很少。第 13 行,if(isset($_POST['submit'})){应该是if(isset($_POST['submit'])){ 。在 while 循环选项中,您应该有 $opt .= "<option value='{$row['size']}' >{$row['size']}></option>"; (缺少“<”)

  2. 此外,还有一些冗余代码。 mysqli_query()$conn->query()执行相同的操作( see this SO tread )。简单的 OOP 与过程语法。

您可以删除

if ($conn->query($sql)){
$msg = "Data inserted successfully";
}
else{
$msg = "Error: " . $sql . "<br>" . $conn->error;
}

之后,这样的事情就足够了

<?php

$databaseName = 'pizza_db';
$databaseUser = 'root';
$databasePassword = 'root';
$databaseHost = '127.0.0.1';
$conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databaseName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected sucessfully";
if(isset($_POST['submit'])){

$sql = "SELECT size FROM size";
$result = mysqli_query($sql);
$opt = "<select name='size'>";
while($row = mysqli_fetch_assoc($result)) {
$opt .= "<option value='{$row['size']}' >{$row['size']}></option>";
}

$opt .="</select>";
$conn->close();
}
?>

但是,据我所知,您有一个空表单,在提交时会获取所有尺寸。如果您想像 php getsized> 生成表单> 提交表单并重定向到下一步,您应该这样做或类似于这(并从 php 部分删除 if $_POST['submit'] 条件)

<!DOCTYPE html>
<html>
<head>
<title>Pizza</title>
</head>
<body>
<form action="submit-chosen-size.php" method="POST">
<label for="size">Select your size:</label>
<?php echo($obj); ?>
<button type ="submit" name="submit"> Submit</button
</form>
</body>
</html>

编辑:请尝试使用此代码。它与上面的代码非常相似,但有助于查找问题

<?php
$databaseName = 'pizza_db';
$databaseUser = 'root';
$databasePassword = 'root';
$databaseHost = '127.0.0.1';
$conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databaseName);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected sucessfully";

$sql = "SELECT size FROM size";
$result = mysqli_query($sql);
$opt = "<select name='size'>";
while($row = mysqli_fetch_assoc($result)) {
$opt .= "<option value='{$row['size']}' >{$row['size']}></option>";
echo $row['size']; // you can remove this later
}
$opt .="</select>";
echo $obj; //you can remove this later
$conn->close();
?>

<!DOCTYPE html>
<html>
<head>
<title>Pizza</title>
</head>
<body>
<form action="submit-chosen-size.php" method="POST">
<label for="size">Select your size:</label>
<?php echo(isset($obj) ? $obj : "Empty"); ?>
<button type ="submit" name="submit"> Submit</button
</form>
</body>
</html>

关于PHP创建下拉菜单,插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47644121/

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