Jump to content

insert data on different rows


davanderbilt

Recommended Posts

I am still relatively new to all the intricacies of PHP and am having a problem.

 

I have 10 different students (S1-S10) from the same school who all are given the same foods for lunch on a particular date. 

 

They each get the same entree, condiment (like ketchup), grain, fruit, veggie1, veggie2, beverage1 and beverage2.

 

I need to be able to enter the amount of each item consumed (like 8 tater tots for the grain) and proportion consumed (i.e. None, 25%, 50%, 100%, other, etc.) for each student.  The proportion consumed can be a drop down but it needs to allow you to type in a value if over 200%.

 

This data then needs to go into an Access table with each student (S1-S10) as a separate row.

 

Here are my Access fields in order:

schoolid (number) 1001=Burlington, 1002=CCC, 1003=Maple Woods

date (date/time)

student (number) S1=1, S2=2, S3=3 and so on

veg1 (text)

veg1amtconsumed (text)

veg1proportion (text)

veg2 (text)

veg2amtconsumed (text)

veg2proportion (text)

entree (text)

entreeamtconsumed (text)

entreeproportion (text)

condiment (text)

condimentamtconsumed (text)

condimentproportion (text)

bev1 (text)

bev1amtconsumed (text)

bev1proportion (text)

bev2 (text)

bev2amtconsumed (text)

bev2proportion (text)

grain (text)

grainamtconsumed (text)

grainproportion (text)

fruit (text)

fruitamtconsumed (text)

fruitproportion (text)

 

I haven't found any way to do this other than making people enter S1's information individually on the page, then go back and enter S2's and repeat the back and forth process for all 10 students, which is a pain.  Can anyone help me out with this?

Link to comment
Share on other sites

On way could be to add a multiple selection list

 

<select name='student[]' size='10' multiple>
  <option value='1'>Student 1</option>
  <option value='2'>Student 2</option>
  <option value='3'>Student 3</option>
  <option value='4'>Student 4</option>
  <option value='5'>Student 5</option>
  <option value='6'>Student 6</option>
  <option value='7'>Student 7</option>
  <option value='8'>Student 8</option>
  <option value='9'>Student 9</option>
  <option value='10'>Student 10</option>
</select>

 

then insert a record for each selected student

 

<?php
foreach  ($_POST['student'] as $studentid) {
    // insert record for studentid
}
?>

Link to comment
Share on other sites

I already had a select list, but I changed the code to match what you advised, so it looks like this:

