Page 1 of 2

INSERT queries

Posted: Wed Mar 15, 2006 9:34 pm
by enigm4_
Hi Guys.

I'm trying to make a form for users to submit an entry for a competition. All they hgave to do is select the comp(s) they want from a multiple choice SELECT box and tell us whatever we ask for for that issue (i.e. this issue we are asking for short stories (100 words or less).

How do I get a select box to enter into the DB which selections have been made? So far I'm only able to enter in whatever the last selection made is, and that doesn't help!!

Cheers
~Chris

P.S. Sorry if it's already been answered, i've been searching for hours and no answers

Posted: Wed Mar 15, 2006 9:45 pm
by feyd
name it so php will create an array, you can then loop through creating records for each.

choice[] or similar, just as long as it ends in []

Posted: Wed Mar 15, 2006 10:41 pm
by enigm4_
Thanks feyd. how do I loop through it?

Cheers
~Chris

Posted: Wed Mar 15, 2006 10:50 pm
by feyd
with a loop. :P

foreach, while, do..while, for can all be used.

Posted: Wed Mar 15, 2006 10:59 pm
by enigm4_
The problem is I don't know how to write this kind of loop.

How do I make it work?

Posted: Thu Mar 16, 2006 12:53 am
by Benjamin
Download the manual.. there is a copy below in my signature.

Posted: Thu Mar 16, 2006 3:09 am
by enigm4_
I've read the PHP manual and 90% of it means jack to me.

I just don't get the manual, I learn better through real world example.

I have written while loops before but never anything to do this. so I do something along the lines of:

Code: Select all

