1

I am having an issue. I have a bunch of inputs that share the same name. This creates arrays which are placed into the database and that all works fine but I am having a major dilemma in trying to keep the blank rows of inputs from creating blank entries in the database table.

The code is as such:

foreach($_POST['datetime'] as $dbrow=>$startdate) {
if(isset($_POST['def'])) {
$start = $startdate; 
$abc = $_POST['abc'][$dbrow];
$def = $_POST['def'][$dbrow];
$ghi = $_POST['ghi'][$dbrow];


$db_insert = "INSERT INTO tablename (start, abc, def, ghi) VALUES('$start', '$abc', '$def', '$ghi')";

This is fine and dandy but I cant get the if statement to work. The form that is using POST to get the users information has 5 rows of inputs each with fours columns (start, abc, def, ghi). If I enter data in the form and submit it, then all the data goes to the database (yay - success), if I only enter the data in rows 1-4 then the database still enters 5 rows of data. What am I doing wrong?

---------------------EDIT--------------------- Ok so upon a deeper look what appears to be happening is the code keeps wanting to submit ALL of rows of dynamically generated inputs whether they contain content or not. So I devised the following code:

$db_tur = count(array_filter($start_date));
for($db_total_used_rows=0;$db_total_used_rows<=$db_tur;$db_total_used_rows++){


$db_start_date = $_POST['datetime'][$db_total_used_rows]; 
$db_abc = $_POST['abc'][$db_total_used_rows];
$db_def = $_POST['def'][$db_total_used_rows];
$db_ghi = $_POST['ghi'][$db_total_used_rows];


}

$db_insert = .....;

In theory what this does is $db_tur counts up all of the inputs being used under the start_date variable. I then break it into a count array which should be able to be used as $db_total_used_rows. However this doesnt seem to be limiting the total number of rows of inputs being inserted into the database. So I guess its back to the drawing board unless someone else has a better idea of how to accomplish this. I'm so ready to give up right now.

4
  • Why are you checking if if(isset($_POST['def'])) is set? Why not just if(isset($startdate))? Commented Oct 8, 2013 at 18:03
  • 1
    why dont you check the values coming in & if they are empty, skip running the query for them. Commented Oct 8, 2013 at 18:03
  • There are some SQL injection vulnerabilities in this code... Commented Oct 8, 2013 at 18:05
  • Thanks for the quick responses guys. I'll try out some of the answers. @ Charles, ya I just retyped a bunch of the code just to give a jist of what was going on it isnt the finalized secured code. Commented Oct 8, 2013 at 18:16

2 Answers 2

1

Use this loop structure:

foreach ($_POST['datetime'] as $dbrow => $start) {
    $abc = $_POST['abc'][$dbrow];
    $def = $_POST['def'][$dbrow];
    $ghi = $_POST['ghi'][$dbrow];
    if (!empty($abc) && !empty($def) && !empty($ghi) && !empty($start)) {
        $db_insert = ...;
        ...
    }
}

You could change && to || if it's OK for the user to leave some of the fields blank -- it will only skip rows where all fields are blank.

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

7 Comments

Just to clarify, are you suggesting removing the part of the loop where I am adding a row count to each POSTED variable? Ie. removing the $abc = $_POST['abc'][$dbrow];
Awesome, can't wait to try it out!
Ok the good news is that it partially worked. There are no longer any blank entries being made. However it still is producing a minimum of five records. I have a sneaking suspicion that this is because of the fact that the rows of inputs are generated through a loop with a minimum number of rows being generated on page load (5).
If it's creating 5 records, but not any blank records, what's in the other records if the user only fills in one of the records?
Using the code that you provided it replicates the data on all five records. If I fill out two rows of inputs then the first row will enter correctly but the second row will be replicated across the other 4 records.
|
0

Except for checkboxes, all form fields will POST, even when empty. So if def is a text box it will always be present in your POST. A better test would be

if(!empty($_POST['def'])) {

1 Comment

That only checks if all rows are blank. It doesn't filter out individual blank rows.

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.