?
Solved

PHP PDO not displaying records

Posted on 2013-11-09
7
Medium Priority
?
346 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 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

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

764 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