Solved

Looping through form fields. Need Help

Posted on 2007-11-21
4
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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.

623 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