Home > Enterprise >  How Do I Fix the implode error that keeps coming up
How Do I Fix the implode error that keeps coming up

Time:07-09

Below is the code that I have copied as I was following a tutorial and the affected code is line 150 where it is telling me this error: Fatal error: Uncaught TypeError: implode(): Argument #2 ($array) must be of type ?array, mysqli given in C:\xampp\htdocs\shopping cart\shop_order.php:150 Stack trace: #0 C:\xampp\htdocs\shopping cart\shop_order.php(150): implode(Array, Object(mysqli)) #1 {main} thrown in C:\xampp\htdocs\shopping cart\shop_order.php on line 150

<?php
 /* Program name:  Shop_order.php
  * Description:   Processes order when it's been submitted.
  */
session_start();                                                               
include("dbinfo.inc");
if(!isset($_SESSION['order_number']))                                          
{
   echo "No order number found<br>\n
   <a href='shop_products.php'>Continue shopping</a>";
   exit();
}
if(@$_GET['from'] == "cart")                                            
{
   include("shop_form_shipinfo.inc");
   exit();
}
elseif(isset($_POST['Summary']))                                               
{
   foreach($_POST as $field => $value)                                      
   {

if ($value == "")
      {
         $blanks[] = $field;
      }
      else
      {
        $good_data[$field] = strip_tags(trim($value));
      }
   }
   if(isset($blanks))
   {
      $message = "The following fields are blank.
                  Please enter the required information:  ";
      foreach($blanks as $value)
      {
         $message .="$value, ";
      }
      extract($good_data);
      include("shop_form_shipinfo.inc");
      exit();
   }
   foreach($_POST as $field => $value)                                      
   {
     if($field != "Summary")
     {
       if(preg_match("/name/i",$field))
       {
         if (!preg_match("/^[A-Za-z' -]{1,50}$/",$value))
         {
            $errors[] = "$value is not a valid name.";
         }
       }
       if(preg_match("/street/i",$field)or
          preg_match("/addr/i",$field) or
          preg_match("/city/i",$field))
       {
         if(!preg_match("/^[A-Za-z0-9.,' -]{1,50}$/",$value))
         {
            $errors[] = "$value is not a valid address
                          or city.";
         }
       }
       if(preg_match("/state/i",$field))
       {
         if(!preg_match("/[A-Za-z]/",$value))
         {
            $errors[] = "$value is not a valid state.";
         }
       }

if(preg_match("/email/i",$field))
       {
         if(!preg_match("/^. @. \\.. $/",$value))
         {
            $errors[]="$value is not a valid email address.";
         }
       }
       if(preg_match("/zip/i",$field))
       {
         if(!preg_match("/^[0-9]{5,5}(\-[0-9]{4,4})?$/",
                        $value))
         {
            $errors[] = "$value is not a valid zipcode.";
         }
       }
       if(preg_match("/phone/i",$field))
       {
         if(!preg_match("/^[0-9)(xX -]{7,20}$/",$value))
         {
            $errors[]="$value is not a valid phone number. ";
         }
       }
       if(preg_match("/cc_number/",$field))
       {
         $value = trim($value);
         $value = ereg_replace(' ','',$value);
         $value = ereg_replace('-','',$value);
         $_POST['cc_number'] = $value;
         if($_POST['cc_type'] == "visa")
         {
           if(!preg_match("/^[4]{1,1}[0-9]{12,15}$/",$value))
           {
             $errors[]="$value is not a valid Visa number. ";
           }
         }
         elseif($_POST['cc_type'] == "mc")
         {
           if(!preg_match("/^[5]{1,1}[0-9]{15,15}$/",$value))
           {
             $errors[] = "$value is not a valid
                           Mastercard number. ";
           }
         }
         else
         {
           if(!preg_match("/^[3]{1,1}[0-9]{14,14}$/",$value))
           {
             $errors[] = "$value is not a valid
                           American Express number. ";

}
         }
       }
       $$field = strip_tags(trim($value));
     }
   }
   if(@is_array($errors))
   {
      $message = "";
      foreach($errors as $value)
      {
         $message .= $value." Please try again<br />";
      }
      include("shop_form_shipinfo.inc");
      exit();
   }                                                                           
    /* Process data when all fields are correct */
   $cxn = mysqli_connect($host,$user,$passwd,$dbname);
   foreach($_POST as $field => $value)                                      
   {
      if($field != "Summary" )                 
      {
         $value = mysqli_real_escape_string($cxn,$value);
         $updates[] = "$field = '$value'";
      }
   }
   $update_string = implode($updates,$cxn);                                     
   $sql_ship = "UPDATE CustomerOrder SET $update_string                        
          WHERE order_number='{$_SESSION['order_number']}'";
   $result = mysqli_query($cxn,$sql_ship)
                 or die(mysqli_error($cxn));
   extract($_POST);                                                            
   include("shop_page_summary.inc");
}
elseif(isset($_POST['Ship']))                                                  
{
   include("shop_form_shipinfo.inc");
}
elseif(isset($_POST['Final']))                                                 
{
   switch ($_POST['Final'])                                                    
   {
      case "Continue Shopping":                                                
         header("Location: shop_products.php");
         break;
      case "Cancel Order":                                                     
         #include("shop_page_cancel.inc");
         unset($_SESSION['order_number']);
         session_destroy();
         exit();
         break;

case "Submit Order":                                                    
         $cxn =
            mysqli_connect($host,$user,$passwd,$dbname);
         $sql = "UPDATE CustomerOrder SET submitted='yes'
           WHERE order_number='{$_SESSION['order_number']}'";
         $result = mysqli_query($cxn,$sql)
                or die("Error: ".mysqli_error($cxn));
         #processCCInfo();                                                     
         #sendOrder();                                                         
         #include("shop_page_accept.inc");                                     
         #email();                                                            
         session_destroy();                                                    
         break;
    }
}
?>

Here is the affected part of the code:

/* Process data when all fields are correct */
   $cxn = mysqli_connect($host,$user,$passwd,$dbname);
   foreach($_POST as $field => $value)                                      
   {
      if($field != "Summary" )                 
      {
         $value = mysqli_real_escape_string($cxn,$value);
         $updates[] = "$field = '$value'";
      }
   }
   $update_string = implode($updates,","); /* This is the affected area this is how it is in the tutorial I was following. */                                   
   $sql_ship = "UPDATE CustomerOrder SET $update_string                        
          WHERE order_number='{$_SESSION['order_number']}'";
   $result = mysqli_query($cxn,$sql_ship)
                 or die(mysqli_error($cxn));
   extract($_POST);                                                            
   include("shop_page_summary.inc");
}

I have tried replacing "," with different fields and I still got the error. the place is suppose to show the order summary.

CodePudding user response:

i just started learning how to code so take anything i say with a major grain of salt, but looking up implode on PHP manual it looks like - var_dump(implode(",", $array)); so maybe your "," and $updates need to swap places? again i just started so i am probably wrong

  •  Tags:  
  • php
  • Related