【发布时间】:2017-07-19 06:50:45
【问题描述】:
我目前正在尝试使用表单和 php 将表单数据插入到 MySQL 数据库中。该表单未提交数据,我不确定我的代码是否存在问题或我的数据库中是否存在某些内容。我已经多次检查数据库中的所有代码是否正确匹配,并验证我的代码没有错误。有什么我错过的简单的东西吗?
<?php
$mysqli = new mysqli("localhost", "root", "", "etrading");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if(isset($_POST['submit'])) {
$key=$_POST['ItemID'];
$name= $_POST['Name'];
$description= $_POST['Description'];
$img_path= $_POST['img_path'];
$quantity= $_POST['Quantity'];
$category= $_POST['Category'];
$location= $_POST['Location'];
$saletype= $_POST['Saletype'];
$price= $_POST['Price'];
$duration= $_POST['Duration'];
$payment= $_POST['Payment'];
$query = "INSERT INTO item (ItemID, Name, Description,img_path, Quantity, Category, Location, Saletype, Price,Duration,Payment) VALUES ('$key','$name','$description','$img_path','$quantity','$category','$location','$saletype','$price','$duration','$payment',)";
if (mysqli_query($mysqli, $query)) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . mysqli_error($mysqli);
}
}
/* close connection */
$mysqli->close();
?>
我还在数据库中将 ItemID 设置为自动递增 这是我正在使用的表单代码。
<form id="sellitem" action="sellitem.php" method="POST" >
<fieldset>
<h4>Sell Your Item</h4>
<p><label class="title" for="Name">Name:</label>
<input type="text" placeholder="Enter item name" name="Name" id="Name" title="Please enter item name"
><br />
<label class="title" for="Description">Description:</label>
<textarea name="Description" rows="5" cols="33" placeholder="Please describe your item" id="Description" title="Please describe your item" ></textarea><br />
Select image to upload:
<input type="file" name="img_path" id="img_path" ><br>
<label class="title" for="Quantity">Quantity:</label>
<input type="text" placeholder="Number of items" name="Quantity" id="Quantity" title="Number of items" ><br />
<label class="title" for="Category">Category:</label>
<select name="Category" id="Category">
<option value="clothes">Clothes</option>
<option value="books">Books</option>
<option value="electronics">Electronics</option>
<option value="sport">Sport</option>
</select></p>
<label class="title" for="Location">Location:</label>
<input type="text" placeholder="Item Location" name="Location" id="Location" title="Enter item location" ><br />
<label class="title" for="Saletype">Sale Type:</label>
<select name="Saletype" id="Saletype" >
<option value="Auction">Auction</option>
<option value="BuyNow">Buy Now</option>
</select>
<label class="title" for="Price">Price: $</label>
<input type="text" placeholder="00.00" name="Price" id="Price" title="Please enter your name" ><br />
<label class="title" for="Duration">Duration:</label>
<input type="text" placeholder="End date" name="Duration" id="Duration" title="End Date" ><br />
<label class="title" for="Payment">Payment Type:</label>
<select name="Payment" id="Payment" >
<option value="PayPal">PayPal</option>
<option value="Bank Deposit">Bank Deposit</option>
<option value="Card">Credit Card</option>
</select><br>
<div class="submit"><input type="submit" value="submit" /></div>
<div class="reset"><input type="reset" /></div>
</fieldset>
</form>
【问题讨论】:
-
请阅读mysqli手册了解如何使用
?操作符 -
我直接使用了来自 w3schools 的代码。但我会阅读 mysqli 手册
-
你在sql查询中有错字...删除'$payment'后面的逗号!
-
只是为了方便您的信息,您在最后一个查询中使用
,(逗号)。 -
一个简单的事情是缺少参数化查询 - 我猜是 w3schools 的危险