<form name="lunch" action="lunch_add_multiple.php" method="post">
<script type="text/javascript" language="JavaScript"><!-- --></script>
<title>Lunch Consumption Form</title>

  <b><u>Lunch Consumption Monitoring Form</u></b>
  <table width="88%" border="0">
    <tr>
      <td nowrap bordercolor="#000000"><div align="center" class="style1">
          <p align="justify" class="style8">
           DATES must be in this Format: (01/01/07)<BR>
           --------------------------------------------</p>
          
          <p align="justify"><strong>School: </strong>
            <select name="schoolid">
                <option value="1001">Burlington</option>
                <option value="1002">CCC</option>
                <option value="1003">Maple Woods</option>
            </select>
          </strong>
              <strong>Date:</strong>
            <input name="date" type="text" id="date" size="10" maxlength="8">
          </p>
          <p align="justify"><strong>Entree: </strong>        
            <input name="entree" type="text" id="entree" size="40" maxlength="50">
                <strong>Condiment:</strong> 
            <input name="condiment" type="text" id="condiment" size="40" maxlength="50">
          </p>
          <p align="justify"><strong>Grain: </strong>         
            <input name="grain" type="text" id="grain" size="40" maxlength="50">
                 <strong>Fruit:</strong>           
            <input name="fruit" type="text" id="fruit" size="40" maxlength="50">
          </p>
          <p align="justify"><strong>Vegetable 1: </strong>
            <input name="veg1" type="text" id="veg1" size="40" maxlength="50">
                <strong>Vegetable 2:</strong>
            <input name="veg2" type="text" id="veg2" size="40" maxlength="50">
          </p>
          <p align="justify"><strong>Beverage 1: </strong>
            <input name="bev1" type="text" id="bev1" size="40" maxlength="50">
                 <strong>Beverage 2:</strong>
            <input name="bev2" type="text" id="bev2" size="40" maxlength="50">
          </p>
          <br>
          <table width="100%">
        	<tr>
        		<td width="10%"><b>Student</b></td>
        		<td width="20%"> </td>
        		<td width="45%"><b>Amount Consumed</b></td>
        		<td width="35%"><b>Proportion Consumed (in %)</b></td>
        	</tr>
        	<tr>
          		<td align="left">
          		<select name='student[]' size='10' multiple>
                <option value='1'>S1</option>
                <option value='2'>S2</option>
                <option value='3'>S3</option>
                <option value='4'>S4</option>
                <option value='5'>S5</option>
                <option value='6'>S6</option>
                <option value='7'>S7</option>
                <option value='8'>S8</option>
                <option value='9'>S9</option>
                <option value='1-'>S10</option>
            </select>
            	<td align="left">Entree</td>
            	<td><input name="entreeamtconsumed" type="text" id="entreeamtconsumed" size="40" maxlength="50"></td>
            	<td><input name="entreeproportion" type="text" id="entreeproportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Fruit</td>
            	<td><input name="fruitamtconsumed" type="text" id="fruitamtconsumed" size="40" maxlength="50"></td>
            	<td><input name="fruitproportion" type="text" id="fruitproportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Vegetable 1</td>
            	<td><input name="veg1amtconsumed" type="text" id="veg1amtconsumed" size="40" maxlength="50"></td>
            	<td><input name="veg1proportion" type="text" id="veg1proportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Vegetable 2</td>
            	<td><input name="veg2amtconsumed" type="text" id="veg2amtconsumed" size="40" maxlength="50"></td>
            	<td><input name="veg2proportion" type="text" id="veg2proportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Grain</td>
            	<td><input name="grainamtconsumed" type="text" id="grainamtconsumed" size="40" maxlength="50"></td>
            	<td><input name="grainproportion" type="text" id="grainproportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Condiment</td>
            	<td><input name="condimentamtconsumed" type="text" id="condimentamtconsumed" size="40" maxlength="50"></td>
            	<td><input name="condimentproportion" type="text" id="condimentproportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Beverage 1</td>
            	<td><input name="bev1amtconsumed" type="text" id="bev1amtconsumed" size="40" maxlength="50"></td>
            	<td><input name="bev1proportion" type="text" id="bev1proportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Beverage 2</td>
            	<td><input name="bev2amtconsumed" type="text" id="bev2amtconsumed" size="40" maxlength="50"></td>
            	<td><input name="bev2proportion" type="text" id="bev2proportion" size="2" maxlength="3"></td>
            </tr>
          </table>
	  <!--<p align="justify"> </p>
          <p align="right">ENTER
            <input name="enter" type="text" id="enter" size="10" maxlength="5">
          </p>
          <p align="right">REVIEWED
            <input name="reviewed" type="text" id="reviewed" size="10" maxlength="5">
          </p>-->
          <p align="justify">
            <input type = "hidden" name = "action_h" value = "save">
		<input type = "hidden" name = "user_id" value = "<?php echo $user_id ?>">
          
            <input name="submit" type = "submit" class="style13" value="SUBMIT">
	  </p>
          <p align="justify"> </p>
      </div>
  </td>
    </tr>
  </table>
  <p> </p>
</form>

 

My insert statement now looks like this:

$conn = odbc_connect("YKIDS","","");

foreach ($_POST['student'] as $student) {
$insert_statement = "
insert into 
	lunch_consumption 
	values ($schoolid, '$date', $student, '$veg1', '$veg1amtconsumed', 
'$veg1proportion', '$veg2', '$veg2amtconsumed', '$veg2proportion', '$entree', 
'$entreeamtconsumed', '$entreeproportion', '$condiment', '$condimentamtconsumed', 
'$condimentproportion', '$bev1', '$bev1amtconsumed', '$bev1proportion', '$bev2', 
'$bev2amtconsumed', '$bev2proportion', '$grain', '$grainamtconsumed', '$grainproportion', 
'$fruit', '$fruitamtconsumed', '$fruitproportion')";
}	

$result = odbc_exec($conn,$insert_statement);

 

When I do try to submit, I get this SQL error:

Warning: Invalid argument supplied for foreach() in c:\program files\apache 
group\apache\htdocs\yhealthykids\lunch_add_multiple.php on line 121

Link to comment
Share on other sites

Could be no students were selected.

 

Change to

 

$conn = odbc_connect("YKIDS","","");

if (isset($_POST['student'])) {
   foreach ($_POST['student'] as $student) {
      $insert_statement = "
insert into 
	lunch_consumption 
		values ($schoolid, '$date', $student, '$veg1', '$veg1amtconsumed', 
      '$veg1proportion', '$veg2', '$veg2amtconsumed', '$veg2proportion', '$entree', 
      '$entreeamtconsumed', '$entreeproportion', '$condiment', '$condimentamtconsumed', 
      '$condimentproportion', '$bev1', '$bev1amtconsumed', '$bev1proportion', '$bev2', 
      '$bev2amtconsumed', '$bev2proportion', '$grain', '$grainamtconsumed', '$grainproportion', 
      '$fruit', '$fruitamtconsumed', '$fruitproportion')";

      $result = odbc_exec($conn,$insert_statement);   // needs to be in the loop
   }	
}

