gpt4 book ai didi

php - 数据未插入数据库

转载 作者:行者123 更新时间:2023-11-29 19:46:15 24 4
gpt4 key购买 nike

我正在尝试将表单数据插入数据库,但它不起作用,我添加了下面的代码。请检查并告诉我问题是什么。由于删除和搜索查询正在工作,但插入是唯一不起作用的查询。谢谢

<form method="post" action= "assign5.php">

Name : <input type ="text" name="name" >

<br>

Flavor : <select name="flavor">

<option value="Chocolate">Chocolate</option>
<option value="Vanilla">Vanilla</option>
<option value="Strawberry">Strawberry</option>
<option value="MahngiVanilla">MahngiVanilla</option>
<option value="SastiStrawberry">SastiStrawberry</option>

</select>

<br>

Scoops:
<input type ="radio" name="scoops" value="1">1</input>
<input type ="radio" name="scoops" value="2">2</input>
<input type ="radio" name="scoops" value="3">3</input>
<input type ="radio" name="scoops" value="4">4</input>
<input type ="radio" name="scoops" value="5">5</input>

<input type ="submit" name="button" value="Place Order"/>

<br>
<br>

</form>
<?php
require_once 'login.php';

$connection = new mysqli($db_hostname,$db_username,$db_password,$db_database);

if($connection ->connect_error) die($connection ->connect_error);

if(isset($_POST['name']) && isset($_POST['Flavour']) && isset($_POST['Scoops']) ){

$CName=$_POST['name'];
$Flavor=$_POST['flavor'];
$Scoops=$_POST['scoops'];

$sql ="INSERT INTO orders VALUES (CName, Flavour, Scoops) VALUES ('$CName', '$FLAVOUR', '$Scoops')";
$result=$connection->query($sql);
if(!$result) die($connection->error);
header("Location: assign5.php");

}

$connection->close();

?>

最佳答案

您必须提供表格中的名称、 flavor 和范围。即使您提供了,但您的查询仍然不起作用。因为您在表单中的输入键是小写字母,并且您在使用第一个字符的大写字母的情况下检查它。所以尝试一下

if(isset($_POST['name']) && isset($_POST['flavour']) && isset($_POST['scoops']) ){
.....
.....
}

您使用了两次 Values。请更正此问题

$sql ="INSERT INTO orders VALUES (CName, Flavour, Scoops) VALUES ('$CName', '$FLAVOUR', '$Scoops')";

致:

 $sql ="INSERT INTO orders (CName, Flavour, Scoops) VALUES ('$CName', '$FLAVOUR', '$Scoops')";

关于php - 数据未插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40956863/

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