dongpu5874 2017-07-19 06:50
浏览 52
已采纳

使用表单和php将数据插入MySQL

I am currently trying to get form data to insert to a MySQL database using a form and php. The form is not submitting the data and I am not sure if there is an issue with my code or there is something in my database. I have checked numerous times that all the code matches correctly in the database as well as validating my code with no errors. Is there something simple that i have missed?

<?php

 $mysqli = new mysqli("localhost", "root", "", "etrading");

 /* check connection */
 if ($mysqli->connect_errno) {
 printf("Connect failed: %s
", $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();
?>

I have also set the ItemID to auto increment in the database And this is my form code that i am using.

<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>
  • 写回答

3条回答 默认 最新

  • dongqian9013 2017-07-19 06:56
    关注

    Change this line of your code from

    <input type="submit" value="submit" />
    

    to

    <input type="submit" value="submit" name="submit" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看