0

i am trying to remove the old value of an array i searched over the internet i just found the method unset() using this solution but my case it seems different some how here is my full code

<?php



header('Content-type: application/json; charset=utf-8');
$link = mysqli_connect("localhost","root","");
mysqli_query($link,"SET NAMES UTF8");
$db= mysqli_select_db($link,"Mirsa") or die("Cannot select Database.");



 if (isset($_GET['Product_ID']) ){
   $productId;
    $Menu_ID =$_GET['Product_ID'];



$query = "SELECT 
   mirsaProductId,mirsaProductCode,mirsaProductDescription,
   mirsaProductPcsInCartoon,mirsaProductWeight,mirsaProductVolume,
    mirsaProductCodeBType,mirsaProductCodeCType,FK_mirsaCategoryId 
   from mirsaProduct WHERE FK_mirsaCategoryId='$Menu_ID'";



    $result = mysqli_query($link,$query) or die(mysql_error($link));

                $response["Products"] = array();
                $products = array();
         while ($row = mysqli_fetch_array($result)) {
                $image[]=array()         
                $products["mirsaProductId"] =          $row["mirsaProductId"];
                $products["mirsaProductCode"]         = $row["mirsaProductCode"];
                $products["mirsaProductDescription"]  = $row["mirsaProductDescription"];
                $products["mirsaProductPcsInCartoon"] = $row["mirsaProductPcsInCartoon"];
                $products["mirsaProductWeight"] =      $row["mirsaProductWeight"];
                $products["mirsaProductVolume"] =       $row["mirsaProductVolume"];
                $products["mirsaProductCodeBType"] =   $row["mirsaProductCodeBType"];
                $products["mirsaProductCodeCType"] =   $row["mirsaProductCodeCType"];
                $productId = $row["mirsaProductId"];




$query2 = "SELECT mirsaProductUrlImage FROM mirsaProduct,mirsaProductImages
WHERE FK_mirsaProductId = '$productId' GROUP BY mirsaProductUrlImage";     

    $result2=mysqli_query($link, $query2);
    while ($row2 = mysqli_fetch_array($result2))
      {
             $image [] = $row2["mirsaProductUrlImage"];
             $products["mirsaProductUrlImage"]=$image;
      }
     array_push($response["Products"], $products);





        }
                  die(json_encode($response));


  }

// echoing JSON response

 else {
// no products found
$response["success"] = 0;
$response["message"] = "No Products";
   die(json_encode($response));
 }
    ?>

i am getting a response of this way

  {
 ProductId: "1",
 ProductCode: "118.023",
 ProductDescription: "NEM OVAL (Transparent)",
 ProductPcsInCartoon: "10",
 ProductWeight: "7.55 - 8.05(KG)",
 ProductVolume: "0.104(m3)",
 ProductCodeBType: "NA",
 ProductCodeCType: "NA",
 ProductUrlImage: [
  "1.png",
  "2.png"
 ]
 },


{
    ProductId: "2",
    ProductCode: "118.024",
    ProductDescription: "NEM OVAL (Opac)",
    ProductPcsInCartoon: "10",
    ProductWeight: "7.55 - 8.05(KG)",
    ProductVolume: "7.55 - 8.05(KG)",
    ProductCodeBType: "NA",
    ProductCodeCType: "NA",
    ProductUrlImage: [
            "1.png",
            "2.png"
          ]

the second JSON object is not containing these 2 url of image it takes the old value of the $image[] so i need to reinitialised or empty his value in a way i need your help guys you are always our support

Community
  • 1
  • 1
Zakaria Darwish
  • 358
  • 5
  • 22
  • 1
    Your code is vulnerable to [**SQL injection attacks**](https://en.wikipedia.org/wiki/SQL_injection). You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Apr 26 '17 at 17:59
  • i know its just an api getting json value for my iOS app no transaction no secret info...lets focus on how to solve my problem then i will update my code – Zakaria Darwish Apr 26 '17 at 18:01
  • If I understood the question right, you don't need an $image array there. So instead this -> $image [] = $row2["mirsaProductUrlImage"]; $products["mirsaProductUrlImage"]=$image; Use this -> $products["mirsaProductUrlImage"]=$row2["mirsaProductUrlImage"]; – Оzgur Apr 26 '17 at 18:10
  • no mate i need the array[] but i need to empty the array on each time he end the loop – Zakaria Darwish Apr 26 '17 at 18:11

1 Answers1

0

I am going to take a stab. Since you never really initialize $image to be an array. Do you actually intend for:

$image[] = array()

to be:

$image = array();

That would cause the $image array to be reset each time through the first while loop.

Dan
  • 10,614
  • 5
  • 24
  • 35