2013-01-04 56 views
0

我有这个工作,然后当我添加新类别Quantity它现在产生这个错误。未定义的偏移量:0编辑功能

Undefined offset: 0 in (the form values) 

我看不到未定义的偏移量在哪里。任何帮助,将不胜感激。

我也知道mysql已折旧,但希望它在更改前工作。

作为一个说明,所有数据都被召回了对编辑的形式,它是当你处理它,它产生错误

而且查询已经过验证和运行。

编辑功能

function edit_product($id, $name, $description, $price, $quantity, $picture, $category) { 

     $id   = (int) $id; 
     $category  = (int) $category; 
     $name   = mysql_real_escape_string($name); 
     $description = mysql_real_escape_string($description); 
     $price  = mysql_real_escape_string($price); 
     $quantity  = (int) $quantity; 
     $picture  = mysql_real_escape_string($picture); 



     mysql_query("UPDATE `shop` SET 

     `prod_id`   = {$category}, 
     `name`   = '{$name}', 
     `description`  = '{$description}', 
     `price`   = '{$price}', 
     `quantity`  = '{$quantity}', 
     `picture`   = '{$picture}' 
     WHERE `id`  = {$id}"); 

    echo mysql_error(); 
    } 

编辑页面

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

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

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


     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($quantity)) { 
     $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, $quantity, $picture, $_POST['category']); 
     header("Location: ../admin/edit_products.php?id={$post[0]['post_id']}"); 
     die(); 
     } 
    } 
    ?> 

..... 

<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">Quantity</label> 
<input type="text" name="quantity" value="<?php echo $post[0]['quantity']; ?>"><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="category"> 
<?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> 


-------------- 

获取POST功能 - 根据要求。

function get_posts($id = null, $cat_id = null) { 
    $posts = array(); 

    $query ="SELECT `shop`.`id` AS `post_id` , `products_cat`.`id` AS `category_id` , `shop`.`name` , `description` , `price` , `quantity` , `picture` 
FROM `shop` 
INNER JOIN `products_cat` ON `shop`.`prod_id` = `products_cat`.`id` "; 

    if (isset($id)) { 
    $id = (int) $id; 
    $query .= " WHERE `shop`.`id` = {$id}"; 
    } 

    if (isset($cat_id)) { 
     $cat_id = (int) $cat_id; 
     $query .= " WHERE `products_cat`.`id` = {$cat_id}"; 
} 

    $query .= " ORDER BY `shop`.`price` DESC"; 

    $query = mysql_query($query); 
    echo mysql_error(); 
    while ($row = mysql_fetch_assoc($query)) { 
     $posts[] = $row; 
     } 

    return $posts; 
} 
+1

你确定它在你的编辑函数中,而不是你引用'$ post [0] ...'的地方吗? – Crontab

+1

在什么线上? –

+0

我只是看着你的代码,看到你有'prod_id = {$ category},' - 不应该是'prod_id ='{$ category}',' - 用'''? ...只需检查... –

回答

4

的第一件事情,请不要使用mysql_ ,去mysqli_

有一段时间,在上面的代码中

Undefined offset: 0 error is cooming from lines as you said 

<input type="text" name="price" value="<?php echo $post[0]['price']; ?>"> 

这是因为在指数$后的值] ['价格']尚未设置。

只是

<?php echo isset($post[0]['price'])?$post[0]['price']:''; ?> 
的价值属性

替换它。

希望它能帮助你!

+1

Brillant。感谢帮助sandip。你很耐心。是的,我转移到其他代码一旦我得到everthing排序:) – user1941674

+1

智能工作sandip。保持。你移动我们移动。 –