looped insert [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

looped insert [Solved]

Post by C_Calav »

hi guys,

im working on this script and so far so good.

now i want to turn what i have into a a loop where it loops the insert statement.

in the comments is where i am starting my loop.

now what i have trouble with is this bit:

Code: Select all

$s_id = $_POSTї"s_id"];
$q_no = $_POSTї"q_1"];
$r_text = $_POSTї"txt_answer_0"];
$r_radio = $_POSTї"radio_0"];
now the s_id will stay the same.

but the q_no will go: q_1, q_2, q_3
r_text will go: txt_answer_0, txt_answer_1, txt_answer_2
r_radio will go: radio_0, radio_1, radio_2

how can i dynamically have this change?

has what i have written made sense?

in the example below i have hardcoded the vaules in for ONE insert.

thanx

Code: Select all

<?php
     session_start(); 
     ob_start();
     
	include("conection.php"); 
     

	if ($_SERVER&#1111;'REQUEST_METHOD'] == "POST") 
        &#123;

	$s_id = $_POST&#1111;"s_id"];
	$q_no = $_POST&#1111;"q_1"];
	$r_text = $_POST&#1111;"txt_answer_0"];
	$r_radio = $_POST&#1111;"radio_0"];

    ## for ($i=0; $i <$num_results; $i++)
    ## &#123;
    ## $sql  = "INSERT INTO tbl_results (s_id,q_no,r_text,r_radio)     
    ##VALUES ('$s_id','$q_no','$r_text','$r_radio')";
    ## $result = mysql_query($sql) or die 
    ##("Execution failed: ".mysql_error());
    ## &#125;

	echo"s_id: $s_id <br />";
	echo"q_no: $q_no <br />";
	echo"r_text: $r_text <br />";
	echo"r_radio: $r_radio <br />";

	&#125; 

?>
Last edited by C_Calav on Mon Feb 14, 2005 6:11 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use concatenation to tack on the number you need for each..

alternately, you can rename the form fields to ones that php will automatically create nicely formed arrays for you to use.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hmmm i see,

can you give me a example of the conncatination?

and how will that fix my problem of for example running a loop retriving the variables?

Code: Select all

$q_no = $_POST&#1111;"q_x"];
where x is dynamically changing

thanx for the help
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

for($x = 0; $x < 3; $x++)
&#123;
  echo $_POST&#1111;'q_' . $x];
&#125;
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanx feyd,

whats the sql to see what the higest number i have for q_no in a row.

so i can set the 3 below to be the higest number.

Code: Select all

for($x = 0; $x < 3; $x++) 
&#123; 
    echo $_POST&#1111;'q_' . $x]; 
&#125;
thanx.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can switch to a straight counting and check when the n'th question doesn't exist :: while(), isset()

I'd still recommend renaming the fields to create a nice array..
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

this is what i am trying to do:

Code: Select all

$s_id = $_POST&#1111;"s_id"];

$sql="SELECT MAX (q_no) FROM tbl_questions where s_id=$s_id";
$result = mysql_query($sql) or die ("Execution failed: ".mysql_error());

##for($x = 0; $x < 3; $x++) 
##&#123; 
##echo $_POST&#1111;'q_' . $x]; 
##&#125;
its not working yet. but ill get the max number, then put the max number in the for loop so it knows how many times to loop.


can you show me a example or what i have to do to build a array.

i dont think i have done one in php before.

i would want to do this the best way

thanx
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

final HTML output should appear similar to the following example.

Code: Select all

<form method="post">
<input type="hidden" name="s_id" value="3" />

What do you see yourself doing in 5 years? <input type="text" name="q&#1111;0]&#1111;answer]" value="" /><br /><br />

Have you ever been convicted of a misdemeanor or felony? <input type="radio" name="q&#1111;1]&#1111;radio]" value="yes" /> Yes.
<input type="radio" name="q&#1111;1]&#1111;radio]" value="no" /> No.<br /><br />

If yes to the previous question, did you drop the soap? <input type="radio" name="q&#1111;2]&#1111;radio]" value="yes" /> Yes.
<input type="radio" name="q&#1111;2]&#1111;radio]" value="no" /> No.<br /><br />

What did you eat for lunch today? <input type="text" name="q&#1111;3]&#1111;answer]" value="" /><br /><br />

<input type="submit" name="submit" value="Done" /></form>
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

isnt that kind of what i have already done?

would the php for the array be:

$stuff[0] = 'a';
$stuff[1] = 'b';
$stuff[2] = 'c';
$stuff[3] = 'd';

but then we would still need to do the sql max thing to get the higest number right?

so we know how many rows are in the array, is that right?

here is my out put from my html so far:

Code: Select all

<p>
Q#: 1 Q: question 1
<br /><br />

<label><input type="radio" name="radio_1" value="1">1</label>
<label><input type="radio" name="radio_1" value="2">2</label>
<label><input type="radio" name="radio_1" value="3">3</label>
<label><input type="radio" name="radio_1" value="4">4</label>
<label><input type="radio" name="radio_1" value="5">5</label>                

<br /><br />

<textarea name="txt_answer_1" rows="5"></textarea>

<input type="hidden" id="q_no" name="q_1" value="1"  >  
</p>


<p>
Q#: 2 Q: Quest 2
<br /><br />

<label><input type="radio" name="radio_2" value="1">1</label>
<label><input type="radio" name="radio_2" value="2">2</label>
<label><input type="radio" name="radio_2" value="3">3</label>
<label><input type="radio" name="radio_2" value="4">4</label>
<label><input type="radio" name="radio_2" value="5">5</label>                

<br /><br />

<textarea name="txt_answer_2" rows="5"></textarea>

<input type="hidden" id="q_no" name="q_2" value="2"  >  
</p>
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

what is wrong with this querry?

$sql="SELECT MAX (q_no) FROM tbl_questions where s_id='$s_id'";

giving me this error
Execution failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(q_no) FROM tbl_questions where s_id=47' at line 1
thanx
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your html is nothing like the one I posted. When posted, mine will automatically be arrays. Yours will not.

copy my example to a file, set the action to a seperate file where all you do is print_r($_POST).. you'll see what I'm talking about.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I might also add that it would probably be better to insert all the entries in one query, rather than multiple queries - it'll likely mean a quicker page load and less server load.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanx feyd and pickle,

feyd: i will try your example and get back to you, looks like will be tomorow.

pickle: how would i "insert all the entries in one query" ?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

It always takes me a few tries before I get the syntax correct, but the query would go something like:

Code: Select all

INSERT INTO
   tbl_results
   (s_id,
    q_no,
    r_text,
    r_radio,)
VALUES
    ('$s_id1',
     '$q_no1',
     '$r_text1,'
     '$r_radio1'),
     ('$s_id2',
     '$q_no2',
     '$r_text2,'
     '$r_radio2')
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hey pickle, thanx

but i would not be able to use this method, no?

i would have to have a loop becuase my questions etc are always going to be different.

i could be wrong im just saying.
Post Reply