Solved

Looping through form fields. Need Help

Posted on 2007-11-21
4
231 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 250 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 250 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now