0

Earlier I just asked a question regarding my group and I's project.

We're encountering a problem regarding the data not being saved on the DB.

Here is our code:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
try {
    $dbh = new PDO('mysql:host=localhost;dbname='.$dbname, $username, $password);
} catch (PDOException $e) {
    print "Connection Failed: " . $e->getMessage() . "<br/>";
    die();
}

    if($_POST['statustext'] == 'Delivered') {
      $statuscode = '<div class="text-xs-center tag-" style="background-color: #4cbb87; ">';
    } elseif($_POST['statustext'] == 'Out for Delivery') {
      $statuscode = '<div class="text-xs-center tag-" style="background-color: #f5a551; ">';
    } elseif($_POST['statustext'] == 'Intransit') {
      $statuscode = '<div class="text-xs-center tag-" style="background-color: #65aee0; ">';
    } elseif($_POST['statustext'] == 'Info Received') {
      $statuscode = '<div class="text-xs-center tag-" style="background-color: #214977; ">';
    } elseif($_POST['statustext'] == 'Pending') {
      $statuscode = '<div class="text-xs-center tag-" style="background-color: #ccc; ">';
    } elseif($_POST['statustext'] == 'Shipment Hold') {
      $statuscode = '<div class="text-xs-center tag-" style="background-color: #d26759; ">';
    } elseif($_POST['statustext'] == 'Expired') {
      $statuscode = '<div class="text-xs-center tag-" style="background-color: #d26759; ">';
    }

    $stmt = $dbh->prepare("INSERT INTO customers (trackingno, datecreated, item, quantity, weight, agency, origin, destination, statustext, statuscode, timeline) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->bindParam(1, $trackingno);
    $stmt->bindParam(2, $date);
    $stmt->bindParam(3, $item);
    $stmt->bindParam(4, $quantity);
    $stmt->bindParam(5, $weight);
    $stmt->bindParam(6, $agency);
    $stmt->bindParam(7, $origin);
    $stmt->bindParam(8, $destination);
    $stmt->bindParam(9, $statustext);
    $stmt->bindParam(10, $statuscode);
    $stmt->bindParam(11, $timeline);
    $arr = $_POST;

    for($i = 0; $i <= count($arr['trackingno'])-1;$i++){
      $trackingno = $arr['trackingno'][$i];
      $date = date("Y-m-d H:i:s");
      $item = $arr['item'][$i];
      $quantity = $arr['quantity'][$i];
      $weight = $arr['weight'][$i];
      $agency = $arr['agency'][$i];
      $origin = $arr['origin'][$i];
      $destination = $arr['destination'][$i];
      $statustext = $arr['statustext'][$i];
      $timeline = $arr['timeline'][$i];
    }
      $stmt->execute();

      if($stmt){
        header("Location: ./add.php?add=success");
      } else{
        header("Location: ./add.php?add=failed");
      }
?>

There is no error in the code being encountered when were debugging but the problem is the data is not being saved on the DB. What seems to be the problem.

Edit: The problem is from the $statuscode which is the loop at the top for it. The error I get is Notice: Array to string conversion. How can I possibly solve that.

UG-Clarck
  • 5
  • 3

0 Answers0