Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Looping through form fields. Need Help

Posted on 2007-11-21
4
Medium Priority
?
262 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 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 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