User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 402,066 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,554 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1112 | Replies: 22
Reply
Join Date: Jan 2008
Posts: 76
Reputation: rickarro is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster in Training

Help Need Help

  #1  
Jan 11th, 2008
I'm very new to php and am looking for help getting data out of my MYSQL database. I've made all my proper connections and all is running fine so far. What i want to do is have people get a result from the database based on their input...i.e., put in firstname, lastname and dob in three separate boxes and use that data as a way to query the database to see if they are in it based on those three items. I can do it with just lastname, but want all three to be true before a response is given. More or less like a lookup based on three variables. I just don't know enough to figure this one out by myself. Does anyone have a lookup script already working that i can play with, or pointers on how to make this work. I'm using php, mysql on IIS in windows. Hope this is enough info, appreciate anyones help. I figured out the "WHERE lname = "smith" to get a response but would like "WHERE lname = (what was inputed) AND fname = (what was inputed) AND dob = (what was inputed)"

Thanks
Last edited by rickarro : Jan 11th, 2008 at 5:57 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2007
Location: Sofia, Bulgaria
Posts: 138
Reputation: MitkOK is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Re: Need Help

  #2  
Jan 11th, 2008
Hi.

So, why don't you make such kind of query statement ?
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 239
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Need Help

  #3  
Jan 12th, 2008
Have 3 textboxes in your form. Call it firstname, lastname and date_of_birth. The page submits to 'somepage.php'. In somepage.php, you can access the form variables like,

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
....


Then in your query, you can use these posted form variables.


$query="select * from users where firstname='$firstname' AND lastname='$lastname'";


Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Jan 2008
Posts: 76
Reputation: rickarro is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster in Training

Re: Need Help

  #4  
Jan 12th, 2008
Thank you nav33n, that looks very helpful, i'll give it a try. I wasn't sure if you could use the "AND" in the select statement, i think i'd tried it but may have had a few other things wrong as well, but this gives me something to work with. I appreciate your help, i'll let you know how it goes.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 239
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Need Help

  #5  
Jan 12th, 2008
You are welcome! And yep, you can use both AND and && .
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Jan 2008
Posts: 76
Reputation: rickarro is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster in Training

Re: Need Help

  #6  
Jan 14th, 2008
Ok, i'm further than i was, after your post and reading up on other scripts, i came up with this. The form works great but i'm only seeing the actual script of my query when I run it. I have my form as in .php script and the rest in another .php script. Here is the form script.....
<body>

<form name="form" action="fromform.php" method="post">
<p>Input First Name: <input name="fname" type="text" id="fname">
<p>Input Last Name: <input name="lname" type="text" id="lname">
<p>Input Date of Birth (i.e. yyyymmdd Like 20060923): <input name="dob" type="text" id="dob">
<p><input type="submit" name="submit" value="submit">
</form>

And here is my "fromform.php" script. I'm thinking I have my (")'s wrong in the $query = select *...... area somewhere. Can you see what I'm doing wrong?

<?
$host = "localhost";
$user = "myuser";
$pass = "mypassword";
$dbname = "voters";

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
$dbname = "voters";
mysql_select_db($dbname);


$query= "select * from voters where lname=" . $_POST['lname'] . "; AND
fname="' . $_POST['fname'] . ""; AND dob=" . $_POST['dob'] . ""'"; 
echo $query;

$result= mysql_query($query);
$num_results = mysql_num_rows($result);

while ($row = mysql_fetch_array($result)) {
echo "<p>",$row['lname'], ": ",$row['fname'], ": ",$row['dob'];
}

?> 

Am I way off base here or just a few feet

Thanks again for all your help.
Last edited by rickarro : Jan 14th, 2008 at 3:44 pm.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 239
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Need Help

  #7  
Jan 14th, 2008
Yep. You are wrong in writing the query.
  1. <?
  2. $host = "localhost";
  3. $user = "myuser";
  4. $pass = "mypassword";
  5. $dbname = "voters";
  6.  
  7. $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
  8. $dbname = "voters";
  9. mysql_select_db($dbname);
  10.  
  11. $lname=$_POST['lname'];
  12. $fname=$_POST['fname'];
  13. $dob=$_POST['dob'];
  14.  
  15. $query= "select * from voters where lname='$lname' AND
  16. fname='$fname' AND dob='$dob'";
  17. echo $query;
  18.  
  19. $result= mysql_query($query);
  20. $num_results = mysql_num_rows($result);
  21.  
  22. while ($row = mysql_fetch_array($result)) {
  23. echo "<p>",$row['lname'], ": ",$row['fname'], ": ",$row['dob'];
  24. }
  25.  
  26. ?>

Check this. Don't you think this is much easier than using $_POST in your query ? I do it this way. I first assign the values of the $_POST['variable'] to a php variable, then use that php variable(in this case, $fname,$lname and $dob). And, you shouldn't end your query (with a semicolon) after every condition.
Last edited by nav33n : Jan 14th, 2008 at 4:22 pm.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Jan 2008
Posts: 76
Reputation: rickarro is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster in Training

Re: Need Help

  #8  
Jan 14th, 2008
Originally Posted by nav33n View Post
Yep. You are wrong in writing the query.
  1. <?
  2. $host = "localhost";
  3. $user = "myuser";
  4. $pass = "mypassword";
  5. $dbname = "voters";
  6.  
  7. $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
  8. $dbname = "voters";
  9. mysql_select_db($dbname);
  10.  
  11. $lname=$_POST['lname'];
  12. $fname=$_POST['fname'];
  13. $dob=$_POST['dob'];
  14.  
  15. $query= "select * from voters where lname='$lname' AND
  16. fname='$fname' AND dob='$dob'";
  17. echo $query;
  18.  
  19. $result= mysql_query($query);
  20. $num_results = mysql_num_rows($result);
  21.  
  22. while ($row = mysql_fetch_array($result)) {
  23. echo "<p>",$row['lname'], ": ",$row['fname'], ": ",$row['dob'];
  24. }
  25.  
  26. ?>

Check this. Don't you think this is much easier than using $_POST in your query ? I do it this way. I first assign the values of the $_POST['variable'] to a php variable, then use that php variable(in this case, $fname,$lname and $dob). And, you shouldn't end your query (with a semicolon) after every condition.



Yes that is much easier and cleaner, thanks. However, after cleaning it up as you stated, i'm still getting this error/display instead of the printed product....
select * from voters where lname='doe' AND fname='john' AND dob='20080126'
I've gone over it several times and can't seem to find the culprit. Also, i'm having to put "<?php" at the top instead of "<?" or all it shows me is the scripting, this must have something to do with my .config set up.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 239
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Need Help

  #9  
Jan 14th, 2008
hmm.. Its showing the query because you are printing it (echo $query; ). I don't see anything wrong with the query or the script. The problem might be with your php configuration.
Last edited by nav33n : Jan 14th, 2008 at 11:57 pm.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Jan 2008
Posts: 76
Reputation: rickarro is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster in Training

Re: Need Help

  #10  
Jan 15th, 2008
Ok, i'll play with it some more. I was able to get actual results out of it, so it is printing the proper results, but it is still printing the script as well. Hmmmm (rubs chin). Thank you for all your help, i really appreciate it. I never thought i would get this far but thanks to you !!!!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 12:45 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC