简体   繁体   中英

inserting data to MySQL using a form and php

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\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();
?>

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>

Change this line of your code from

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

to

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

You are not entering if(isset($_POST['submit'])) {

Add the name attribute to the submit button

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

1.Every input field should have name attribute to POST/GET data.

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

2.Due to obscene of name attribute of type="submit" field form posting only

Array
(
    [Name] => dsfdsf
    [Description] => dfdsf
    [img_path] => 
    [Quantity] => 
    [Category] => clothes
    [Location] => 
    [Saletype] => Auction
    [Price] => 
    [Duration] => 
    [Payment] => PayPal
)

3.Below query added one extra comma

$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' , )";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM