wealth ouseinfo
wealth ouseinfo

Reputation: 173

Insert query in foreach not inserting

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?

Upvotes: 2

Views: 211

Answers (1)

Rinsad Ahmed
Rinsad Ahmed

Reputation: 1943

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

Upvotes: 1

Related Questions