使用表单和php将数据插入MySQL
我目前正在尝试使用表单和php将表单数据插入MySQL数据库。表单没有提交数据,我不确定我的代码是否有问题,或者我的数据库中是否存在某些内容。我已经多次检查数据库中的所有代码是否正确匹配,并验证我的代码是否没有错误。有什么简单的东西我错过了吗使用表单和php将数据插入MySQL,php,mysql,insert,Php,Mysql,Insert,我目前正在尝试使用表单和php将表单数据插入MySQL数据库。表单没有提交数据,我不确定我的代码是否有问题,或者我的数据库中是否存在某些内容。我已经多次检查数据库中的所有代码是否正确匹配,并验证我的代码是否没有错误。有什么简单的东西我错过了吗 <?php $mysqli = new mysqli("localhost", "root", "", "etrading"); /* check connection */ if ($mysqli->connect_errno) {
<?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>
将这一行代码从
<input type="submit" value="submit" />
到
您没有输入ifisset$\u POST['submit']{
将name属性添加到submit按钮
<input type="submit" value="submit" name="submit" />
1.每个输入字段都应该有name属性来发布/获取数据
<input type="submit" value="submit" />
to
<input type="submit" value="submit" name="submit" />
3.下面的查询添加了一个额外的逗号
$query=插入项目ID、名称、描述、img_路径、数量、类别、位置、销售类型、价格、期限、付款值“$key”、“$Name”、“$Description”、“$img_路径”、“$Quantity”、“$Category”、“$Location”、“$Saletype”、“$Price”、“$Duration”、“$Payment”;请阅读mysqli手册,了解如何直接从w3schools。但我会阅读mysqli手册。如果sql查询中有输入错误,请删除“$payment”后面的逗号!仅为了了解您的信息,您在查询的最后一个部分使用了逗号。一个简单的问题是缺少参数化查询-我想w3schools的危险性
<input type="submit" value="submit" />
to
<input type="submit" value="submit" name="submit" />
Array
(
[Name] => dsfdsf
[Description] => dfdsf
[img_path] =>
[Quantity] =>
[Category] => clothes
[Location] =>
[Saletype] => Auction
[Price] =>
[Duration] =>
[Payment] => PayPal
)