Link to comment
Share on other sites

OK.  I added the if statement and moved $result inside the loop and it isn't giving the error any more, but it isn't going into the database either.

 

My first attempt at this form had me entering a student, then going back to the form and entering each other student individually.  It was going into the database.

 

Full insert text is:

<?php
echo '<br><table><tr><td><b>';
foreach ($HTTP_POST_VARS as $key => $value) {
$temp = stripslashes($value);
$HTTP_POST_VARS[$key] = $temp;
}
echo '</td></tr></table></b>';

$schoolid=isset($HTTP_POST_VARS['schoolid']) ? $HTTP_POST_VARS['schoolid'] :'NA';
$date=isset($HTTP_POST_VARS['date']) ? $HTTP_POST_VARS['date'] :'00/00/0000';
//$student=isset($HTTP_POST_VARS['student']) ? $HTTP_POST_VARS['student'] :'NA';
$veg1=isset($HTTP_POST_VARS['veg1']) ? $HTTP_POST_VARS['veg1'] : 0;
$veg1amtconsumed=isset($HTTP_POST_VARS['veg1amtconsumed']) ? $HTTP_POST_VARS['veg1amtconsumed'] : 0;
$veg1proportion=isset($HTTP_POST_VARS['veg1proportion']) ? $HTTP_POST_VARS['veg1proportion'] : 0;
$veg2=isset($HTTP_POST_VARS['veg2']) ? $HTTP_POST_VARS['veg2'] : 0;
$veg2amtconsumed=isset($HTTP_POST_VARS['veg2amtconsumed']) ? $HTTP_POST_VARS['veg2amtconsumed'] : 0;
$veg2proportion=isset($HTTP_POST_VARS['veg2proportion']) ? $HTTP_POST_VARS['veg2proportion'] : 0;
$entree=isset($HTTP_POST_VARS['entree']) ? $HTTP_POST_VARS['entree'] : 0;
$entreeamtconsumed=isset($HTTP_POST_VARS['entreeamtconsumed']) ? $HTTP_POST_VARS['entreeamtconsumed'] : 0;
$entreeproportion=isset($HTTP_POST_VARS['entreeproportion']) ? $HTTP_POST_VARS['entreeproportion'] : 0;
$condiment=isset($HTTP_POST_VARS['condiment']) ? $HTTP_POST_VARS['condiment'] : 0;
$condimentamtconsumed=isset($HTTP_POST_VARS['condimentamtconsumed']) ? $HTTP_POST_VARS['condimentamtconsumed'] : 0;
$condimentproportion=isset($HTTP_POST_VARS['condimentproportion']) ? $HTTP_POST_VARS['condimentproportion'] : 0;
$bev1=isset($HTTP_POST_VARS['bev1']) ? $HTTP_POST_VARS['bev1'] : 0;
$bev1amtconsumed=isset($HTTP_POST_VARS['bev1amtconsumed']) ? $HTTP_POST_VARS['bev1amtconsumed'] : 0;
$bev1proportion=isset($HTTP_POST_VARS['bev1proportion']) ? $HTTP_POST_VARS['bev1proportion'] : 'NA';
$bev2=isset($HTTP_POST_VARS['bev2']) ? $HTTP_POST_VARS['bev2'] : 0;
$bev2amtconsumed=isset($HTTP_POST_VARS['bev2amtconsumed']) ? $HTTP_POST_VARS['bev2amtconsumed'] : 0;
$bev2proportion=isset($HTTP_POST_VARS['bev2proportion']) ? $HTTP_POST_VARS['bev2proportion'] : 'NA';
$grain=isset($HTTP_POST_VARS['grain']) ? $HTTP_POST_VARS['grain'] : 0;
$grainamtconsumed=isset($HTTP_POST_VARS['grainamtconsumed']) ? $HTTP_POST_VARS['grainamtconsumed'] : 0;
$grainproportion=isset($HTTP_POST_VARS['grainproportion']) ? $HTTP_POST_VARS['grainproportion'] : 'NA';
$fruit=isset($HTTP_POST_VARS['fruit']) ? $HTTP_POST_VARS['fruit'] : 0;
$fruitamtconsumed=isset($HTTP_POST_VARS['fruitamtconsumed']) ? $HTTP_POST_VARS['fruitamtconsumed'] : 'NA';
$fruitproportion=isset($HTTP_POST_VARS['fruitproportion']) ? $HTTP_POST_VARS['fruitproportion'] : 'NA';

