没有产品添加到数据库中

时间:2016-04-28 09:07:54

标签: php mysql image

我修复了警告错误,但没有产品添加到我的数据库中。 我不知道我的错是什么?希望有人能帮助我

这是我在add.php中的代码:

if ($_POST) {
// Image 1
$image_name_1 = $_FILES ['image_1'] ['name'];
$image_type_1 = $_FILES ['image_1'] ['type'];
$image_size_1 = $_FILES ['image_1'] ['size'];
$image_tmp_name_1 = $_FILES ['image_1'] ['tmp_name'];

if ($image_name_1 == '' || $image_name_1 == null) {
    echo "<script> alert('Please select an image')</script>";
} else
    move_uploaded_file ( $image_tmp_name_1, '../../userfiles/' . $image_name_1 );

    // Image 2
$image_name_2 = $_FILES ['image_2'] ['name'];
$image_type_2 = $_FILES ['image_2'] ['type'];
$image_size_2 = $_FILES ['image_2'] ['size'];
$image_tmp_name_2 = $_FILES ['image_2'] ['tmp_name'];

if ($image_name_2 == '' || $image_name_2 == null) {
    echo "<script> alert('Please select an image')</script>";
} else
    move_uploaded_file ( $image_tmp_name_2, '../../userfiles/' . $image_name_2 );

    // Image 3
$image_name_3 = $_FILES ['image_3'] ['name'];
$image_type_3 = $_FILES ['image_3'] ['type'];
$image_size_3 = $_FILES ['image_3'] ['size'];
$image_tmp_name_3 = $_FILES ['image_3'] ['tmp_name'];

if ($image_name_3 == '' || $image_name_3 == null) {
    echo "<script> alert('Please select an image')</script>";
} else
    move_uploaded_file ( $image_tmp_name_3, '../../userfiles/' . $image_name_3 );

$data = array (
        'category_id' => $_POST ['category_id'],
        'name' => $_POST ['name'],
        'price' => $_POST ['price'],
        'detail' => $_POST ['detail'],
        'img_1' => $image_name_1,
        'img_2' => $image_name_2,
        'img_3' => $image_name_3,
        'status' => isset ( $_POST ['status'] ) ? 1 : 0,
        'created' => date ( 'Y-m-d H:i:s' ),
        'modified' => date ( 'Y-m-d H:i:s' ) 
);


if (add_product ( $data )) {
    $_SESSION ['success'] = true;
    // rdr to list.php
    header ( 'location:list.php' );
}
 }

这是添加表单

<form name="add" method="POST" enctype="multipart/form-data" action="">
    <table class="table table-hover">
        <tr>
            <th>Category</th>
            <th><select name="category_id">
                <?php while($category_active = mysql_fetch_assoc($category_active_list)): ?>
                <option
                        value="<?php echo $category_active['category_id']; ?>"><?php echo $category_active['name']; ?></option>
                <?php endwhile; ?>
                </select></th>
        </tr>
        <tr>
            <th>Product Name</th>
            <th><input type="text" name="name" value="" /></th>
        </tr>
        <tr>
            <th>Detail</th>
            <th><textarea name="detail"></textarea></th>
        </tr>
        <tr>
            <th>Image</th>
            <th><input type="file" name="image_1" /></th>
        </tr>
        <tr>
            <th>Image 2</th>
            <th><input type="file" name="image_2" /></th>
        </tr>
        <tr>
            <th>Image 3</th>
            <th><input type="file" name="image_3" /></th>
        </tr>
        <tr>
            <th>Price</th>
            <th><input type="text" name="name" value="" /></th>
        </tr>
        <tr>
            <th>Status</th>
            <th><input type="checkbox" name="status" value="1" /></th>
        </tr>
        <tr>
            <th></th>
            <th><input type="submit" value="Add" name="btnAdd" /></th>
        </tr>
    </table>
</form>

添加功能

function add_product($data) {
// SQL
$sql = "INSERT INTO tbl_product(category_id, name, price,detail, img_1, img_2, img_3, status, created, modified) VALUES({$data['category_id']}, '{$data['name']}', {$data['price']}, '{$data['detail']}', '{$data['image_1']}', '{$data['image_2']}', '{$data['image_3']}', {$data['status']}, '{$data['created']}', '{$data['modified']}')";
return mysql_query ( $sql );
}

如何解决?.Tks all

1 个答案:

答案 0 :(得分:2)

由于您的 add.php 存在于admin->product文件夹中。因此,您需要返回两个目录以将图像保存在userfiles文件夹中。

外,代码无变化
move_uploaded_file ( $image_tmp_name_1, '../../userfiles/' . $image_name_1 );

../../

中附加userfiles/
相关问题