-1

嗨,我很难理解为什么这没有更新数据库。它重新加载指向的页面,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();
    }
4

2 回答 2

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

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

于 2013-01-03T14:13:57.340 回答
0

函数参数的数量不同。该函数需要 9,但您提供了 7。

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

像这样使用编码标准:

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

当您“调用”该功能时也遵循相同的标准。

于 2013-01-03T14:51:01.393 回答