Problem in while loop

Discussion in 'PHP' started by Shaimaa, Feb 22, 2010.

  1. #1
    Dear all,

    I have piece of code:

    $person_ID = array();
    	$day_ID = 0;
    	for($i=0;$i<count($dayid);$i++){
    		echo "Day ID: ".$dayid[$i]."<br>";
    		$day_ID = $dayid[$i];
    		//$sql = "SELECT personID FROM time Where dayID=".$day_ID." AND frm=".$_POST['select_from']." AND to2=".$_POST['select_to'];
    		//$sql = " SELECT personID FROM time Where dayID= ".$day_ID." AND frm=".$_POST['select_from']." AND to2= ".$_POST['select_to'];
    		$sql = 'SELECT personID FROM time Where dayID="$day_ID" AND frm="$_POST[select_from]" AND to2="$_POST[select_to]"';
    		echo "SQL: ".$sql."<br>";
    	        $selectPersonIDs = mysql_query($sql );
    		
                    /*
    THIS LINE
                    */
    		while($row = mysql_fetch_array($selectPersonIDs ))
    		{
    		  	echo "person id: ".$row['personID']."<br />";
    		  	$person_ID["$i"] = $row['personID'];
    		  	echo "Person ID: ".$person_ID["$i"]."<br>";
    		}
    	  }
    
    PHP:
    and there is a problem in the line which have a comment with capital letters ... I don't know why?

    I don't know why BUT it's clearly that it doesn't accept
    substitution of variables which comes from the form using POST method or declared in the code
     
    Shaimaa, Feb 22, 2010 IP
  2. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #2
    im not understanding ur question properly what is the issue can u explain in details its not captuing title wods ?
     
    Bohra, Feb 22, 2010 IP
  3. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #3
    You have to clear up your syntax. The way it is now is pretty easy to make errors.

    Your error is that the $sql is using SINGLE QUOTES.. And you include variables in that, and single quoted strings DOES NOT parse variables.

    E.g.:
    
    <?
    $hey = "Elias";
    echo 'Hey, $hey'; //Returns: Hey, $hey
    echo "Hey, $hey"; //Returns: Hey, Elias
    ?>
    
    Code (markup):
    
    $person_ID = array();
        $day_ID = 0;
        for($i=0;$i<count($dayid);$i++){
            echo "Day ID: ".$dayid[$i]."<br>";
            $day_ID = $dayid[$i];
            [B]$selectPersonIDs = mysql_query("SELECT * FROM time WHERE dayID = '".$day_ID."' AND frm = '".$_POST['select_from']."' AND to2 = '".$_POST['select_to']."'");[/B]
     
            while($row = mysql_fetch_array($selectPersonIDs ))
            {
                echo "person id: ".$row['personID']."<br />";
                [B]$person_ID[$i][/B] = $row['personID'];
                echo "Person ID: ".[B]$person_ID[$i][/B]."<br>";
            }
          }
    
    Code (markup):
    Furthermore, you might want to prepare your code against sql injections. As it is now, the code is VERY vulnerable for sql injections/hacking.
     
    elias_sorensen, Feb 22, 2010 IP
  4. Shaimaa

    Shaimaa Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thank you all

    I modified the code to be:

    	$person_ID = array();
    	$day_ID = 0;
    	for($i=0;$i<count($dayid);$i++){
    		echo "Day ID: ".$dayid[$i]."<br>";
    		$day_ID = $dayid[$i];
    		//$sql = "SELECT personID FROM time Where dayID=".$day_ID." AND frm=".$_POST['select_from']." AND to2=".$_POST['select_to'];
    		//$sql = " SELECT personID FROM time Where dayID= ".$day_ID." AND frm=".$_POST['select_from']." AND to2= ".$_POST['select_to'];
    		/*
    		New Line
    		*/
    		//$sql = "SELECT personID FROM time Where dayID='$day_ID' AND frm='$_POST[select_from]' AND to2='$_POST[select_to]'";
    		$sql = "SELECT personID FROM time Where dayID= ".$day_ID." AND frm= ".$_POST['select_from']." AND to2= ".$_POST['select_to'];
    		echo "SQL: ".$sql."<br>";
    	        $selectPersonIDs = mysql_query($sql );
    		/*
    		new line
    		*/
    		while($row = mysql_fetch_assoc($selectPersonIDs ))
    		{
    		  	echo "person id: ".$row['personID']."<br />";
    		  	$person_ID["$i"] = $row['personID'];
    		  	echo "Person ID: ".$person_ID["$i"]."<br>";
    		}
    	  }
    
    PHP:
    where the variables used are integers

    but the problem is:

    in the while loop I don't know what to do to solve this problem... thank you in advance
     
    Shaimaa, Feb 22, 2010 IP
  5. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you dont have prob in while loop. There is some error in your query like you are using binding operator . while passing variable
     
    Om ji Kesharwani, Feb 22, 2010 IP
  6. Shaimaa

    Shaimaa Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thank you...
    can you give me more explanation?
     
    Shaimaa, Feb 22, 2010 IP
  7. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #7
    Use this query instead:
    
    $sql = "SELECT personID FROM time WHERE dayID= '".$day_ID."' AND frm= '".$_POST['select_from']."' AND to2= '".$_POST['select_to']."'".;
    
    Code (markup):
    You HAVE to put strings into quotes - otherwise it will be treated as an int.
     
    elias_sorensen, Feb 22, 2010 IP
  8. Shaimaa

    Shaimaa Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I run the following query:

    
    		$sql = "SELECT personID FROM time WHERE dayID= '".$day_ID."' AND frm= '".$_POST['select_from']."' AND to2= '".$_POST['select_to']."'";
    
    PHP:
    on MY SQL and it gave me the following error:

     
    Shaimaa, Feb 22, 2010 IP
  9. AntelopeSalad

    AntelopeSalad Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    If dayID is expecting an integer then it's failing because if you look at the actual error you'll see the day is '23 ' not '23'.

    Also this line:
    $person_ID["$i"] = $row['personID'];

    should be:
    $person_ID[$i] = $row['personID'];
     
    AntelopeSalad, Feb 22, 2010 IP
  10. Shaimaa

    Shaimaa Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Finally I wrote the following

    	$person_ID = array();
    	$day_ID = 0;
    	for($i=0;$i<count($dayid);$i++){
    		echo "Day ID: ".$dayid[$i]."<br>";
    		$day_ID = $dayid[$i];
    		//$sql = "SELECT personID FROM time Where dayID=".$day_ID." AND frm=".$_POST['select_from']." AND to2=".$_POST['select_to'];
    		//$sql = " SELECT personID FROM time Where dayID= ".$day_ID." AND frm=".$_POST['select_from']." AND to2= ".$_POST['select_to'];
    		/*
    		New Line
    		*/
    		//$sql = "SELECT personID FROM time Where dayID='$day_ID' AND frm='$_POST[select_from]' AND to2='$_POST[select_to]'";
    		//$sql = "SELECT personID FROM time Where dayID= ".$day_ID." AND frm= ".$_POST['select_from']." AND to2= ".$_POST['select_to'];
    //		$sql = "SELECT personID FROM time WHERE dayID= '".$day_ID."' AND frm= '".$_POST['select_from']."' AND to2= '".$_POST['select_to']."'";
    		$sql = "SELECT personID FROM time WHERE dayID= ".$day_ID." AND frm= ".$_POST['select_from']." AND to2= ".$_POST['select_to'];
    
    
    		echo "SQL: ".$sql."<br>";
    	        $selectPersonIDs = mysql_query($sql );
    		/*
    		new line
    		*/
    		while($row = mysql_fetch_array($selectPersonIDs ))
    		{
    		  	echo "person id: ".$row['personID']."<br />";
    		  	/*
    		  	New Line ...
    		  	*/
    //		  	$person_ID[$i] = $row['personID'];
    			$person_ID["$i"] = $row['personID'];
    		  	echo "Person ID: ".$person_ID["$i"]."<br>";
    		}
    	  }
    
    PHP:
    and it gives me the following warring:

    :(:(:(:(
     
    Shaimaa, Feb 23, 2010 IP
  11. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #11
    try this :

    $from = $_POST['select_from'];
    $to = $_POST['select_to'];

    $sql = "SELECT personID FROM time WHERE dayID='$day_ID' AND frm='$from' AND to2='$to' ";
     
    shallowink, Feb 23, 2010 IP
  12. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #12
    Why don't you check for MySQL errors then? That will give you the exact reason on why it failed.

    
    $selectPersonIDs = mysql_query($sql)or die(mysql_error());
    
    Code (markup):
     
    elias_sorensen, Feb 24, 2010 IP