-
need help in php
Hi,
i am trying to make a simple script in php.. right now i can add to database to mySql..
my First code in adding data is here:
PHP Code:
<?
//replace username and password with your mysql name and password
$conn = mysql_connect("localhost","USERNAME","PASSWORD");
//select the database
$db = mysql_select_db("wwwtest_db");
$username = $_POST["username"];
$password = $_POST["password"];
$credits = $_POST["credits"];
//insert the values
$result= MYSQL_QUERY("INSERT INTO users (id, username, password, credits)".
"VALUES ('NULL', '$username', '$password', '$credits')");
echo "Username and password with credits submitted ok.";
?>
Now, my problem is here when im trying to logon is ok but if i write wrong username and password is not giving error but need answer is this ("username and password is invalid"); ..
the code is here:
PHP Code:
<?
$conn = mysql_connect("localhost","USERNAME","PASSWORD");
$db = mysql_select_db("wwwtest_db");
$username = $_POST["username"];
$password = $_POST["password"];
$result = MYSQL_QUERY("SELECT * from users WHERE username='$username'and
password='$password'")
or die ("username and password is invalid");
$worked = mysql_fetch_array($result);
$username = $worked[username];
$password = $worked[password];
$email = $worked[email];
if($worked)
echo "Welcome $user! Your Credit left is $credits";
?>
-
Re: need help in php
You need to first ensure that you escape your variables. Second the result returned by mysql_query only returns false if a query fails. A query can succeed and return no results.
mysql_num_rows() will help you here.