Results 1 to 4 of 4

Thread: Php variable increment problem

  1. #1
    Join Date
    Apr 2009
    Posts
    29

    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:
    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.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    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.

  3. #3
    Join Date
    Apr 2009
    Posts
    29

    Re: Php variable increment problem

    after reading your reply i have done the following changes
    {$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 ....

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    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 />";

Similar Threads

  1. Problem solving scope of variable
    By Maya Angelou in forum Software Development
    Replies: 5
    Last Post: 23-03-2010, 10:10 AM
  2. Replies: 6
    Last Post: 16-12-2009, 06:31 PM
  3. Problem of const variable and its reference
    By KABIRA16 in forum Software Development
    Replies: 3
    Last Post: 04-09-2009, 04:45 PM
  4. Replies: 2
    Last Post: 28-08-2009, 07:51 PM
  5. Problem with variable input in C program
    By Zool in forum Software Development
    Replies: 2
    Last Post: 14-05-2009, 09:43 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,751,646.91713 seconds with 16 queries