为什么这个UPDATE查询不适用于php

时间:2013-01-03 14:09:47

标签: php mysql sql-update

您好我很难理解为什么这不会更新数据库。它重新加载HEADER指向的页面,但不更新任何字段。任何帮助都会有点贪婪。

更新表单

 <?php
    include("../script/dbconnect.php");
    include("../script/addprodcat.php");

    $post = get_posts($_GET['id']);

    if ( isset($_POST['name'], $_POST['description'], $_POST['price'], $_POST['sale'], $_POST['picture'], $_POST['category']) ) {
    $errors = array();

        $name = trim($_POST['name']);
        $description = trim($_POST['description']);

        if ( empty($name) ) {
            $errors[] = 'You need to supply a title';
        }   else if ( strlen($name) > 255 ) {
        $errors[] = 'Title cannot be longer than 255 characters';
        }

        if ( empty($description) ) {
        $errors[] = 'You need to supply text';
        }

        if ( empty($price) ) {
        $errors[] = 'You need to supply text';
        }

        if ( empty($sale) ) {
        $errors[] = 'You need to supply text';
        }

        if ( empty($picture) ) {
        $errors[] = 'You need to supply text';
    }

    if (! category_exists('id', $_POST['category']) ) {
    $errors[] = 'Category does not exist';

    }

    if ( empty($errors) ) {
    edit_product($_GET['id'], $name, $description, $price, $sale, $picture, $_POST['category']);
    header("Location: ../admin/edit_products.php?id={$post[0]['post_id']}");
    die();
    }
}
?>
<div style="width:100%; height:150px; background-color:white;"><span style="font-family:saxMonoRegular; letter-spacing:2px; display:block; font-size:4.5em; text-align:center; padding-top:15px;"> Edit <?php echo $post[0]['name']; ?> </span></div>

<div class="link" style="width:100%; background-color:#ccc;">

<form action="" method="post">

<?php
if ( isset($errors) && ! empty($errors) ) {
echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
}
?>

<label for="name">Title</label>
<input type="text" name="name" value="<?php echo $post[0]['name']; ?>"><br/>
<label for="price">Price</label>
<input type="text" name="price" value="<?php echo $post[0]['price']; ?>"><br/>
<label for="sale">Sale</label>
<input type="text" name="sale" value="<?php echo $post[0]['sale']; ?>"><br/>
<label for="picture">Picture</label>
<input type="text" name="picture" value="<?php echo $post[0]['picture']; ?>"><br/>


<label for="description">Description</label>
<textarea  name="description" rows="15" cols="50"><?php echo $post[0]['description']; ?></textarea><br/>


<label for="prod_id">Category</label>
<select name="prod_id">
<?php
foreach ( get_categories() as $category ) {
$selected = ( $category['name'] == $post[0]['name'] ) ? " selected" : '';
?>

<option value="<?php echo $category['id']; ?>" <?php echo $selected; ?>> <?php echo $category['name']; ?></option>
<?php
}
?>
</select><br/>

<input class="button-link" type="submit" value="Edit Post">
</form>

</div>  

addprodcat.php

function edit_product($id, $prod_id, $prod_sub_id, $name, $description, $price, $sale, $picture, $category) {

      $id           = (int) $id;  
      $prod_id      = (int) $prod_id;
      $prod_sub_id  = (int) $prod_sub_id;
      $name         = mysql_real_escape_string($name);
      $description  = mysql_real_escape_string($description);
      $price        = mysql_real_escape_string($price);
      $sale         = mysql_real_escape_string($sale);
      $picture      = mysql_real_escape_string($picture);
      $category = (int) $category;


      mysql_query("UPDATE `products` SET 
      `cat_id`     = {$category},
      `prod_id`         = {$prod_id},
      `prod_sub_id `    = '{$prod_sub_id}',
      `name`            = '{$name}',
      `description`     = '{$description}',
      `price`           = '{$price}',
      `sale`            = '{$sale}',
      `picture`         = '{$picture}'
      WHERE `id`        = {$id}");
    echo mysql_error();
    }

2 个答案:

答案 0 :(得分:5)

您的更新表单仅将7个参数传递给edit_products函数;但是,这个函数需要9。

edit_product($_GET['id'], $name, $description, $price, $sale, $picture, $_POST['category']);

...

function edit_product($id, $prod_id, $prod_sub_id, $name, $description, $price, $sale, $picture, $category)

您还需要传递$prod_id$prod_sub_id

作为一个额外的注释,在调试代码时值得注释掉任何重定向,因为任何(非致命的)错误/警告都会被遗漏。

答案 1 :(得分:0)

功能参数的数量不同。该函数需要9但你提供了7。

这是在冗长的行中发生的常见用户错误。

使用这样的编码标准:

function edit_product(
    $id, 
    $prod_id, 
    $prod_sub_id, 
    $name, 
    $description, 
    $price, 
    $sale, 
    $picture, 
    $category
) {
    /*function code */
}

当你“调用”这个功能时也遵循相同的标准。