0

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();
?>
3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by wayz1229
0

Try the following:

<?php
require_once('config.php');
    
    for ($i=1;$i<11;$i++) {
    $aid='aid'.$i;
    $$aid=$_POST['Question-'.$i];
    
    $qid='qid'.$i;
    $$qid=$_POST['qid'.$i];
	
    $query = 'UPDATE answers SET acount = acount + 1 WHERE aid = "'.mysql_real_escape_string($$qid).'" AND qid = "'.mysql_real_escape_string($$qid).'"';
    mysql_query($query) or die("ERROR: $query. ".mysql_error());    
    }
    mysql_close();
?>

I haven't tested it for bugs and yes those variables with two dollar signs should be correct. Also you can take a look at my loops tutorial as additional reference.

0

Try the following:

<?php
require_once('config.php');
    
    for ($i=1;$i<11;$i++) {
    $aid='aid'.$i;
    $$aid=$_POST['Question-'.$i];
    
    $qid='qid'.$i;
    $$qid=$_POST['qid'.$i];
	
    $query = 'UPDATE answers SET acount = acount + 1 WHERE aid = "'.mysql_real_escape_string($$aid).'" AND qid = "'.mysql_real_escape_string($$qid).'"';
    mysql_query($query) or die("ERROR: $query. ".mysql_error());    
    }
    mysql_close();
?>

I haven't tested it for bugs and yes those variables with two dollar signs should be correct. Also you can take a look at my loops tutorial as additional reference.

It works perfectly .. thank you cwarn23.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.