• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

PHP PDO not displaying records

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
Sim1980
Asked:
Sim1980
  • 4
  • 3
1 Solution
 
Marco GasiFreelancerCommented:
Put

session_start():

Open in new window


at the top of the script.
0
 
Sim1980Author Commented:
session_start(); is in my common.php file.
0
 
Sim1980Author Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Marco GasiFreelancerCommented:
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
 
Sim1980Author Commented:
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
 
Sim1980Author Commented:
Thanks. I added the bindParam as well as the userid in the GET and it worked.
0
 
Marco GasiFreelancerCommented:
Glad to help you. Thanks for the points and good luck with your project. :)
Marco
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now