2017-07-15 221 views
-1

我试图创建一个表单提交数据到MySQL数据库中,但它无法正常工作。目前,我的INSERT查询有以下错误:使用PHP将表单数据提交到MySQL数据库

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数据库也会有所帮助。

+0

我认为你正在使用,而不是''mysql' mysqli'。 '$ result = mysql_query($ query);'。将其更改为'$ result = mysqli_query($ mysqli,$ query);' – urfusion

+1

也是一个普遍的说法:你的代码对sql注入攻击是宽泛的。请阅读使用“预处理语句”和“参数绑定”组合的安全优势来防止此问题。 – arkascha

+0

而'echo'语句是一种语言结构,而不是函数。您不需要括号:'echo“
输入数据成功”;' – arkascha

回答

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

?>

here

+0

你愿意详细说明吗?仅有代码的答案被认为是不好的答案... – arkascha

+0

感谢兄弟。下一次详细解答代码 –

+0

嗯,当然,但是为什么不给这个问题添加一些解释词呢? – arkascha

0

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

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

Php doc