This is a discussion on Applying an SQL query through a for...while loop (!!) within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi there, Newbie at this, so here goes: I am attempting to build a page that updates multiple records. The ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi there,
Newbie at this, so here goes: I am attempting to build a page that updates multiple records. The page uses as its input, a series of dynamically generated text boxes from the previous page (through POST variables) The text boxes on the previous page are named as follows: txt_1 txt_2 txt_3 etc.... txt_ans1 txt_ans2 txt_ans3 etc... with a hidden field next to each box to identify the primary key as follows: txt_hidden_1 txt_hidden_2 txt_hidden_3 etc... They are dynamically generated as they are read from the database, hence the common naming conventions. My form handling page (which the form POST action points to) is designed to cycle through each of these values and update the recordset WHERE id=txt_hidden_$counter. If this is unclear (probally is) then look at the code below: $number_of_records = $_POST[txt_num_rows]; //get the value from the hidden field which contains the total number of records //create the sql statement. This should look like UPDATE.... WHERE id=somevariable //the counter will increment for each so we need to have the counter incremented for each time the query is run //need to loop through all the records and apply the SQL query for ( $marker=1; $marker<=$number_of_records; $marker++) { $sql = "UPDATE faq SET faq_quest='$_POST[txt_$marker]', faq_ans='$_POST[txt_ans$marker]' WHERE faq_id = '$_POST[txt_ans$marker]'"; //code here to actually RUN the SQL query } As you can probally guess from the fact that I am typing this massive post, the above code does not work. I keep getting errors when I attempt to use the $marker variable from within the SQL query, to cycle through all the POST variables and update all the records. I think its to do with the single quotes ', which tell the compiler not to process variables, but rather take everything literally, so-to-speak. I guess, my question is: how can I re-write the above snippet to allow me to do what I wish to achieve. Many, many thanks to anyone who takes the time to read this and reply. |
|
|||
|
Are you echoing out what $sql is? I think you should because I doubt it is
what you think it is. you have: $sql = "UPDATE faq SET faq_quest='$_POST[txt_$marker]', and I think you ought to have $sql = "UPDATE faq SET faq_quest='".$_POST[txt_$marker]."',faq_ans='".$_POST[txt_ans$marker]."' WHERE faq_id = '".$_POST[txt_ans$marker]."'"; -Ike "Steve Macleod" <steven_mac@leodhotmail.com> wrote in message news:EYZTb.1512$KR3.433@news-binary.blueyonder.co.uk... > Hi there, > > Newbie at this, so here goes: > > I am attempting to build a page that updates multiple records. The page uses > as its input, a series of dynamically generated text boxes from the previous > page (through POST variables) > > The text boxes on the previous page are named as follows: > > txt_1 > txt_2 > txt_3 > etc.... > > txt_ans1 > txt_ans2 > txt_ans3 > etc... > > with a hidden field next to each box to identify the primary key as follows: > > txt_hidden_1 > txt_hidden_2 > txt_hidden_3 > > etc... > > They are dynamically generated as they are read from the database, hence the > common naming conventions. > > My form handling page (which the form POST action points to) is designed to > cycle through each of these values and update the recordset WHERE > id=txt_hidden_$counter. If this is unclear (probally is) then look at the > code below: > > > $number_of_records = $_POST[txt_num_rows]; //get the value from the hidden > field which contains the total number of records > > > //create the sql statement. This should look like UPDATE.... WHERE > id=somevariable > //the counter will increment for each so we need to have the counter > incremented for each time the query is run > > > //need to loop through all the records and apply the SQL query > > for ( $marker=1; $marker<=$number_of_records; $marker++) { > > $sql = "UPDATE faq SET faq_quest='$_POST[txt_$marker]', > faq_ans='$_POST[txt_ans$marker]' WHERE faq_id = '$_POST[txt_ans$marker]'"; > //code here to actually RUN the SQL query > } > > As you can probally guess from the fact that I am typing this massive post, > the above code does not work. I keep getting errors when I attempt to use > the $marker variable from within the SQL query, to cycle through all the > POST variables and update all the records. I think its to do with the single > quotes ', which tell the compiler not to process variables, but rather take > everything literally, so-to-speak. > > I guess, my question is: how can I re-write the above snippet to allow me to > do what I wish to achieve. > > Many, many thanks to anyone who takes the time to read this and reply. > > > > > |