Solved

PHP PDO not displaying records

Posted on 2013-11-09
7
343 Views
Last Modified: 2013-11-10
Hi all.

I have the following php file that is not displaying  any communities in the drop down list, yet I know there are records. This happens when I added the :user_id parameter. When I just leave the :q parameter it works fine. What am I doing wrong with the :user_id parameter?

Thank you in advance.

<?php
    require("common.php");
    
    if(!$_SESSION['user']){
        header("Location: index.php"); 
        exit();
        }
$query ='SELECT BuilderCommunityID,Community FROM BuilderCommunity INNER JOIN FieldSuperAssignment ON BuilderCommunity.BuilderCommunityID = FieldSuperAssignment.BuilderCommunityID WHERE Builder = :q AND UserID = :user_id ORDER BY Community';          

$smt1->execute(array(':q' => $_GET["q"],':user_id' => $_SESSION["user"]["userid"]));
$data1 = $smt1->fetchAll();

printf("<option selected></option>");
foreach ($data1 as $row){
printf("<option value = '%s'>%s</option>", html_escape($row['BuilderCommunityID']),html_escape($row['Community']));
 }
?>

Open in new window

0
Comment
Question by:Sim1980
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39636039
Put

session_start():

Open in new window


at the top of the script.
0
 

Author Comment

by:Sim1980
ID: 39636047
session_start(); is in my common.php file.
0
 

Author Comment

by:Sim1980
ID: 39636048
I tried this and still does not work:

$smt1 = $db1->prepare('SELECT BuilderCommunityID,Community FROM BuilderCommunity INNER JOIN FieldSuperAssignment ON BuilderCommunity.BuilderCommunityID = FieldSuperAssignment.BuilderCommunityID WHERE Builder = :q  AND UserID = :user_id ORDER BY Community');

$smt1->execute(array(':q' => $_GET["q"],':user_id' => $_SESSION['user']['userid']));
$data1 = $smt1->fetchAll();

Open in new window

0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 39636057
Have you checked that $_SESSION['user']['userid'] have the expected value? You can try to use var_dump()

echo "<pre>";
var_dump($_SESSION['user']);
echo "</pre>";

Open in new window


Try to use bindParam.
$smt1 = $db1->prepare('SELECT BuilderCommunityID,Community FROM BuilderCommunity INNER JOIN FieldSuperAssignment ON BuilderCommunity.BuilderCommunityID = FieldSuperAssignment.BuilderCommunityID WHERE Builder = :q  AND UserID = :user_id ORDER BY Community');
$smt->bindParam(':q', $_GET["q"], PDO::PARAM_STR);
$smt->bindParam(':user_id', $_SESSION['user']['userid'], PDO::PARAM_STR);
$smt1->execute();
$data1 = $smt1->fetchAll();

Open in new window

0
 

Author Comment

by:Sim1980
ID: 39636067
You know. It may be because of the javascript I'm using in the php file that calls the above php file. I may need to include the userid in the "GET" string.

<?php 
 // First we execute our common code to connection to the database and start the session 
 	require("common.php");

      

if(!$_SESSION['user']){
 header("Location: index.php");
 exit();
}

      if ($_SESSION['user']['usertype'] == 'admin' or $_SESSION['user']['usertype'] == 'Management') 
      {
      	$smt1 = $db1->prepare('SELECT DISTINCT Builder FROM BuilderCommunity INNER JOIN FieldSuperDataEntry ON BuilderCommunity.BuilderCommunityID = FieldSuperDataEntry.BuilderCommunityID ORDER BY Builder');
	  	$smt1->execute();
	  	$data1 = $smt1->fetchAll();
      }   
      
      else
      {
	  	$smt1 = $db1->prepare('SELECT DISTINCT Builder FROM BuilderCommunity INNER JOIN FieldSuperAssignment ON BuilderCommunity.BuilderCommunityID = FieldSuperAssignment.BuilderCommunityID   WHERE UserID = :user_id ORDER BY Builder');
	  	$smt1->execute(array(':user_id' => $_SESSION['user']['userid']));
	  	$data1 = $smt1->fetchAll();
      }        



$_SESSION['action_token'] = generate_secure_token(); 
?> 

<!DOCTYPE html>
<head>
  <title>Field Super Web App</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <link rel="stylesheet" href="style.css" type="text/css" />
  <link href="iphone-icon1.png" rel="apple-touch-icon">
  <script>

function ReloadPage() {
if ((/iphone|ipod|ipad.*os 6/gi).test(navigator.appVersion)) {
window.onpageshow = function(evt) {
if (evt.persisted) {
document.body.style.display = "none";
location.reload();
}
};
}
}

function showUser(str,str1)
{
if (str=="")
{
document.getElementById("community").innerHTML="";
return;
} if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("community").innerHTML=xmlhttp.responseText;
}
}

xmlhttp.open("GET","loadcommunity.php?q="+str,true);
xmlhttp.send();
}


</script>
</head>
<body onload="ReloadPage()">
<div class="wrapper">
    <div id="logo"></div>
    <form class="form4" action="ViewBuilderCommunityLotsBlocks.php" method="post">
<div class="formtitle4">View Builder/Community Lots and Blocks</div>

    		
           
    		<div class="input3">
                 <div class="inputtext">Builder:</div>           
                 	<div class="inputcontent" ><select name="Builder" id="Builder" onchange="showUser(this.value)" style="width: 250px"><option selected="selected"></option>    
                
                <?php foreach($data1 as $row) { printf("<option>%s</option>", html_escape($row['Builder']) ); }?>
                 	</select></div>
               
                <br>
                <br>
                <br>
                <div class="inputtext">Community:</div> 
                    <div class="inputcontent" ><select name="community" id="community"  style="width: 250px">
                            
                     </select></div>
     
                
             </div> 
                
               

               
		 <div class="input nobottomborder">    			
		 </div>          
                		
            
        <input type="hidden" name="action_token" value="<?php echo html_escape($_SESSION['action_token']) ?>" />
 
     
			<div class="buttons" align = center>

				<input class="button1"  type="submit" value="View"/>
				<input class="button7"  type="submit" formaction="ViewBuilderLotsBlocks.php" formmethod="post" value="All Communities"/>
				<input class="button1"  type="submit" formaction="Menu.php" value="Menu" /> 

		
			</div>


</form>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:Sim1980
ID: 39637018
Thanks. I added the bindParam as well as the userid in the GET and it worked.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39637021
Glad to help you. Thanks for the points and good luck with your project. :)
Marco
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question