while(isset($_POST['comp[])) {
    $comp=$_POST['comp[]'];
}
What I don't understand if what should be inside the while loop. I can't set all of them into the same varaible.

Posted: Thu Mar 16, 2006 4:24 am
by JayBird

Code: Select all

foreach($_POST['comp'] as $temp) {
    echo $temp;
}

Posted: Thu Mar 16, 2006 4:50 am
by enigm4_
So would I then just

$temp into the DB and it will contain all or do I need to inset $temp[0], $temp[1], etc...?

Thanks

Posted: Thu Mar 16, 2006 5:01 am
by Benjamin
enigm4_ wrote:What I don't understand if what should be inside the while loop. I can't set all of them into the same varaible.
Well here is the deal. (and I am not being sarcastic or anything) Go to php.net and use the search box. Type in while. You will see an example.

You will see...

Code: Select all

$i = 1;
while ($i <= 10) {
   echo $i++;  /* the printed value would be
                   $i before the increment
                   (post-increment) */
}
What this means is: "While the variable $i is less than or equal to 10, echo (print) $i.

Another example would be...

Code: Select all

$Apples = 0;
$GiveMeAnApple = 1;
$MaxApples = 10;

while ($Apples <= $MaxApples) {
  $Apples = $Apples + $GiveMeAnApple;
  echo "You were just given an apple!<br />";
}
If your goal is be become a programmer, it's very important that you learn how to read and understand the manual. The PHP manual is much easier to understand than most, so if you can't understand that, then you may want to study some programming books.

ALSO::: If you don't understand what all the !=, ==, <=, etc are. Then you can read about these in the "Operands" section of the manual.

Posted: Sat Mar 18, 2006 9:54 pm
by enigm4_
Hey guys,

When I try to submit into my database it gives me an error on line 49 (the foreach line)

error:
Warning: Invalid argument supplied for foreach() in /home/dxqmyhvh/public_html/reakt/inner.php(43) : eval()'d code on line 49

Code: Select all

if(isset($_POST['submit'])) {
			foreach($_POST['entries'] as $comps) {
				echo $comps;
			}
			$story=$_POST['story'];
			
			//Inset everything into the database
			$compentry=mysql_query("INSERT INTO tblCompetitionSubmissions (compUserID, compStory, compEntries, compEntryDate) VALUES ('$userid', '$story', '$comps', NOW())");
			
			//Check whether or not the inset worked:
			if($compentry) {
				//If it worked, echo a success
				echo "<div class=\"information\"><h4>Success!</h4><p>Thankyou, your submission has been received.  We will let you know if you win!</p></div>";
			} else {
				//If it failed, show the error:
				echo "<div class=\"phpoutput\"><h4>Error</h4><p>Sorry, your submission could not be sent at this time.  Please try again later, or <a href=\"mailto:online@reakt.com.au\">email Chris</a> to report your problems.</p></div>";
			}
		}
Can anyone tell me why this is? $_POST['entries'] is deffinately the select field.

cheers

Posted: Sun Mar 19, 2006 11:42 am
by John Cartwright
add

Code: Select all

var_dump($_POST['entries']);
$_POST['entries'] must be an array, what is the var_dump returning?

Posted: Sun Mar 19, 2006 11:56 am
by enigm4_

Code: Select all

Warning: Invalid argument supplied for foreach() in /home/dxqmyhvh/public_html/reakt/inner.php(43) : eval()'d code on line 49

array(13) { [0]=> string(38) "I Bet You Look Good on the Dance Floor" [1]=> string(47) "Whatever People Say I Am, That\'s What I\'m Not" [2]=> string(11) "Dirty Harry" [3]=> string(14) "Suddenly I See" [4]=> string(4) "Talk" [5]=> string(17) "Lights and Sounds" [6]=> string(28) "Nobody Move, Nobody Get Hurt" [7]=> string(23) "Can I Have it Like That" [8]=> string(13) "Raven\'s Gate" [9]=> string(83) "Ultimate 10 Passes entitling you and nine friends to two ultimare laser games free!" [10]=> string(13) "One Game Free" [11]=> string(22) "Double Surprise Passes" [12]=> string(36) "T-Shirts (available in red or black)" }
That's what I get (The first part obviously being the php error)

here's the PHP that evals and submits it to the db :

Code: Select all

if(isset($_POST['submit'])) {
			foreach($_POST['entries[]'] as $comps) {
				echo $comps;
			}
			$story=$_POST['story'];
			
			var_dump($_POST['entries']);
			
			//Inset everything into the database
			$compentry=mysql_query("INSERT INTO tblCompetitionSubmissions (compUserID, compStory, compEntries, compEntryDate) VALUES ('$userid', '$story', '$comps', NOW())");
			
			//Check whether or not the inset worked:
			if($compentry) {
				//If it worked, echo a success
				echo "<div class=\"information\"><h4>Success!</h4><p>Thankyou, your submission has been received.  We will let you know if you win!</p></div>";
			} else {
				//If it failed, show the error:
				echo "<div class=\"phpoutput\"><h4>Error</h4><p>Sorry, your submission could not be sent at this time.  Please try again later, or <a href=\"mailto:online@reakt.com.au\">email Chris</a> to report your problems.</p></div>";
			}
		}
and the form:

Code: Select all

echo "
			<fieldset>
				<legend>Entry Form</legend>
				<form action=\"";$_SERVER['PHP_SELF'];echo "\" method=\"post\">
					<label for=\"entries\">Prizes:</label>
						<select name=\"entries[]\" id=\"entries\" size=\"7\" WIDTH=\"250\" STYLE=\"width:250px;\" multiple>
		";
							$prizes=mysql_query("SELECT * FROM tblPrizes WHERE prizeIssue='$issue' AND prizeVolume='$volume'");
							while($prize=mysql_fetch_array($prizes)) {
								echo "
									<option name=\"$prize[0]\" selected>$prize[2]</option>
								";
							}
		echo"
						</select>
					<label for=\"story\">Tell us a short story</label>
						<textarea name=\"story\" cols=\"5\" rows=\"5\">"; if(isset($_POST['story'])) echo $_POST['story']; echo"</textarea>
					<label></label>
						<input name=\"submit\" type=\"submit\" value=\"Submit\">
				</form>
			</fieldset>
		";

Posted: Sun Mar 19, 2006 3:14 pm
by John Cartwright
foreach($_POST['entries'] as $comps) {

not

foreach($_POST['entries[]'] as $comps) {

Posted: Sun Mar 19, 2006 8:07 pm
by enigm4_
It's still giving me the same error on line 49.

Is there perhaps another way I could go about this?