警告:非法字符串偏移'名称'

时间:2016-02-10 20:02:47

标签: php

我正在尝试制作产品插页,以便我可以将产品插入数据库,然后在产品页面中显示它们。

但是当我点击添加产品按钮时,我会收到以下错误。

  

警告:非法字符串偏移'名称'   第14行的C:\ MAMP \ htdocs \ Project \ admin \ insert.php

     

注意:未初始化的字符串偏移量:0英寸   第14行的C:\ MAMP \ htdocs \ Project \ admin \ insert.php

     

警告:非法字符串偏移'tmp_name'   第15行的C:\ MAMP \ htdocs \ Project \ admin \ insert.php

     

注意:未初始化的字符串偏移量:0英寸   第15行的C:\ MAMP \ htdocs \ Project \ admin \ insert.php

这是insert.php页面。

<?php
include'/setup/function.php';

$Error = "";
$_FILES['image'] = "";

//INSERT PRODUCT////////////////////////////////////////////////
if(isset($_POST['add_pro'])){

    $pro_cat        = validateFormData($_POST['category']);
    $pro_title      = validateFormData($_POST['title']);
    $pro_price      = validateFormData($_POST['price']);
    $pro_desc       = validateFormData($_POST['desc']);
    $pro_image      = $_FILES['image']['name'];
    $pro_image_tmp  = $_FILES['image']['tmp_name'];
    $pro_keyword    = validateFormData($_POST['keyword']);

    move_uploaded_file($pro_image_tmp, "image/$pro_image");

    $query = "INSERT INTO product (pro_id, pro_cat, pro_title, pro_price, pro_desc, pro_image, pro_keyword,) 
            VALUES (NULL, '$pro_cat', '$pro_title', '$pro_price', '$pro_desc', '$pro_image', '$pro_keyword')";
    $result = mysqli_query($conn, $query);

    mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>Add New Product</title>

        <?php include'/setup/style.php'; ?>
        <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>

        <?php echo $Error; ?>

        <div class="container">
            <div class="content">
                <h2>Add a New Product</h2>
                <form method="post" action="insert.php" enctype="multipart/form-data">
                    <div class="form-group">
                        <label for="title">Title</label>
                        <input type="text" name="title" class="form-control" id="title" placeholder="Enter Product Title">
                    </div>
                    <div class="form-group">
                        <label for="image">Add Image</label>
                        <input type="file" name="image" id="image">
                    </div>
                    <div class="form-group">
                        <label for="desc">Description</label>
                        <textarea name="desc" class="form-control" cols="50" rows="10" id="desc" placeholder="Enter Product Description"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="price">Price</label>
                        <div class="input-group">
                            <span class="input-group-addon">$</span>
                            <input type="text" name="price" class="form-control" id="price" placeholder="Enter Product Price">
                        </div>                       
                    </div>
                    <div class="form-group">
                        <label for="keyword">KeyWords (seperate keywords with a coma.)</label>
                        <input type="text" name="keyword" class="form-control" id="keyword" placeholder="Enter Product KeyWords">
                    </div>
                    <div class="form-group">
                        <label for="category">Title</label>
                        <select name="category" class="form-control" id="category">
                            <option>Select a Category</option>
                            <?php
                            $Cquery = "SELECT * FROM category";
                            $Cresult = mysqli_query($conn, $Cquery);

                            if(mysqli_num_rows($Cresult) > 0){
                                while($row = mysqli_fetch_assoc($Cresult)){
                                    $cat_id = $row['cat_id'];
                                    $cat_title = $row['cat_title']; 
                            ?>
                            <option value="<?php echo $cat_id; ?>"><?php echo $cat_title; ?></option>
                            <?php }} ?>
                        </select>
                    </div>

                    <button type="submit" name="add_pro" class="btn btn-primary">Add Product</button>
                </form>
            </div>
        </div>



        <!-- Footer
        ================================================== -->
        <hr class="featurette-divider">
        <div class="container">
            <footer>
                <p>&copy; <?php echo date('Y'); ?> Arcitic Falcon &middot; <a href="#">Privacy</a> &middot; <a href="#">Terms</a></p>
            </footer>
        </div><!-- /.container -->

        <!-- Bootstrap core JavaScript
        ================================================== -->
        <?php include'/setup/javascript.php'; ?>
    </body>
</html>

由于

3 个答案:

答案 0 :(得分:3)

您将$_FILES['image']设置为空字符串:

$_FILES['image'] = $_FILES['pdf'] = "";

删除它。

答案 1 :(得分:3)

你杀了你上传的内容:

$_FILES['image'] = $_FILES['pdf'] = "";
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

由于您为这两个$ _FILES条目分配了一个空字符串,因此它们不再是数组,并且当您执行

$_FILES['image']['tmp_name'];

您正在使用非数组的非法偏移量。字符串 CAN 被视为数组,但您不能使用非数字索引与它们交谈:

$foo = 'bar';
echo $foo[1]; // outputs "a"
echo $foo['baz']; // illegal offset

答案 2 :(得分:1)

你应该在php文件的开头尝试这段代码。

echo "<pre>";
print_r($_FILE['image']);
edcho "</pre>"; 
die;