Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 准备语句和img文件上载失败_Php_Html_Image_Mysqli_Prepared Statement - Fatal编程技术网

Php 准备语句和img文件上载失败

Php 准备语句和img文件上载失败,php,html,image,mysqli,prepared-statement,Php,Html,Image,Mysqli,Prepared Statement,我正试图创建我的第一个准备好的声明,我真的需要一些帮助来看看我做错了什么。在执行此操作之前,我的大部分查询都正常工作,但每个人都告诉我应该这样做以防止SQL注入 我正在尝试将产品信息发送到我的数据库。在这些数据中,我试图发送一个img文件,并将其移动到我的数据库中。在尝试这个准备好的语句之前,我正在执行一个简单的mysqli insert语句,我的查询中唯一不起作用的部分是让img的文件名作为实际文件名显示在我的数据库中。上传的方式是“数组”或“1”。我改变了周围的事情,试图使它与程序声明的工作

我正试图创建我的第一个准备好的声明,我真的需要一些帮助来看看我做错了什么。在执行此操作之前,我的大部分查询都正常工作,但每个人都告诉我应该这样做以防止SQL注入

我正在尝试将产品信息发送到我的数据库。在这些数据中,我试图发送一个img文件,并将其移动到我的数据库中。在尝试这个准备好的语句之前,我正在执行一个简单的mysqli insert语句,我的查询中唯一不起作用的部分是让img的文件名作为实际文件名显示在我的数据库中。上传的方式是“数组”或“1”。我改变了周围的事情,试图使它与程序声明的工作,我可以最好的

我不太确定我上传img文件的尝试是否正确。原因是这些数据现在都没有被发送到我的数据库。我的数据库连接没有收到任何错误

我从验证开始。我还没有对img文件进行任何验证。我会补充说,一旦我可以得到这个实际发送

然后我处理表单信息。我不确定$file变量的结构是否适合将文件名发送到数据库,因为我遇到了一个错误

我通过..上的php错误信息获得此错误。。注意:第90行的未定义索引:img in/home4/pfarley1/public_html/addproduct.php

//Create
$date = date('Y-m-d H:i:s');

if($validation->passed()) {
    if(isset($_POST['create'])){ 
        $product_id = trim( $_POST['product_id'] );
        $name = trim( $_POST['name'] );
        $price = trim( $_POST['price'] );
        $saleprice = trim( $_POST['saleprice'] );
        $final_price = trim( $_POST['final_price'] );
        $shippingprice = trim( $_POST['shippingprice'] );
        $category = trim( $_POST['category'] );
        $item_details = trim( $_POST['item_details'] );
        $item_details2 = trim( $_POST['item_details2'] );
        $description = trim( $_POST['description'] );
        $viewproduct_type = trim( $_POST['viewproduct_type'] );
        $date = ( $_POST['date_created'] );
 //line 90 $file = ($_POST ['img'] [move_uploaded_file($_FILES['file']['tmp_name'],"productpics/". $_FILES['file']['name'])] );

    //Connection
    $con = mysqli_connect("localhost","root","","bfb");
    /* check connection */
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }



    /* create a prepared statement */
    if ($stmt = mysqli_prepare($con, "INSERT INTO products ('product_id', 'name', 'price', 'saleprice', 'final_price', 'shippingprice', 'category', item_details', 'item_details2', 'description', 'viewproduct_type', 'date', 'img') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {


        /* bind parameters for markers */
        $stmt->bind_param('isiiiisssssis', $product_id, $name, $price, $saleprice, $final_price, $shippingprice, $category, $item_details, $item_details2, $description, $viewproduct_type, $date, $file);

        /* execute query */
        $stmt->execute();


    /* close statement */
    mysqli_stmt_close($stmt);
        }
    }
  }
 }
 }   
?>
形式

<form action="" method="POST" enctype="multipart/form-data">
    <div class="field">
        <label for="product_id">Product ID</label>
        <input type="text" name="product_id" class="smallinputbar" required>
    </div>
    <div class="field">
        <label for="name">Product Name</label>
        <input type="text" class="inputbar" name="name" required>
    </div>
    <div class="field">
        <label for="price">Product Price</label>
        <input type="text" class="smallinputbar" name="price" required>
    </div>
    <div class="field">
        <label for="saleprice">Sale Price</label>
        <input type="text" class="smallinputbar" name="saleprice">
    </div>
    <div class="field">
        <label for="final_price">Final Price</label>
        <input type="text" class="smallinputbar" name="final_price" required>
    </div>
    <div class="field">
        <label for="shippingprice">Shipping Price</label>
        <input type="text" class="smallinputbar" name="shippingprice" required>
    </div>
    <div class="field">
        <label for="category">Category</label>
        <input type="text" class="inputbar" name="category" required>
    </div>
    <div class="field">
        <label for="item_details">Item Details</label>
        <input type="message" class="messageinput" name="item_details" required>
    </div>
    <div class="field">
        <label for="item_details2">Item Details 2</label>
        <input type="message" class="messageinput" name="item_details2">
    </div>
    <div class="field">
        <label for="description">Description</label>
        <input type="message" class="messageinput" name="description" required>
    </div>
    <div class="field">
        <label for="viewproduct_type">View Product Type</label>
        <select class="optionbar" name="viewproduct_type">
            <option name="notSelected">Not Selected</option>
            <option name="viewproduct_type" value="a href='./viewProduct.php?view_product=$id">Standard</option>
            <option name="viewproduct_type" value="Option">Option</option>
        </select>
    </div>

        <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
        <label for="button">

        <input type="submit" id="button" name="create" value="Create New Product">
        </label>

    <span class="floatright">
        <input type="file" name="file" class="inputbarfile">
        <input type="submit" name="create" id="signinButton" value="Upload">
    </span>

产品ID
品名
产品价格
销售价格
最终价格
运费
类别
项目详细信息
项目详情2
描述
查看产品类型
未选择
标准
选项

您的表单不包含名为
img
的字段。那篇文章['img']
应该是什么?文件不在
$\u POST
中,它在
$\u文件中。我不确定如何构造它。我读到当你上传一个文件时,你必须给它命名为file。它进入数据库的位置称为img。我如何构造它,使其进入数据库中的img行?好的,当您发送文件时,您有
name
属性是索引名,其中的值将以PHP表示。发送文件时,它包含在
$\u files
超全局变量中。要从表单中访问文件,请使用
$\u FILES[\u表单字段中的'name']
。在您的情况下,它将是
$\u FILES['file']
。因此,由于我的file变量中已经有了$\u FILES['file']。我是否需要将表单中的文件名更改为img?”代码“代码”