dtwncxs3547 2017-07-15 07:46
浏览 56


This question already has an answer here:

I am trying to create a form to submit data into a MySQL database but it is not working. At the moment I have the following error for my INSERT query:

PHP Syntax Check: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in your code

at the moment I have the following php


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

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

  $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);
       echo("<br>Input data is succeed");
} else{
    echo("<br>Input data is fail");

  /* close connection */


This is currently what I have for my form. I am yet to still write code in for uploading an image. I am currently trying to get the form to work with no errors before I attempt the image upload.

<form id="sellitem" action="sellitem.php" method="POST" onsubmit="return checkForm(this);" >
            <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>

            <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>

            <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 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>



If I could please get some help as to why this error is appearing. Also a useful link/site to creating a simple upload photo to the MySQL database would also be helpful.

  • 写回答

2条回答 默认 最新

  • duanpo6079 2017-07-15 08:04

    The code just declares a string variable that contains a MySQL query: It does not execute the query. here is the solution

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



    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 关于#matlab#的问题:需要 MATLAB 运行程序,对比四个多址通信方式(TDMA,FDMA,CDMA,OFDMA)的抗噪声性能
  • ¥15 有没有复现过PFENet的,如何解决?
  • ¥15 FDMA,OFDMA,TDMA 和 CDMA 系统容量对比仿真
  • ¥15 图神经网络的入侵检测
  • ¥15 工业数据采集技术+存储架构推荐
  • ¥15 GPT-SOVITS如何训练少数民族语言支持合成语音
  • ¥20 树莓派4b使用Camera Module 3时出现the system should be configured for the legacy camera stack问题
  • ¥200 GitHub开源程序配置在VScode调试
  • ¥15 爬虫保存的scv文件0kb
  • ¥20 如何实现基于强化学习的带电作业机械臂的运动规划与控制