-1

I'm trying to copy the row and insert into same table with new product Id number, in copy_action.php page I'm getting errors said

Undefined offset: 1 in  .... line 18
Undefined offset: 1 in  .... line 18
Undefined offset: 1 in  .... line 18

Insert code is come with array, and i'm not sure why i'm getting errors on line 18 where it said

('$product_name[$i]','$product_category[$i], $product_price[$i]')" or die(mysql_error());

please completely code and see what i missed and how to solve that please!

<?php
include('dbcon.php');
$product_id=$_POST['product_id'];
$product_name = $_POST['product_name'];
$product_category = $_POST['product_category'];
$product_price = $_POST['product_price'];


$N = count($product_id);
for($i=0; $i < $N; $i++)
{

    $order = "INSERT INTO product
       (product_name, product_category, product_price)
      VALUES
       ('$product_name[$i]','$product_category[$i], $product_price[$i]')" or die(mysql_error());


}
$result = mysql_query($order);  //order executes
if($result)
{
 echo("
Input data is succeed");
}
else
{
 echo("
Input data is fail");
}

?>
r5d
  • 579
  • 5
  • 24
user3417953
  • 113
  • 2
  • 3
  • 9
  • Please post the result of `var_dump($product_name);` placed before the `for` loop. – TheWolf Mar 29 '14 at 19:53
  • `('$product_name[$i]','$product_category[$i], $product_price[$i]')` ... a single quote `'` is missing after the `$product_category[$i]` – Tun Zarni Kyaw Mar 29 '14 at 19:55
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – CBroe Mar 29 '14 at 19:56

1 Answers1

0

Use ISSET()

Use of isset(), empty() conditions before using any suspicious variable works well.

isset — Determine if a variable is set and is not NULL

if(isset($_POST['product_id']){
$product_id=$_POST['product_id'];
}
underscore
  • 6,495
  • 6
  • 39
  • 78
  • I have tried all of options, i fixed sign quotes were missing, i put if(isset($_POST['product_id']), is not working i'm getting same errors message, then i put var_dump($product_name); i got result said array (size=1) 0 => string 'INTERNET TV/RADIO USB STICK' (length=27) and at end my error code said "Input data is fail" what did i missed! – user3417953 Mar 29 '14 at 20:31
  • You need to wrapp your all post varibles with ISSET().Not just only one – underscore Mar 29 '14 at 20:33
  • i have tried wrapp all of post variables still not working here what i put... if (isset($_POST["product_name"]) && !empty($_POST["product_name"])) if (isset($_POST["product_category"]) && !empty($_POST["product_category"])) if (isset($_POST["product_price"]) && !empty($_POST["product_price"])) , any other options! – user3417953 Mar 29 '14 at 20:52