2013-01-04 4 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> 


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

포스트 기능를 가져옵니다 - 요청으로.

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

브릴 런트. 도움말 sandip 주셔서 감사합니다. 너는 참을성이있어. 그리고 내가 영원히 정렬되면 다른 코드로 넘어갑니다. – user1941674

+1

똑똑한 작업 sandip. 그것을 지키십시오. 움직여서 움직여. –

관련 문제