通过php将数据添加到sql中

时间:2014-11-12 18:27:18

标签: php html sql

任何人都可以帮我这个,我已多次检查SQL表名,但每次我试图发布它都会给我一个错误。对此很新。提前致谢。

  require_once('connect.php');

if (isset($_POST['add_product'])):
$product_description=$_POST['product_description'];
$price=$_POST['price'];
$reorder_level=$_POST['reorder_level'];
$current_level=$_POST['current_level'];

$imagename = $_FILES['image']['name'];
$add_this = "products/$imagename";
move_uploaded_file($_FILES['image']['tmp_name'],$add_this);

$my_query="INSERT INTO products VALUES ('','$product_description','$price','$reorder_level','$current_level', '$imagename')";

$result= mysqli_query($connection, $my_query);

if ($result):
    echo "<b>Item Successfully Added!</b>";
    echo "File ";
    echo $_FILES['image']['name'];
    echo " was uploaded - ";
    echo $_FILES['image']['size'];
    echo " bytes in size<br>Temporary name: ";
    echo $_FILES['image']['tmp_name'];
    echo " - file type: ";
    echo $_FILES['image']['type'];

else:
    echo "<b>ERROR: unable to post.</b>";
endif;

 endif;

require_once 'header1.php'; 
 ?>

以下是使用

的表单
     <H1>Add a New Product</H1>
 <table>
<form method=post action="addproduct.php" enctype="multipart/form-data">
<tr><td><b>Product Description:</b><td><input type="text" name="product_description" size="30">
<tr><td><b>Price:</b><td><input type="text" name="price"> 
<tr><td><b>Re Order Level:</b><td><input type="text" name="reorder_level">
<tr><td><b>Stock Level:</b><td><input type="text" name="current_level"> 
<tr><td><b>Add Image:</b><td><input type="file" name="image">
<tr><td><input type="submit" name="add_product" >
</form>
</table>
</body>

1 个答案:

答案 0 :(得分:-1)

<?php
  require_once('connect.php');
$mysql = new MYSQLI("host", "username", "password", "database");

if (isset($_POST['add_product'])):
$product_description = $_POST['product_description'];
$price = $_POST['price'];
$reorder_level = $_POST['reorder_level'];
$current_level = $_POST['current_level'];

$imagename = $_FILES['image']['name'];
$add_this = "products/$imagename";
move_uploaded_file($_FILES['image']['tmp_name'],$add_this);

$mysql->query("INSERT INTO products (`NAME OF CELL IN TABLE WHERE YOU WANT SAVE0 $product_description`, `NAME OF CELL IN TABLE WHERE YOU WANT SAVE $price`,  `NAME OF CELL IN TABLE WHERE YOU WANT SAVE $reorder_level`,  `NAME OF CELL IN TABLE WHERE YOU WANT SAVE $current_level`,  `NAME OF CELL IN TABLE WHERE YOU WANT SAVE $imagename`) VALUES ('{$product_description}', '{$price}', '{reorder_level}', '{$current_level}', '{$imagename}')");

if ($result) {
    echo "<b>Item Successfully Added!</b>";
    echo "File ";
    echo $_FILES['image']['name'];
    echo " was uploaded - ";
    echo $_FILES['image']['size'];
    echo " bytes in size<br>Temporary name: ";
    echo $_FILES['image']['tmp_name'];
    echo " - file type: ";
    echo $_FILES['image']['type'];
}
else {
    echo "<b>ERROR: unable to post.</b>";
}

require_once('header1.php'); 
 ?>

试试这个,但在第3行设置你的信息($ mysql)  在第15行($ mysql-&gt;查询)。