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

Hi.

So, why don't you make such kind of query statement ?

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'"; :)

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.

You are welcome! And yep, you can use both AND and && .

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.

Yep. You are wrong in writing the query.

<?
$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);

$lname=$_POST['lname'];
$fname=$_POST['fname'];
$dob=$_POST['dob'];

$query= "select * from voters where lname='$lname' AND
fname='$fname' AND dob='$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'];
}

?>

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 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.

Yep. You are wrong in writing the query.

<?
$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);

$lname=$_POST['lname'];
$fname=$_POST['fname'];
$dob=$_POST['dob'];

$query= "select * from voters where lname='$lname' AND
fname='$fname' AND dob='$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'];
}

?>

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 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.

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.

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 !!!!

You are welcome! Check your php.ini (c:\php\php.ini or c:\wamp\apache2\bin\php.ini, if you are using Wamp) file for the line short_open_tag. If it is off, turn it On. If short open tags are turned off and if you have short open tags in your script, it will print the script. Thats why its always better to use <?php instead of <? !

And yep, You will learn in no time. Php is really easy ! :)

In my script, I had the following:

$query= "select * from voter where lname='$lname' AND
fname='$fname' AND dob='$dob'";
echo $query;

by slashing out the "echo $query;" at the end of that, i removed the printing
of the script in my browser (i.e., //echo $query;) and all started working fine. So I take it that "echo $query;" should not have been there?

Also, it works fine now that i've done that but only in IE, but not in other browsers, why is that?

lol.. echo is used to output something to the screen :P . It should work in all browsers. http://localhost/filename.php should work fine in any browser. :)

I restarted the browser and it is working now. :)
Thats whats great about being new....everything looks wrong :)
Really appreciate the help, each day a new mountain is climbed, so
i'm off to my next mountain. I'm sure I'll be in touch ;)

Ok, i'm climbing my next mountain :) I want to add, i think, an "if" statement. Basically if the select query finds data...display it, if not, give an error message. I tried this....

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

}
If ($connection = mysql_connect ($host,$user,$pass)= "Null" {
echo "<p>","There is no data";
}
?>

Obviously it didn't work. Should I be putting an if/else statement where
the "While" is or doing something similar to what i've got here. Sorry, i know this is basic stuff
but i've got to start somewhere.

You should have your connection string before any sql operation. So, it would be like,

$connection=mysql_connect($host,$user,$pass);
if(!$conn) {
 echo "Error connecting.";
} else {
 echo "Connected.";
}

Instead of using if and else, you can use die function with mysql_error function to know the error in your mysql statement. die ends the execution of the script. (Its an alias of exit). $connection = mysql_connect($host,$user,$pass) or die(mysql_error()); :) Cheers,
Naveen

You should have your connection string before any sql operation. So, it would be like,

$connection=mysql_connect($host,$user,$pass);
if(!$conn) {
 echo "Error connecting.";
} else {
 echo "Connected.";
}

Instead of using if and else, you can use die function with mysql_error function to know the error in your mysql statement. die ends the execution of the script. (Its an alias of exit). $connection = mysql_connect($host,$user,$pass) or die(mysql_error()); :) Cheers,
Naveen

I understand the "or die" but I want the user to get a more pleasing response than page could not be found. What its really doing is saying if your fname, lname, & dob are found in the database, "Congrats we have you listed", if they are not in, "please come and see us to get your records straight". Does that make sense?

I've got the "Congrats" part down and all is working well, I just need to say "ok, the input was not found in the database so a nice informative reply is shown to the user.
I really hope this makes sense :)

$result=mysql_query($query);
$found=mysql_num_rows($result);
if($found > 0 ){
echo "Record found";
} else {
echo "Record not found.";
}

That will do.

commented: A great help and very understanding to us newbie's. +1
$result=mysql_query($query);
$found=mysql_num_rows($result);
if($found > 0 ){
echo "Record found";
} else {
echo "Record not found.";
}

That will do.

Outstanding, it worked great, ty. I think i had the right idea, just the wrong place. Just for the sake of learning, you changed "$num_results" to "$found", it would work either way but "$found" is more for descriptive purposes, is this right?

Yup :)

You can alternativelly do this to retrieve the values from the page..

create a page something.html and add this

<html>
<head>
          <title>Testing</title>
</head>
<body>
         <form action="retrieve.php" method="post">

<input type="text" name="name">
<input type="submit" name="submit" value="Submit">
       </form>
</body>
</html>

Then this in retrieve.php

<?php

       $name = $_REQUEST['name'];
       echo "The value is <b>$name</b>";
?>

That simple.:)

You can alternativelly do this to retrieve the values from the page..

create a page something.html and add this

<html>
<head>
          <title>Testing</title>
</head>
<body>
         <form action="retrieve.php" method="post">

<input type="text" name="name">
<input type="submit" name="submit" value="Submit">
       </form>
</body>
</html>

Then this in retrieve.php

<?php

       $name = $_REQUEST['name'];
       echo "The value is <b>$name</b>";
?>

That simple.:)

in ur retrieve.php it should be

$name=$_POST;

and not $_REQUEST

in ur retrieve.php it should be

and not $_REQUEST

$_REQUEST can handle both $_POST and $_GET. (And also $_COOKIES).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.