2

I have this query:

    $ManagerName = $mysqli->real_escape_string($_POST['ManagerName']);
    $Password = encryptIt($_POST['Password']);
    $Address = $mysqli->real_escape_string($_POST['Address']);
    $Email = $mysqli->real_escape_string($_POST['Email']);
    $PhoneNo = $mysqli->real_escape_string($_POST['PhoneNo']);
    $OfficeName = $mysqli->real_escape_string($_POST['OfficeName']);

    $ConsignmentNo = $_POST['percel'];
    $status = 'In Transit';

    $det = explode(",",$ConsignmentNo);

$sql = "INSERT INTO tbl_courier_officers (officer_name, off_pwd, address, email, ph_no, office, consignment, status, reg_date)
        VALUES ('$ManagerName', '$Password', '$Address', '$Email', '$PhoneNo', '$OfficeName','$ConsignmentNo','$status', NOW())";   

    $done = mysqli_query($mysqli, $sql);

    if($done){

    echo "added";

    foreach($det as $valuez)
    {
    $m = $Email;
    mysqli_query($mysqli,"INSERT INTO courier_track (email, percel_num) values('$m', '$valuez')");

    }

    mysqli_query($mysqli, "insert into user_log (username,name,action,time, user_id, mydate, mtime)values('$uname','$fullname','Added $ManagerName to courier officers table', '$tv', '$id', '$t', '$tv')");

    }else{
        echo 'Error occured: '.$mysqli->error;
    }

After the first insertion is true, I want to submit the second query which is a foreach loop. I want it to submit the number of time the values occurs but it is not submitting after the first insertion.

All queries are working except for the query in the foreach.

Can someone please tell me what to do?

2
  • mysqli_query returns false on failure. Check for that. If it is false, check mysqli_error (php.net/manual/en/mysqli.error.php). Error will tell you what went wrong. Commented Sep 26, 2018 at 2:33
  • try displaying the query inside foreach and manually running it on your dbms Commented Sep 26, 2018 at 3:10

1 Answer 1

1

Change this piece of code

 mysqli_query($mysqli,"INSERT INTO courier_track (email, percel_num) values('$m', '$valuez')");

with an error notification as follows

 mysqli_query($mysqli,"INSERT INTO courier_track (email, percel_num) values('$m', '$valuez')") or die(mysqli_error($mysqli));

then you will get to know what is wrong in the insert statement.

If it doesn't work put a print_r($det); and add it to the OP so that we can figure out the problem rightly

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. it says the table id doesn't have a default value; forgotten it has no auto increment. Inserting now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.