Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 7th, 2003, 09:20 AM
Authorized User
 
Join Date: Jul 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can't check data from a form. (Like password)

Below is the code I use but it is not working.

<?php global $HTTP_POST_VARS; if (isset($HTTP_POST_VARS['mybutton'])) {
$Link = mysql_connect (“localhost”, '***', '***');
mysql_select_db (“database”, $Link);
$query_to_check = "SELECT Forename,Surname,Address,Postcode,Doctor FROM patients WHERE
Forename = '$HTTP_POST_VARS[forename]' AND
Surname = '$HTTP_POST_VARS[surname]' AND
Address = '$HTTP_POST_VARS[address]' AND
Postcode = '$HTTP_POST_VARS[postcode]' AND
    Doctor = '$HTTP_POST_VARS[doctornames]'";
/* Above I am checking whether data supplied by the form is identical to data in mysql database. Then if $query_to_check brings a result I want to add the data submitted by the form to another table. Here, I have to insert a function which lets the next function to execute if the supplied data is true. It is like username and password checking. Could you help me?
*/

$Result = mysql_query ($query_to_check, $Link);
if ($Result) {
print "The query1 was successfully executed!<BR>\n";
print "
<table border=\"1\"><tr><th>Forename</th><th>Surname</th>
<th>Address</th><th>Postcode</th><th>Doctor</th></tr>";

for ($i=0; $i<mysql_num_rows($Result); $i++) {
$row = mysql_fetch_assoc($Result);
print "<tr>";
print "<td>".$row['Forename']."</td>";
print "<td>".$row['Surname']."</td>";
print "<td>".$row['Address']."</td>";
print "<td>".$row['Postcode']."</td>";
print "<td>".$row['Doctor']."</td>";
print "</tr>";
}
print "</table><br><br>\n";
} // end of if ($Result)
else {
print "The query1 could not be executed!<BR>\n";
echo mysql_errno().": ".mysql_error()."<BR>\n";
}
mysql_close($Link);
} // end of if (isset)
else {
echo "you must have come here from somewhere else.\n";
}


 
Old August 7th, 2003, 11:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default


first of all you don't have to declare $HTTP_POST_VARS as global variables. that array is superglobal... as of your question...

<?

if(isset($HTTP_POST_VARS["mybutton"])) {

//connect to database
$conn = mysql_connect('localhost','***','***');

//select database
mysql_select_db("database",$conn);

if(isset($HTTP_POST_VARS["mybutton"])) {

//assign variables to elements in $HTTP_POST_VARS array

$forename = $HTTP_POST_VARS["forename"];
$surname = $HTTP_POST_VARS["surname"];
$address = $HTTP_POST_VARS["address"];

//... and the rest of it....

/*to check if the user has access (which means his/her data exists in the database), do the following... (say field in your form where user types his username and password is named "username_from_form" and "password_from_form" */

$username_from_form = $HTTP_POST_VARS["username_from_form"];
$password_from_form = $HTTP_POST_VARS["password_from_form"];

$sql = "SELECT username, password FROM members WHERE username LIKE '$username_from_form' AND password LIKE '$password_from_form'";

if(mysql_num_rows(mysql_query($sql,$conn)) == 1) {
    //user exists in the database, mysql_num_rows() returns
    //the number of rows affected by your last mysql_query() }
else {
    echo ("access denied");
    exit; }

} // closing block

hope that helped :D:D:D:):)



the genuine genius
 
Old August 7th, 2003, 01:09 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Moharo, Acutally, $_POST is a superglobal.
And $HTTP_POST_VARS is just a regular global.

But I don't see what necessitates the use of the 'global' keyword here anyway.

Scifo, your code is also sloppy and hard to sort through.

Code:
<?php 

if (isset($HTTP_POST_VARS['mybutton'])) {

$Link = mysql_connect (“localhost”, '***', '***');
mysql_select_db (“database”, $Link);

$query_to_check = "SELECT Forename,Surname,Address,Postcode,Doctor FROM patients WHERE 
Forename = '{$HTTP_POST_VARS["forename"]}' AND 
Surname = '{$HTTP_POST_VARS["surname"]}' AND 
Address = '{$HTTP_POST_VARS["address"]}' AND 
Postcode = '{$HTTP_POST_VARS["postcode"]}' AND 
Doctor = '{$HTTP_POST_VARS["doctornames"]}'"; 

$Result = mysql_query ($query_to_check, $Link);

    if (!empty($Result)) {


        print "The query1 was successfully executed!<BR>\n";
        print "<table border=\"1\"><tr><th>Forename</th><th>Surname</th>
            <th>Address</th><th>Postcode</th><th>Doctor</th></tr>";

        # Since you are reading all the results anyway, do this instead,
        # mysql_fetch_array fetches all the results of the query

        while ($row = mysql_fetch_array($Result)) {

            print "<tr>";
            print "<td>".$row['Forename']."</td>";
            print "<td>".$row['Surname']."</td>";
            print "<td>".$row['Address']."</td>";
            print "<td>".$row['Postcode']."</td>";
            print "<td>".$row['Doctor']."</td>";
            print "</tr>";

        }


            print "</table><br><br>\n";

    } else {

        print "The query1 could not be executed!<BR>\n";
        echo mysql_errno().": ".mysql_error()."<BR>\n";

    }
        mysql_close($Link);
} else {
 
    echo "you must have come here from somewhere else.\n";

}
?>
: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old August 7th, 2003, 01:31 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Scifo, you've posted several topics on similar subjects in the past few days.

Firstly, what are you gaining if every time you get stuck you post your code for us to fix? Its quite understandable, that's what we're here for... but I don't think that you are making a great enough effort to think things through for yourself first. A real understanding of the concepts may only come after thinking through a problem for yourself.
Second- Your code is sloppy and hard to sort through. If you want help your more likely to get it if you make your posts as clear and easy to read as possible. Format them in Notepad first and copy and paste them.
Third - Your variable naming is not consistent which is not only annoying but could give rise to strange bugs in your programming future, but I've already mentioned this before.

Spend some quality time with your PHP book or books. And have more confidence in your own ability to solve problems. Do things consistently and make your code organized and easy to read. Indent if statements, loops and functions. If you are going to capitalize one variable, capitalize them all.

Finally a database expects the same format for any string being inputted be it an INSERT, SELECT, UPDATE, etc.:

$query_to_check = "SELECT Forename,Surname,Address,Postcode,Doctor FROM patients WHERE
Forename = '{$HTTP_POST_VARS["forename"]}' AND
Surname = '{$HTTP_POST_VARS["surname"]}' AND
Address = '{$HTTP_POST_VARS["address"]}' AND
Postcode = '{$HTTP_POST_VARS["postcode"]}' AND
Doctor = '{$HTTP_POST_VARS["doctornames"]}'";

Stick with curly base syntax and quote your strings (in the arrays and around the braces)!

: )
Rich






:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check the password field for case-sensitive zubash73 ASP.NET 2.0 Basics 4 November 27th, 2016 02:47 AM
Check for Duplicate Records in Data Entry Form roznix Access VBA 5 June 7th, 2012 08:53 AM
How to check programmatically check the password? thomaskelly ASP.NET 1.0 and 1.1 Basics 1 May 16th, 2008 08:49 PM
Check Windows-Password Bernd VS.NET 2002/2003 1 October 6th, 2005 12:32 PM
cannot check data provided by a form scifo Beginning PHP 1 August 6th, 2003 11:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.