$conn = odbc_connect("YKIDS","","");

if (isset($_POST['student'])) {
foreach ($_POST['student'] as $student) {
$insert_statement = "
insert into 
	lunch_consumption 
	values ($schoolid, '$date', $student, '$veg1', '$veg1amtconsumed', '$veg1proportion', '$veg2', '$veg2amtconsumed', '$veg2proportion', '$entree', '$entreeamtconsumed', '$entreeproportion', '$condiment', '$condimentamtconsumed', '$condimentproportion', '$bev1', '$bev1amtconsumed', '$bev1proportion', '$bev2', '$bev2amtconsumed', '$bev2proportion', '$grain', '$grainamtconsumed', '$grainproportion', '$fruit', '$fruitamtconsumed', '$fruitproportion')";

//echo $insert_statement;
//if($error_rep==0)
//{
$result = odbc_exec($conn,$insert_statement);
}
}

Link to comment
Share on other sites

It didn't give an error code, just a blank screen...but it didn't go into the database either.  I know  that the odbc call is correct.  I have the html form and the php insert on different pages.

 

Here's the form:

<form name="lunch" action="lunch_add_multiple.php" method="post">
<script type="text/javascript" language="JavaScript"><!-- --></script>
<title>Lunch Consumption Form</title>

  <b><u>Lunch Consumption Monitoring Form</u></b>
  <table width="88%" border="0">
    <tr>
      <td nowrap bordercolor="#000000"><div align="center" class="style1">
          <p align="justify" class="style8">
           DATES must be in this Format: (01/01/07)<BR>
           --------------------------------------------</p>
          
          <p align="justify"><strong>School: </strong>
            <select name="schoolid">
                <option value="1001">Burlington</option>
                <option value="1002">CCC</option>
                <option value="1003">Maple Woods</option>
            </select>
          </strong>
              <strong>Date:</strong>
            <input name="date" type="text" id="date" size="10" maxlength="8">
          </p>
          <p align="justify"><strong>Entree: </strong>        
            <input name="entree" type="text" id="entree" size="40" maxlength="50">
                <strong>Condiment:</strong> 
            <input name="condiment" type="text" id="condiment" size="40" maxlength="50">
          </p>
          <p align="justify"><strong>Grain: </strong>         
            <input name="grain" type="text" id="grain" size="40" maxlength="50">
                 <strong>Fruit:</strong>           
            <input name="fruit" type="text" id="fruit" size="40" maxlength="50">
          </p>
          <p align="justify"><strong>Vegetable 1: </strong>
            <input name="veg1" type="text" id="veg1" size="40" maxlength="50">
                <strong>Vegetable 2:</strong>
            <input name="veg2" type="text" id="veg2" size="40" maxlength="50">
          </p>
          <p align="justify"><strong>Beverage 1: </strong>
            <input name="bev1" type="text" id="bev1" size="40" maxlength="50">
                 <strong>Beverage 2:</strong>
            <input name="bev2" type="text" id="bev2" size="40" maxlength="50">
          </p>
          <br>
          <table width="100%">
        	<tr>
        		<td width="10%"><b>Student</b></td>
        		<td width="20%"> </td>
        		<td width="45%"><b>Amount Consumed</b></td>
        		<td width="35%"><b>Proportion Consumed (in %)</b></td>
        	</tr>
        	<tr>
          		<td align="left">
          		<select name='student[]' size='10' multiple>
                <option value='1'>S1</option>
                <option value='2'>S2</option>
                <option value='3'>S3</option>
                <option value='4'>S4</option>
                <option value='5'>S5</option>
                <option value='6'>S6</option>
                <option value='7'>S7</option>
                <option value='8'>S8</option>
                <option value='9'>S9</option>
                <option value='1-'>S10</option>
            </select>
            	<td align="left">Entree</td>
            	<td><input name="entreeamtconsumed" type="text" id="entreeamtconsumed" size="40" maxlength="50"></td>
            	<td><input name="entreeproportion" type="text" id="entreeproportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Fruit</td>
            	<td><input name="fruitamtconsumed" type="text" id="fruitamtconsumed" size="40" maxlength="50"></td>
            	<td><input name="fruitproportion" type="text" id="fruitproportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Vegetable 1</td>
            	<td><input name="veg1amtconsumed" type="text" id="veg1amtconsumed" size="40" maxlength="50"></td>
            	<td><input name="veg1proportion" type="text" id="veg1proportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Vegetable 2</td>
            	<td><input name="veg2amtconsumed" type="text" id="veg2amtconsumed" size="40" maxlength="50"></td>
            	<td><input name="veg2proportion" type="text" id="veg2proportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Grain</td>
            	<td><input name="grainamtconsumed" type="text" id="grainamtconsumed" size="40" maxlength="50"></td>
            	<td><input name="grainproportion" type="text" id="grainproportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Condiment</td>
            	<td><input name="condimentamtconsumed" type="text" id="condimentamtconsumed" size="40" maxlength="50"></td>
            	<td><input name="condimentproportion" type="text" id="condimentproportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Beverage 1</td>
            	<td><input name="bev1amtconsumed" type="text" id="bev1amtconsumed" size="40" maxlength="50"></td>
            	<td><input name="bev1proportion" type="text" id="bev1proportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Beverage 2</td>
            	<td><input name="bev2amtconsumed" type="text" id="bev2amtconsumed" size="40" maxlength="50"></td>
            	<td><input name="bev2proportion" type="text" id="bev2proportion" size="2" maxlength="3"></td>
            </tr>
          </table>
	  <!--<p align="justify"> </p>
          <p align="right">ENTER
            <input name="enter" type="text" id="enter" size="10" maxlength="5">
          </p>
          <p align="right">REVIEWED
            <input name="reviewed" type="text" id="reviewed" size="10" maxlength="5">
          </p>-->
          <p align="justify">
            <input type = "hidden" name = "action_h" value = "save">
		<input type = "hidden" name = "user_id" value = "<?php echo $user_id ?>">
          
            <input name="submit" type = "submit" class="style13" value="SUBMIT">
	  </p>
          <p align="justify"> </p>
      </div>
  </td>
    </tr>
  </table>
  <p> </p>
</form>

 

And here's the lunch_add_multiple.php it references to add into the database:

<?php
echo '<br><table><tr><td><b>';
foreach ($HTTP_POST_VARS as $key => $value) {
$temp = stripslashes($value);
$HTTP_POST_VARS[$key] = $temp;
}
echo '</td></tr></table></b>';

$schoolid=isset($HTTP_POST_VARS['schoolid']) ? $HTTP_POST_VARS['schoolid'] :'NA';
$date=isset($HTTP_POST_VARS['date']) ? $HTTP_POST_VARS['date'] :'00/00/0000';
//$student=isset($HTTP_POST_VARS['student']) ? $HTTP_POST_VARS['student'] :'NA';
$veg1=isset($HTTP_POST_VARS['veg1']) ? $HTTP_POST_VARS['veg1'] : 0;
$veg1amtconsumed=isset($HTTP_POST_VARS['veg1amtconsumed']) ? $HTTP_POST_VARS['veg1amtconsumed'] : 0;
$veg1proportion=isset($HTTP_POST_VARS['veg1proportion']) ? $HTTP_POST_VARS['veg1proportion'] : 0;
$veg2=isset($HTTP_POST_VARS['veg2']) ? $HTTP_POST_VARS['veg2'] : 0;
$veg2amtconsumed=isset($HTTP_POST_VARS['veg2amtconsumed']) ? $HTTP_POST_VARS['veg2amtconsumed'] : 0;
$veg2proportion=isset($HTTP_POST_VARS['veg2proportion']) ? $HTTP_POST_VARS['veg2proportion'] : 0;
$entree=isset($HTTP_POST_VARS['entree']) ? $HTTP_POST_VARS['entree'] : 0;
$entreeamtconsumed=isset($HTTP_POST_VARS['entreeamtconsumed']) ? $HTTP_POST_VARS['entreeamtconsumed'] : 0;
$entreeproportion=isset($HTTP_POST_VARS['entreeproportion']) ? $HTTP_POST_VARS['entreeproportion'] : 0;
$condiment=isset($HTTP_POST_VARS['condiment']) ? $HTTP_POST_VARS['condiment'] : 0;
$condimentamtconsumed=isset($HTTP_POST_VARS['condimentamtconsumed']) ? $HTTP_POST_VARS['condimentamtconsumed'] : 0;
$condimentproportion=isset($HTTP_POST_VARS['condimentproportion']) ? $HTTP_POST_VARS['condimentproportion'] : 0;
$bev1=isset($HTTP_POST_VARS['bev1']) ? $HTTP_POST_VARS['bev1'] : 0;
$bev1amtconsumed=isset($HTTP_POST_VARS['bev1amtconsumed']) ? $HTTP_POST_VARS['bev1amtconsumed'] : 0;
$bev1proportion=isset($HTTP_POST_VARS['bev1proportion']) ? $HTTP_POST_VARS['bev1proportion'] : 'NA';
$bev2=isset($HTTP_POST_VARS['bev2']) ? $HTTP_POST_VARS['bev2'] : 0;
$bev2amtconsumed=isset($HTTP_POST_VARS['bev2amtconsumed']) ? $HTTP_POST_VARS['bev2amtconsumed'] : 0;
$bev2proportion=isset($HTTP_POST_VARS['bev2proportion']) ? $HTTP_POST_VARS['bev2proportion'] : 'NA';
$grain=isset($HTTP_POST_VARS['grain']) ? $HTTP_POST_VARS['grain'] : 0;
$grainamtconsumed=isset($HTTP_POST_VARS['grainamtconsumed']) ? $HTTP_POST_VARS['grainamtconsumed'] : 0;
$grainproportion=isset($HTTP_POST_VARS['grainproportion']) ? $HTTP_POST_VARS['grainproportion'] : 'NA';
$fruit=isset($HTTP_POST_VARS['fruit']) ? $HTTP_POST_VARS['fruit'] : 0;
$fruitamtconsumed=isset($HTTP_POST_VARS['fruitamtconsumed']) ? $HTTP_POST_VARS['fruitamtconsumed'] : 'NA';
$fruitproportion=isset($HTTP_POST_VARS['fruitproportion']) ? $HTTP_POST_VARS['fruitproportion'] : 'NA';

