Solved

PHP PDO not displaying records

Posted on 2013-11-09
7
338 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
  • 4
  • 3
7 Comments
 
LVL 30

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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 30

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 30

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now