0

I am stuck while inserting data to my mysql table using a foreach loop to insert multiple rows at a time.

foreach($event as $booking){
    
    $startDate = $booking[DTSTART];
    $checkIn = strtotime($startDate[value]);
    
    $check_in = date("Y-m-d", $checkIn);
    
    $endDate = $booking[DTEND];
    $checkOut = strtotime($endDate[value]);
    
    $check_out = date("Y-m-d", $checkOut);
    
    $booking_source = "Airbnb";
    
    $source_id = "2";
    
    $summary = $booking[SUMMARY];
    
    $description = $booking[DESCRIPTION];
    
    $uid = $booking[UID];
    
    $sql = "INSERT INTO test_ics (uid, check_in, check_out, summary, description, source, source_id) VALUES (?,?,?,?,?,?,?)";
            
    $result = $conn->prepare($sql);
    $result->bind_param('sssssss', $uid, $check_in, $check_out, $summary, $description, $booking_source, $source_id);
    $result->execute();
    echo "<br>";
    echo "$check_in";
}

The code is working fine and inserting all the rows when I remove description and summary fields from the table where both of them are VARCHAR(2550) but when I am trying to run the code to insert rows with description and summary (like it is in the code above) its inserting only 10 rows.

Thanks

Notice: Use of undefined constant DTSTART - assumed 'DTSTART' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 40

Notice: Use of undefined constant value - assumed 'value' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 41

Notice: Use of undefined constant DTEND - assumed 'DTEND' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 45

Notice: Use of undefined constant value - assumed 'value' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 46

Notice: Use of undefined constant SUMMARY - assumed 'SUMMARY' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 54

Notice: Use of undefined constant DESCRIPTION - assumed 'DESCRIPTION' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 56

Notice: Use of undefined constant UID - assumed 'UID' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 58

19
  • 2
    Best guess is that there is some character in description or summary which is causing the query to fail. To monitor that in the foreach check the status of the sql insertion and if it fails in an array push the error message and print that array after the foreach. You can then check the exact cause and then fix it accordingly... Commented Mar 12, 2018 at 12:10
  • Hard to know really unless we can see the value of $event. How many records are you expecting to be inserted? Commented Mar 12, 2018 at 12:11
  • VARCHAR(2550) :/ just use TEXT Commented Mar 12, 2018 at 12:13
  • Add a var_dump($event); to check the values. Maybe the summary/desc is bigger than you table field? Commented Mar 12, 2018 at 12:13
  • 1
    $result = $conn->prepare($sql); $result->bind_param('sssssss', $uid, $check_in, $check_out, $summary, $description, $booking_source, $source_id); $result->execute();...any one of these three statements could fail and return false (or throw an exception, depending on your config) but you don't seem to be testing any of them (and then checking the value of $conn->error), either that or you're suppressing exceptions somewhere Commented Mar 12, 2018 at 12:23

2 Answers 2

1

You can remove the notices by putting the string indexes in quotes like e.g. this $booking['DTSTART'].

To catch the database errors, I assume you are using exceptions to report them. This code catches and prints the exception:

    try {
        $result = $conn->prepare($sql);
        $result->bind_param('sssssss', $uid, $check_in, $check_out, $summary, $description, $booking_source, $source_id);
        $result->execute();
    }
    catch (Exception $e)
    {
        echo $e->getMessage();
    }

My guess is it will reveal data that is too long for the column.

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

Comments

1

If your fields name is not constant then you have to write it with single quotes

foreach($event as $booking){
  $startDate = $booking['DTSTART'];
  $checkIn = strtotime($startDate['value']);

  $check_in = date("Y-m-d", $checkIn);

  $endDate = $booking['DTEND'];
  $checkOut = strtotime($endDate['value']);

  $check_out = date("Y-m-d", $checkOut);

  $booking_source = "Airbnb";

  $source_id = "2";

  $summary = $booking['SUMMARY'];

  $description = $booking['DESCRIPTION'];

  $uid = $booking['UID'];

  $sql = "INSERT INTO test_ics (uid, check_in, check_out, summary, description, source, source_id) VALUES (?,?,?,?,?,?,?)";

  $result = $conn->prepare($sql);
  $result->bind_param('sssssss', $uid, $check_in, $check_out, $summary, $description, $booking_source, $source_id);
  $result->execute();
  echo "<br>";
  echo "$check_in";
} 

4 Comments

perhaps worth highlighting exactly what you changed? It may not be immediately obvious to the reader. Also you missed some (value, UID) so I've altered that, and corrected your formatting error. And actually double or single quotes would be fine here, the important thing is to turn it from a constant into a string.
Hi @ADyson thanks for your help but the issue was in the file I am getting data from. Actually, DESCRIPTION was not available in all the rows and also it was NULL in some of the rows that's why I was getting the errors. I have added NULL as default value in my DB and also added if(isset) in my PHP to fix the issue. Thanks anyways for your help.
@RajanBenipuri No problem. But my advice was relevant. If you'd been checking SQL errors you'd have spotted that earlier because SQL would have told you that you couldn't insert a NULL value into that field. And it seems I was correct in assuming that a particular row (or rows) was at fault, hence why it added 10 and then stopped.
Indeed your advice was relevant and useful at the same time. Thanks

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.