Solved

Looping through form fields. Need Help

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

920 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