dswsl2016 2014-11-12 18:27
浏览 12
已采纳

通过php将数据添加到sql中

Can anyone help me with this, i have checked the SQL table names multiple times but every time i attempt to post it gives me an error. Pretty new to this. Thanks in advance.

  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'; 
 ?>

Here is the form im using

     <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条回答 默认 最新

  • duan4739 2014-11-12 18:41
    关注
    <?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'); 
     ?>
    

    Try this, but set your information on line 3 ($mysql) and on line 15 ($mysql->query).

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号