wayz1229 Posted November 13, 2009 Share Posted November 13, 2009 can anyone teach me how to do loop for the script below.. thanks in advance.. <?php require_once('config.php'); $aid1=$_POST['Question-1']; $aid2=$_POST['Question-2']; $aid3=$_POST['Question-3']; $aid4=$_POST['Question-4']; $aid5=$_POST['Question-5']; $aid6=$_POST['Question-6']; $aid7=$_POST['Question-7']; $aid8=$_POST['Question-8']; $aid9=$_POST['Question-9']; $aid10=$_POST['Question-10']; $qid1=$_POST['qid1']; $qid2=$_POST['qid2']; $qid3=$_POST['qid3']; $qid4=$_POST['qid4']; $qid5=$_POST['qid5']; $qid6=$_POST['qid6']; $qid7=$_POST['qid7']; $qid8=$_POST['qid8']; $qid9=$_POST['qid9']; $qid10=$_POST['qid10']; // update vote counter $query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid1' AND qid = '$qid1'"; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); $query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid2' AND qid = '$qid2'"; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); $query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid3' AND qid = '$qid3'"; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); $query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid4' AND qid = '$qid4'"; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); $query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid5' AND qid = '$qid5'"; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); $query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid6' AND qid = '$qid6'"; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); $query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid7' AND qid = '$qid7'"; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); $query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid8' AND qid = '$qid8'"; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); $query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid9' AND qid = '$qid9'"; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); $query = "UPDATE answers SET acount = acount + 1 WHERE aid = '$aid10' AND qid = '$qid10'"; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); // close connection mysql_close(); ?> Link to comment https://forums.phpfreaks.com/topic/181409-need-help/ Share on other sites More sharing options...
mikesta707 Posted November 13, 2009 Share Posted November 13, 2009 I would, instead of having inviduvidual post variables like you do, make an HTML array, so your form should resemble <!-- questions --> <input type="text".... name="Question[]" /> <input type="text".... name="Question[]" /> etc <!-- question ids (assuming they are hidden fields? --> <input type="hidden" name="qid[]" /> <input type="hidden" name="qid[]" /> etc and then you could just loop through each question and corresponding id like so foreach($_POST['Question'] as $key=>$value){ $aid = $value; $qid = $_POST['qid'][$key]; $query = "UPDATE ...... WHERE aid= '$aid' AND qid='$qid'"; //do query } Link to comment https://forums.phpfreaks.com/topic/181409-need-help/#findComment-956960 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.