Php variable increment problem
hi,
I'm creating a quiz in php which allows user to select All questions and answers that I have issue in a table to put a checkbox and then allow the trainer to choose which questions will be in the quiz and then store them in a table "quiz1" composed of: num, login, question, rep1, rep2, rep3, rep. so far it's good.
What I do now is that I want to insert in the num fields (not auto_increment) numbers from 1 to n such that n is the number of existing matter, but unfortunately I can not do so. Here is my code which is responsible for integration:
Code:
if ($ _SESSION [ 'login']! ='')
($ Username = $ _SESSION [ 'login'];
for ($ i = 1; $ i <$ _SESSION [ 'i'], $ i + +) (
if (isset ($ _POST [ 'choice'. $ i])) (
$ choice = $ _POST [ 'choice'. $ i];
$ sql1 = "insert into quiz1 values ('','$ pseudo','".$ choice [0 ]."','".$ choice [1 ]."','".$ choice [2]. "','".$ choice [3 ]."','".$ choice [4 ]."','".$ choice [5 ]."')";
$ res = mysql_query ($ sql1);
)
)
)
And I tried to change it:
Quote:
if ($_SESSION['login'] != '')
{ $pseudo = $_SESSION['login'];
$req="select * from quiz1 where login='$pseudo'";
$res=mysql_query($req);
for($i=1;$i<$_SESSION['i'];$i++){
for($j=1;$j<mysql_num_rows($res);$j++){
if (isset($_POST['choix'.$i])){
$choix=$_POST['choix'.$i];
$sql1 ="insert into quiz1 values ('$j','$pseudo','".$choix[0]."','".$choix[1]."','".$choix[2]."','".$choix[3]."','".$choix[4]."','".$choix[5]."')";
$res = mysql_query($sql1);
}
}
}
but it does nothing. Please help me because I really need your help.
Re: Php variable increment problem
You need some views ... including your $ j to see if you do not try to insert variable that already exist.I advise you to make a request in your for loop (the one with the $ j), which will select in each round the number of max variable in your table.
Re: Php variable increment problem
after reading your reply i have done the following changes
Quote:
{$pseudo = $_SESSION['login'];
$req="select * from quiz1 where login='$pseudo'";
$res=mysql_query($req);
//$resa=mysql_num_rows($res);
for($i=1;$i<$_SESSION['i'];$i++){
for($j=1;$j<$res;$j++){
$requete="select MAX(numquest) from quiz1 where login='$pseudo'";
$reponse=mysql_query($req);
$num=$reponse++;
if (isset($_POST['choix'.$i])){
$choix=$_POST['choix'.$i];
$sql1 ="insert into quiz1 values ('$num','$pseudo','".$choix[0]."','".$choix[1]."','".$choix[2]."','".$choix[3]."','".$choix[4]."','".$choix[5]."')";
$res = mysql_query($sql1);
echo"$num";
but it does nothing at all ....
Re: Php variable increment problem
Try to put two echo to see the values:
echo "SESSION Value [i]:". $ _SESSION [ 'i']."< br /> ";
echo "Number enr found:". mysql_num_rows ($ res). "<br />";