$conn = odbc_connect("YKIDS","","");

if (isset($_POST['student'])) {
foreach ($_POST['student'] as $student) {
$insert_statement = "
insert into 
	lunch_consumption 
	values ($schoolid, '$date', $student, '$veg1', '$veg1amtconsumed', '$veg1proportion', '$veg2', '$veg2amtconsumed', '$veg2proportion', '$entree', '$entreeamtconsumed', '$entreeproportion', '$condiment', '$condimentamtconsumed', '$condimentproportion', '$bev1', '$bev1amtconsumed', '$bev1proportion', '$bev2', '$bev2amtconsumed', '$bev2proportion', '$grain', '$grainamtconsumed', '$grainproportion', '$fruit', '$fruitamtconsumed', '$fruitproportion')";

//echo $insert_statement;
//if($error_rep==0)
//{
$result = odbc_exec($conn,$insert_statement);
if(!$result) echo odbc_errormsg();
}
}
?>

 

MOD EDIT: code tags inserted - again!

Link to comment
Share on other sites

You mentioned that when you were making an update to a single student that the information was being entered into the database successfully.

 

Can you paste a copy of that code so we can compare it to the code you are using for multiple updates?

 

:)

Link to comment
Share on other sites

Here's the html form:

<form name="lunch" action="lunch_add.php" method="post">
<script type="text/javascript" language="JavaScript"><!--
			--></script>
