code updated
i have a table called vote with three fields ans_1,ans_2,ans_3
query strings number is 2 or 3 according to answers the admin is going to save
so they look like this ?1=aaa&2=bbb or ?1=aaa&2=bbb&3=ccc
my point is to save every query string in a column so i use the code below but it keeps using the last value of the query string only
$queries = $_SERVER['QUERY_STRING'];
$answers = explode("&",$queries );
$num = count($answers);
foreach($answers as $val){
$chars= strlen($val);
$test = substr($val,2,$chars-2);
for($x=1; $x<=$num; $x++){
$Q = "update vote set ans_'$x' = '$test' where Vote_ID = '1'";
$R = mysql_query($Q);
if($R) { echo "done"; } else { echo mysql_errno(); }
}
}
If you have dynamic columns for which you are substituting
$x, do not enclose$xin quotes:Please be sure to escape the contents of
$_SERVER['QUERY_STRING']withmysql_real_escape_string().The proper way to parse a query string in PHP is with
parse_str(), rather than attempting toexplode()on the&.However, since you are grabbing the whole query string, and not filtering any specific variables, why not just use
$_GET?Update
To help you understand why your code isn’t working, I’ll modify it here. However, this is not the preferred method of performing this task. Using
foreach($_GET)as above is much better. Indenting the loop properly will help reveal the problem: