Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Looping through form fields. Need Help

Posted on 2007-11-21
4
Medium Priority
?
266 Views
Last Modified: 2013-12-12
Hey all,

I have a form of 15 questions each include three fields Points : Reason : Attachment

In the edit form I have them set up as:

1_points : 1_reason : 1_file
2_points : 2_reason : 2_file
etc.

So I need to update an entry in the database for each question with these three fields.

Right now i'm using:

foreach($_POST as $key => $data) {

which loops through each individual field, I need a way to look at 1_points 1_reason 1_file and then execute the SQL update or insert then with 2_points 2_reason, 2_file etc.

If this makes sense, please let me know i'm very stuck :)

Thank you!
Willliam

Here's my update_card.php

<?
include "config.php";

foreach($_POST as $key => $data) {
if ($key == "letter_grade") {
mysql_query("UPDATE skiareas set letter_grade='$data' where id=$skiarea_id");
}
if ($key == "percentage") {
mysql_query("UPDATE skiareas set percentage='$data' where id=$skiarea_id");
}

$field_name = explode('_',$key);
$question_id = "".$field_name[0]."";
$field_name = "".$field_name[1]."";

$query1 = mysql_query("SELECT count(id) from answer where skiarea_id='$skiarea_id' and question_id='$question_id'");
$query1 = mysql_fetch_array($query1);
$count = "".$query1[0]."";
}

?>
0
Comment
Question by:wglenn
4 Comments
 
LVL 19

Accepted Solution

by:
Michael701 earned 1000 total points
ID: 20332319
looks like your code will loop 3 times for each of the 15 questions, getting a count of this question.

and this is overkill
$question_id = "".$field_name[0]."";
how about just
$question_id = $field_name[0];

I see your reading the previous count from the database, but not updating anything. Is there an update to the database?

note: $count= will be ONLY the last question processed.
0
 
LVL 2

Assisted Solution

by:guston76
guston76 earned 1000 total points
ID: 20338115
Since you know that the number of question is 15, then you can use these code :
for ($i=1; $i<=15; $i++)
{
	$points = $_POST [ $i . '_points' ];
	$reason = $_POST [ $i . '_reason' ];
	$file   = $_POST [ $i . '_file'   ];
 
	// then update your table according to those three variables
	// You can check whether the variables have value with isset()
}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question