使用php将表单数据提交到MySQL数据库

时间:2017-07-15 07:46:17

标签: php mysql forms insert

我正在尝试创建一个表单来将数据提交到MySQL数据库,但它无法正常工作。目前,我的INSERT查询出现以下错误:

PHP语法检查:解析错误:语法错误,意外的''(T_ENCAPSED_AND_WHITESPACE),期望标识符(T_STRING)或变量(T_VARIABLE)或数字(T_NUM_STRING)在您的代码中

此刻我有以下php

<?php 

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

 /* check connection */
 if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
     exit();
   }

 //Query
  $query = "INSERT INTO item (Name, Description, img_path, Quantity, Category, Location, Sale_Type, Price,  Duration, Payment) VALUES
 ($_POST['name'], $_POST['description'], $_POST['photo'], $_POST['quantity'], $_POST['category'], $_POST['location'], $_POST['Sale_Type'], $_POST['price'], $_POST['duration'], $_POST['payment'])";

    $result = mysql_query($query);
    if($result){
       echo("<br>Input data is succeed");
} else{
    echo("<br>Input data is fail");
 }

  /* close connection */
  $mysqli->close();

 ?>

这是我目前的形式。我还在编写用于上传图像的代码。在尝试上传图片之前,我目前正试图让表单无错误。

<form id="sellitem" action="sellitem.php" method="POST" onsubmit="return checkForm(this);" >
        <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="text">Description:</label>
            <textarea name="description" rows="5" cols="33" type="text" placeholder="Please describe your item"  id="description" title="Please describe your item" ></textarea><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="name">Sale Type:</label>
            <select name="Sale_Type" id="Sale_Type" >
                <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="name" title="Please enter your name" ><br />

            <label class="title" for="name">Quantity:</label>
            <input type="text" placeholder="Number of items" name="quantity" id="name" title="Number of items" ><br />

            <label class="title" for="name">Duration:</label>
            <input type="text" placeholder="End date" name="duration" id="duration" title="End Date" ><br />

            <label class="title" for="name">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>
            Select image to upload:
             <input type="file" name="img_path" id="img_path" >


            <div class="submit"><input type="submit" value="Submit" /></div>
            <div class="reset"><input type="reset" value="Reset" /></div>

            </fieldset>

            </form>

如果可以请一些帮助,看看为什么会出现这个错误。另外,为MySQL数据库创建简单上传照片的有用链接/站点也会很有帮助。

2 个答案:

答案 0 :(得分:0)

您正在使用mysqli对象,因此查询应该使用

运行
$result=$mysqli->query($query);

Php doc

答案 1 :(得分:-1)

代码只声明一个包含MySQL查询的字符串变量: 它不执行查询。 这是解决方案

<?php 
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "yourdb";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
 if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

 $sql = "INSERT INTO table_name (name) VALUES 
      ('".$_POST["name"]."')";

  if ($conn->query($sql) === TRUE) {
     echo "New record created successfully";
  } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
 }

$conn->close();

&GT;

here