i am working on building an intranet site for our company. basically, it is a time sheet for our hourly employees.

so far everything seems to be working ok except that i am getting extra rows inserted into my database. the data is all either null or zero on these rows. i have looked through my code and cant find my mistake. i also searched these forums, and so far havent found any posts with the same errors.

the following is a snippet of my code that writes to the database. please forgive it's sloppiness, i am learning on the fly.

for ($n=0;$n<=3;$n+=1){
if ($employee[$n]!="none"){
echo $employee[$n]." - ";
echo $hours[$n]." hours <br>";
echo "Crew Chief: ".$chief[$n]."<br>";
$cchief=$chief[$n];
$employee_insert=$employee[$n];
$hours_insert=$hours[$n];
$connection = mysql_connect($host,$user,$password)
or die ("Could not connect to server");
$db = mysql_select_db($database,$connection)
or die ("Could not select database");
$query = "INSERT INTO survey (input_date,projectname,projectnumber,employee,hours,code,codedescription,description,weather,day,crewchief)
       VALUES('$insert_date','$job[1]','$job[0]','$employee_insert','$hours_insert','$code','$code_description','$description','$weather','$day','$cchief')";
$result = mysql_query($query)
or die ("Could not execute query");
mysql_close($connection);
}
}

    the code you show
    will insert when:
    $employee[$n] where n=0,1,2,3
    if $employee[$n] is not 'none'

    so max 4 records will be inserted, and minimum 0

    the values insert will be:
    '$insert_date','$job[1]','$job[0]','$employee_insert','$hours_insert','$code','$code_description','$description','$weather','$day','$cchief'
    and from those values, all will be the same for these records
    except for: $cchief $employee_insert $hours_insert

    what does your echo show?

      the echo's that i have already built in show correctly.

      i want most of the data to be the same for all people just the variables you point out should vary.

        cyclefiend2000 wrote:

        the echo's that i have already built in show correctly.
        i want most of the data to be the same for all people just the variables you point out should vary.

        i am getting extra rows inserted into my database.
        the data is all either null or zero on these rows.

        this means that there are NULL or 0 values
        in those arrays you use:
        arrays: $employee $hours $chief
        or if you mean other values those variables also are NULL or 0

        I cant understand this, as the code you show probably is not the cause.
        What I see your code should work ..if it get correct data to insert.

        You can connect to db ONCE before the loop and close it after loop
        ... but this is another detail which will not answer your question

        $connection = mysql_connect($host,$user,$password)
        or die ("Could not connect to server");
        $db = mysql_select_db($database,$connection)
        or die ("Could not select database");
        
        for ($n=0;$n<=3;$n+=1){
        if ($employee[$n]!="none"){
        
        echo $employee[$n]." - ";
        echo $hours[$n]." hours <br>";
        echo "Crew Chief: ".$chief[$n]."<br>";
        
        
        $cchief=$chief[$n];
        $employee_insert=$employee[$n];
        $hours_insert=$hours[$n];
        
        $query = "INSERT INTO survey   
        (input_date,projectname,projectnumber,employee,hours, code,codedescription,description,weather,day,crewchief) VALUES('$insert_date','$job[1]','$job[0]','$employee_insert','$hours_insert', '$code','$code_description','$description','$weather','$day','$cchief')"; $result = mysql_query($query) or die ("Could not execute query"); } } mysql_close($connection);

          i originally had it set up that same way you suggest, and changed it thinking that maybe that would solve the problem.

          i'd be happy to post more of my code if you think that will help. i just posted the section that writes to the database thinking the problem was in there.

          most of the variables are selected from drop down lists or radio buttons. the only two that arent are $description and $weather. those are text boxes.

          the time sheet is for an entire crew and not just an individual. the sheet is filled out by the crew chief. there can be as many as 4 people on a crew and as little as 1 person. there are four drop down menus to select crew members with "none" being the default value.

          my thinking was that the if loop would execute until it got to the point there were no more people on the crew. when i echo the results to the screen this seems to be the case.

            this is weird. i just added some more data using the time sheet to the database. just like i was doing yesterday, and today no NULL rows. ??

            not sure what was going on yesterday?

            i will post back if the problem occurs again.

              cyclefiend2000 wrote:

              this is weird. i just added some more data using the time sheet to the database. just like i was doing yesterday, and today no NULL rows. ??

              not sure what was going on yesterday?

              i will post back if the problem occurs again.

              This confirms my opinion.
              It is no fault in the code you show.
              And hopefully no bad in rest of your code.
              So, you have not to post more code, unless you see some more strange things.

              Regards 🙂

                Write a Reply...