Solved

PHP PDO not displaying records

Posted on 2013-11-09
7
345 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

687 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