<title>Lunch Consumption Form</title>

  <b><u>Lunch Consumption Monitoring Form</u></b>
  <table width="88%" border="0">
    <tr>
      <td nowrap bordercolor="#000000"><div align="center" class="style1">
          <p align="justify" class="style8">
           DATES must be in this Format: (01/01/07)<BR>
           --------------------------------------------</p>
          
          <p align="justify"><strong>School: </strong>
            <select name="schoolid">
                <option value="1001">Burlington</option>
                <option value="1002">CCC</option>
                <option value="1003">Maple Woods</option>
            </select>
          </strong>
              <strong>Date:</strong>
            <input name="date" type="text" id="date" size="10" maxlength="8">
          </p>
          <p align="justify"><strong>Entree: </strong>        
            <input name="entree" type="text" id="entree" size="40" maxlength="50">
                <strong>Condiment:</strong> 
            <input name="condiment" type="text" id="condiment" size="40" maxlength="50">
          </p>
          <p align="justify"><strong>Grain: </strong>         
            <input name="grain" type="text" id="grain" size="40" maxlength="50">
                 <strong>Fruit:</strong>           
            <input name="fruit" type="text" id="fruit" size="40" maxlength="50">
          </p>
          <p align="justify"><strong>Vegetable 1: </strong>
            <input name="veg1" type="text" id="veg1" size="40" maxlength="50">
                <strong>Vegetable 2:</strong>
            <input name="veg2" type="text" id="veg2" size="40" maxlength="50">
          </p>
          <p align="justify"><strong>Beverage 1: </strong>
            <input name="bev1" type="text" id="bev1" size="40" maxlength="50">
                 <strong>Beverage 2:</strong>
            <input name="bev2" type="text" id="bev2" size="40" maxlength="50">
          </p>
          <br>
          <table width="100%">
        	<tr>
        		<td width="10%"><b>Student</b></td>
        		<td width="20%"> </td>
        		<td width="45%"><b>Amount Consumed</b></td>
        		<td width="35%"><b>Proportion Consumed (in %)</b></td>
        	</tr>
        	<tr>
          		<td align="left">
          		<select name="student">
                <option value="1">S1</option>
                <option value="2">S2</option>
                <option value="3">S3</option>
                <option value="4">S4</option>
                <option value="5">S5</option>
                <option value="6">S6</option>
                <option value="7">S7</option>
                <option value="8">S8</option>
                <option value="9">S9</option>
                <option value="10">S10</option>
            </select>
            	<td align="left">Entree</td>
            	<td><input name="entreeamtconsumed" type="text" id="entreeamtconsumed" size="40" maxlength="50"></td>
            	<td><input name="entreeproportion" type="text" id="entreeproportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Fruit</td>
            	<td><input name="fruitamtconsumed" type="text" id="fruitamtconsumed" size="40" maxlength="50"></td>
            	<td><input name="fruitproportion" type="text" id="fruitproportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Vegetable 1</td>
            	<td><input name="veg1amtconsumed" type="text" id="veg1amtconsumed" size="40" maxlength="50"></td>
            	<td><input name="veg1proportion" type="text" id="veg1proportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Vegetable 2</td>
            	<td><input name="veg2amtconsumed" type="text" id="veg2amtconsumed" size="40" maxlength="50"></td>
            	<td><input name="veg2proportion" type="text" id="veg2proportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Grain</td>
            	<td><input name="grainamtconsumed" type="text" id="grainamtconsumed" size="40" maxlength="50"></td>
            	<td><input name="grainproportion" type="text" id="grainproportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Condiment</td>
            	<td><input name="condimentamtconsumed" type="text" id="condimentamtconsumed" size="40" maxlength="50"></td>
            	<td><input name="condimentproportion" type="text" id="condimentproportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Beverage 1</td>
            	<td><input name="bev1amtconsumed" type="text" id="bev1amtconsumed" size="40" maxlength="50"></td>
            	<td><input name="bev1proportion" type="text" id="bev1proportion" size="2" maxlength="3"></td>
            </tr>
            <tr>
          		<td> </td>
            	<td align="left">Beverage 2</td>
            	<td><input name="bev2amtconsumed" type="text" id="bev2amtconsumed" size="40" maxlength="50"></td>
            	<td><input name="bev2proportion" type="text" id="bev2proportion" size="2" maxlength="3"></td>
            </tr>
          </table>
	  <!--<p align="justify"> </p>
          <p align="right">ENTER
            <input name="enter" type="text" id="enter" size="10" maxlength="5">
          </p>
          <p align="right">REVIEWED
            <input name="reviewed" type="text" id="reviewed" size="10" maxlength="5">
          </p>-->
          <p align="justify">
            <input type = "hidden" name = "action_h" value = "save">
		<input type = "hidden" name = "user_id" value = "<?php echo $user_id ?>">
          
            <input name="submit" type = "submit" class="style13" value="SUBMIT">
	  </p>
          <p align="justify"> </p>
      </div>
  </td>
    </tr>
  </table>
  <p> </p>
</form>

 

And here's lunch_add.php

<?php
$error_rep=0;
?>
<?php
echo '<br><table><tr><td><b>';
foreach ($HTTP_POST_VARS as $key => $value) {
$temp = stripslashes($value);
$HTTP_POST_VARS[$key] = $temp;
}
echo '</td></tr></table></b>';

