PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
dwfait
Forum Contributor
Posts: 113 Joined: Sun Aug 01, 2004 10:36 pm
Post
by dwfait » Mon Aug 02, 2004 9:19 am
Hi, in my code, i have a query, using $_POST values as WHERE values. If there is a match, how do i get the value from another field in the match?
Here is the code:
Code: Select all
<?php
$ssppluser = $_POSTї"user"];
$sspplpass = $_POSTї"pass"];
$link = mysql_connect("localhost", "root", "")
or die("Could not connect");
mysql_select_db("stormst_sspp")
or exit("Could not select database");
$result = mysql_query("SELECT user AND pass FROM sspp WHERE user='$ssppluser' AND pass='$ssppluser'")
or die ("Invalid query");
$num_rows = mysql_num_rows($result);
If ("$num_rows"==1) {
echo "Login Complete.";
} else {
echo "Bad username / password.";
}
mysql_close($link);
?>
There will only be one correct search result, as it is a login scrupt. So how would i get the field value 'id' from the row that was returned by the query, if there was a row returned?
nigma
DevNet Resident
Posts: 1094 Joined: Sat Jan 25, 2003 1:49 am
Post
by nigma » Mon Aug 02, 2004 10:39 am
Make your query something like:
Code: Select all
select id from sspp where user='user' and pass='pass'
Then later on:
Code: Select all
if (mysql_num_rows($query_result) == 1) {
$row = mysql_fetch_array($query_result);
$id = $row['id'];
echo "Your id is $id";
}
else {
// output other message
}
Let me know if you need clearification
dwfait
Forum Contributor
Posts: 113 Joined: Sun Aug 01, 2004 10:36 pm
Post
by dwfait » Mon Aug 02, 2004 10:58 am
hmm, when i incorporate this into my code, the variables seem to be null. Here is the code:
Code: Select all
<?php
if (is_null($action)) {
?>
<form action="index.php?action=login" method="POST">
Your Username: <input type="text" name="user" />
Your Password: <input type="text" name="pass" />
<input type="submit">
</form>
<?php
} else if ($action=='login') {
$ssppluser = $_POST["user"];
$sspplpass = $_POST["pass"];
$link = mysql_connect("localhost", "root", "")
or die("Could not connect");
mysql_select_db("stormst_sspp")
or exit("Could not select database");
$result = mysql_query("SELECT user AND pass FROM sspp WHERE user='$ssppluser' AND pass='$ssppluser'")
or die ("Invalid query");
$num_rows = mysql_num_rows($result);
If ($num_rows==1) {
$row = mysql_fetch_array($result);
$id = $row['id'];
$staff = $row['staff'];
echo "Login Complete. User ID: $id , Staff: $staff";
?>
<br>
<a href="index.php?action=loginc&user=$ssppluser">Click here to continue</a>
<?php
} else {
echo "Bad username / password.";
}
mysql_close($link);
?>
<?php
} else if ($action=='loginc') {
echo "Welcome $user";
}
?>
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Mon Aug 02, 2004 11:08 am
Code: Select all
//......skipped....
mysql_query("select id, staff from sspp WHERE user='$ssppluser' AND pass='$ssppluser'");
if(mysql_num_rows()>0) { // sucessful login
list($id, $staff) = mysql_fetch_row();
echo "ID: $id, Staff: $staff";
}
dwfait
Forum Contributor
Posts: 113 Joined: Sun Aug 01, 2004 10:36 pm
Post
by dwfait » Mon Aug 02, 2004 11:35 am
Unfortunatly, that code did not work, but with a litle tinkering i got it to work by doing this:
Code: Select all
$result=mysql_query("select id, staff from sspp WHERE user='$ssppluser' AND pass='$ssppluser'");
if (mysql_num_rows($result)>0) {
list($id, $staff) = mysql_fetch_row($result);
echo "ID: $id, Staff: $staff";
Thanks for your help!
nigma
DevNet Resident
Posts: 1094 Joined: Sat Jan 25, 2003 1:49 am
Post
by nigma » Mon Aug 02, 2004 12:02 pm
dwfait: the reason it didn't work is because you never altered your query so that it would return the fields you were trying to access (id and staff).