1

Hello so I have a table named tables that contains 9 fields (`id, first, second, third, fourth, fifth, sixth, seventh, eight) and if all of those fields aren't empty, I want to insert it as new records on the same table. This is my code for now:

    $db = new db();
        $bind = array(
        ":tid" => "1"
        );
        $results = $db->select("tables", "tableID = :tid", $bind);
            foreach ($results as $r) {
                $tableID = $r['id'];
                $l1_1 = $r['first'];
                $l1_2 = $r['second'];
                $l1_3 = $r['third'];
                $l1_4 = $r['fourth'];
                $l1_5 = $r['fifth'];
                $l1_6 = $r['sixth'];
                $l1_7 = $r['seventh'];
                $l1_8 = $r['eight'];
            }
if($l1_1 != "" AND $l1_2 != "" AND $l1_3 != "" AND $l1_4 != "" AND $l1_5 != "" AND $l1_6 != "" AND $l1_7 != "" AND $l1_8 != "") 
{
$db->insert("tables", array(
            "first" => $l1_1,
            "second" => $l1_2,
            "third" => $l1_3,
            "fourth" => $l1_4,
        ));

        //insert 2nd record
$db->insert("tables", array(
            "first" => $l1_5,
            "second" => $l1_6,
            "third" => $l1_7,
            "fourth" => $l1_8,
        ));
}

I also have a long code that the summary of what it does is to check if field is empty and insert that data there, until all fields are filled. When the last data is entered, the code above will execute. When the last data is entered, it will do the code above that will check if all fields is empty and insert a new data. This code works but the last data being inserted appears blank in the new record being inserted.

I know my explanation is vague, but please just ask me what you want to know.

2
  • Shouldn't be //insert 2nd record $db->insert("tables", array("fifth" => $l1_5, "sixth" => $l1_6, "seventh" => $l1_7, "eight" => $l1_8, )); ? Commented Jul 13, 2015 at 3:30
  • why would you have an empty row in the db ? you can check if the fields are empty in the query. Commented Jul 13, 2015 at 3:31

1 Answer 1

2

I believe your answer lies in your If statement. You are checking if the fields are "", or blank. This is incorrect as they could be NULL in the database. There is an answer out there that explains how you can resolve it.

Better way to check variable for null or empty string?

Change your code as follows:

if(isset($l1_1) && trim(($l1_1) != ''
 AND isset($l1_2) && trim(($l1_2) != ''
 AND isset($l1_3) && trim(($l1_3) != ''
 AND isset($l1_4) && trim(($l1_4) != ''
 AND isset($l1_5) && trim(($l1_5) != ''
 AND isset($l1_6) && trim(($l1_6) != ''
 AND isset($l1_7) && trim(($l1_7) != ''
 AND isset($l1_8) && trim(($l1_8) != '') 
Sign up to request clarification or add additional context in comments.

Comments

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.