Jump to content

Noob question regarding do while loop


maestroc

Recommended Posts

I am in over my head on what I thought would be a fairly simple project.  I am only as good as the one PHP book I own can teach me and I am hoping someone will help me with this.  I am creating a php based submission form to take registrations for a district wide school honor band.  The posted values from the form are to be both saved to a MySQL db and also emailed to the chair of the event.  I have tons of variables on the form one part of which looks like this (this section is repeated 14 times, incrementing the value each time, to allow the user to enter 14 different people's names and info).

 

<td> <input type="text" name="first1" /></td><td><input type="text" name="last1" /></td><td><SELECT NAME="instrument1" SIZE=1>
<OPTION SELECTED>Which Instrument?
<OPTION>Flute
  <OPTION>Oboe 
  <OPTION>Bassoon 
  <OPTION>Clarinet
  <OPTION>Bb Clarinet
  <OPTION>Eb Clarinet
  <OPTION>Alto Clarinet
  <OPTION>Bass Clarinet
  <OPTION>Alto Sax
  <OPTION>Tenor Sax
  <OPTION>Bari Sax
  <OPTION>Trumpet
  <OPTION>French Horn
  <OPTION>Trombone
  <OPTION>Baritone BC
  <OPTION>Baritone TC
  <OPTION>Tuba  
  <OPTION>Percussion</SELECT><input type="radio" name="grade1" value="9" /></td><td><input type="radio" name="grade1" value="10" /> </td></tr>
<tr>
<td> <input type="text" name="first2" /></td><td><input type="text" name="last2" /></td><td><SELECT NAME="instrument2" SIZE=1>
<OPTION SELECTED>Which Instrument?
<OPTION>Flute
  <OPTION>Oboe 
  <OPTION>Bassoon 
  <OPTION>Clarinet
  <OPTION>Bb Clarinet
  <OPTION>Eb Clarinet
  <OPTION>Alto Clarinet
  <OPTION>Bass Clarinet
  <OPTION>Alto Sax
  <OPTION>Tenor Sax
  <OPTION>Bari Sax
  <OPTION>Trumpet
  <OPTION>French Horn
  <OPTION>Trombone
  <OPTION>Baritone BC
  <OPTION>Baritone TC
  <OPTION>Tuba  
  <OPTION>Percussion</SELECT><input type="radio" name="grade2" value="9" /></td><td><input type="radio" name="grade2" value="10" /> </td></tr>

 

Being a non-programmer I figured this would be the easiest way to do it, until I started working on the save script.  I tried using a do while loop to save me the trouble of writing everything out 14 times:

 

$i=0;
do
{
   $message .= "<tr><td>".$_POST['first$i']." ".$_POST['last$i'].", ".$_POST['instrument$i'].", ".$_POST['grade$i']."</td></tr>";  
   $i++;
}
while($i<14);

 

But it didn't work.  It is creating 14 lines of commas but not including the posted incremental information.  Do I have to escape the $i variables somehow? 

 

I also wanted to do something similar to insert that same information into individual rows in the database but I can't get that to work either.

 

Could someone guide me on what to try or where to look to figure it out?  If you have ideas on how to do the sql insert I'd be grateful for that as well.  At the moment all I have for that statement is:

 

$query2 = "INSERT INTO hshbSubmissions (first, last, instrument, grade, school) VALUES ('".$_POST['first1']."','".$_POST['last1']."','".$_POST['instrument1']."','".$_POST['grade1']."''".$_POST['school']."')";

 

But this does not work and I don't know why.  Anyone want to help a noob?

 

Sincerely,

MaestroC

 

Link to comment
Share on other sites

You need double quotes around those first$i :

 

   $message .= "<tr><td>".$_POST["first$i"]." ".$_POST["last$i"].", ".$_POST["instrument$i"].", ".$_POST["grade$i"]."</td></tr>";  

 

Double quotes allow variables like $i to be substituted, and also allow interpreting of escape sequences like "\n" for newline.  Single quotes allow no processing inside, so if you write '$i' then you get exactly '$i', not the contents of $i.

 

Regarding your mysql query, what happens when you try it?  Does the query fail (mysql_query() returns false), and if so, what does mysql_error() show?

Link to comment
Share on other sites

One more thing - you should start with $i = 1 and have the termination condition as ($i <= 14).  Otherwise you will run through 0-13 instead of 1-14.

 

Alternatively you can put the $i++ before the $message line, and leave the start and termination conditions unchanged.

Link to comment
Share on other sites

I used the double quotes and it fixed the problem right away.  Thanks!

 

As for your question:

Regarding your mysql query, what happens when you try it?  Does the query fail (mysql_query() returns false), and if so, what does mysql_error() show?

 

I put in an error check line on the query I pasted previously and I get back the error:

 

Couldn't save query2: Column count doesn't match value count at row 1

 

I then realized I left out a stupid comma between the last two variables:

$query2 = "INSERT INTO hshbSubmissions (first, last, instrument, grade, school) VALUES ('".$_POST['first1']."','".$_POST['last1']."','".$_POST['instrument1']."','".$_POST['grade1']."''".$_POST['school']."')";

 

I put in the comma and it is working now!  Thanks!

 

 

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.