$schoolid=isset($HTTP_POST_VARS['schoolid']) ? $HTTP_POST_VARS['schoolid'] :'NA';
$date=isset($HTTP_POST_VARS['date']) ? $HTTP_POST_VARS['date'] :'00/00/0000';
$student=isset($HTTP_POST_VARS['student']) ? $HTTP_POST_VARS['student'] :'NA';
$veg1=isset($HTTP_POST_VARS['veg1']) ? $HTTP_POST_VARS['veg1'] : 0;
$veg1amtconsumed=isset($HTTP_POST_VARS['veg1amtconsumed']) ? $HTTP_POST_VARS['veg1amtconsumed'] : 0;
$veg1proportion=isset($HTTP_POST_VARS['veg1proportion']) ? $HTTP_POST_VARS['veg1proportion'] : 0;
$veg2=isset($HTTP_POST_VARS['veg2']) ? $HTTP_POST_VARS['veg2'] : 0;
$veg2amtconsumed=isset($HTTP_POST_VARS['veg2amtconsumed']) ? $HTTP_POST_VARS['veg2amtconsumed'] : 0;
$veg2proportion=isset($HTTP_POST_VARS['veg2proportion']) ? $HTTP_POST_VARS['veg2proportion'] : 0;
$entree=isset($HTTP_POST_VARS['entree']) ? $HTTP_POST_VARS['entree'] : 0;
$entreeamtconsumed=isset($HTTP_POST_VARS['entreeamtconsumed']) ? $HTTP_POST_VARS['entreeamtconsumed'] : 0;
$entreeproportion=isset($HTTP_POST_VARS['entreeproportion']) ? $HTTP_POST_VARS['entreeproportion'] : 0;
$condiment=isset($HTTP_POST_VARS['condiment']) ? $HTTP_POST_VARS['condiment'] : 0;
$condimentamtconsumed=isset($HTTP_POST_VARS['condimentamtconsumed']) ? $HTTP_POST_VARS['condimentamtconsumed'] : 0;
$condimentproportion=isset($HTTP_POST_VARS['condimentproportion']) ? $HTTP_POST_VARS['condimentproportion'] : 0;
$bev1=isset($HTTP_POST_VARS['bev1']) ? $HTTP_POST_VARS['bev1'] : 0;
$bev1amtconsumed=isset($HTTP_POST_VARS['bev1amtconsumed']) ? $HTTP_POST_VARS['bev1amtconsumed'] : 0;
$bev1proportion=isset($HTTP_POST_VARS['bev1proportion']) ? $HTTP_POST_VARS['bev1proportion'] : 'NA';
$bev2=isset($HTTP_POST_VARS['bev2']) ? $HTTP_POST_VARS['bev2'] : 0;
$bev2amtconsumed=isset($HTTP_POST_VARS['bev2amtconsumed']) ? $HTTP_POST_VARS['bev2amtconsumed'] : 0;
$bev2proportion=isset($HTTP_POST_VARS['bev2proportion']) ? $HTTP_POST_VARS['bev2proportion'] : 'NA';
$grain=isset($HTTP_POST_VARS['grain']) ? $HTTP_POST_VARS['grain'] : 0;
$grainamtconsumed=isset($HTTP_POST_VARS['grainamtconsumed']) ? $HTTP_POST_VARS['grainamtconsumed'] : 0;
$grainproportion=isset($HTTP_POST_VARS['grainproportion']) ? $HTTP_POST_VARS['grainproportion'] : 'NA';
$fruit=isset($HTTP_POST_VARS['fruit']) ? $HTTP_POST_VARS['fruit'] : 0;
$fruitamtconsumed=isset($HTTP_POST_VARS['fruitamtconsumed']) ? $HTTP_POST_VARS['fruitamtconsumed'] : 'NA';
$fruitproportion=isset($HTTP_POST_VARS['fruitproportion']) ? $HTTP_POST_VARS['fruitproportion'] : 'NA';

$conn = odbc_connect("YKIDS","","");

$insert_statement = "
insert into 
	lunch_consumption values 
	($schoolid, '$date', $student, '$veg1', '$veg1amtconsumed', '$veg1proportion', '$veg2', '$veg2amtconsumed', '$veg2proportion', 
'$entree', '$entreeamtconsumed', '$entreeproportion', '$condiment', '$condimentamtconsumed', '$condimentproportion', '$bev1', '$bev1amtconsumed', 
'$bev1proportion', '$bev2', '$bev2amtconsumed', '$bev2proportion', '$grain', '$grainamtconsumed', '$grainproportion', '$fruit', 
'$fruitamtconsumed', '$fruitproportion')";

//echo $insert_statement;
//if($error_rep==0)
//{
$result = odbc_exec($conn,$insert_statement);
if(!$result)
{
  echo 'There has been ERROR submitting your data. Please go back and check your data.';
?>
<?php
}
elseif ($result)
{
?>
<table width="698" border="0">
  <tr>
    <td width="688" height="51" bordercolor="#FFFFFF"><h4><font color="#0000FF" size="+4"><em><font color="#006699" size="+3">Your</font> <font color="#006699" size="+3">data has been submitted successfully! </font></em></font></h4>
      <p><span class="style18">Click <a href="javascript:history.back()">here</a> to go back</span></p></td>
  </tr>
</table>
<h3 align="justify"><font color="#0000FF" size="+4"> </font>
  <?php

  
}
//}

else 
{
?>
  <table width="698" border="0">
  <tr>
    <td width="688" height="70" bordercolor="#FFFFFF"><p><font color="#0000FF" size="+4"><em><font color="#006699" size="+3">All the fields have not been filled.</font></em></font></p>
    <p><font color="#0000FF" size="+4"><em><font color="#006699" size="+3">Please fill them.</font></em></font><?php echo '  <a href = "javascript:history.back();">Click Here to Go Back</a>'; ?></p></td>
  </tr>
</table>
<h3 align="justify"><font color="#0000FF" size="+4"> </font>
<?php
}

?>

 

This works for adding each student individually to the database.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.