Jump to content

This code won't work


fishfin

Recommended Posts

For some reason this code wont work:

 

<?php
$db_host = "localhost";
$db_user = "root";
$db_password = "apass";
$db_name = "database";

if($_POST['submit'])
{
if(!$_POST['username'])
{		echo "Error: Unable to make post. You must enter a username in order to make a post.";
	die;
}
if(!$_POST['password'])
{
	echo "Error: Unable to make post. You must enter a password in order to make a post.";
	die;
}
if(!$_POST['content'])
{
	echo "Error: Unable to make post. You must have something to post in order to make a post.";
	die;
}

$date = date("h:i A dS M");
$username = $_POST['username'];
$content = $_POST['content'];

$content = strip_tags($_POST['content'],'');

mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());

mysql_select_db($db_name) or die(mysql_error());
$password = mysql_query("SELECT password FROM users WHERE username='$username' LIMIT 1") or die(mysql_error());

echo $password;

if($_POST['password'] != $password)
{
	echo "Error: Either the username or password was incorrect. Please try again.";
	die;
}

mysql_query("INSERT INTO posts (content, user, date, ip) VALUES ('$content', '$username', '$date', '$_SERVER[REMOTE_ADDR]'");

echo "You have successfully made a post.";
}
?>

<form method="post" action="post.php">
Username <input type="text" name="username">
<br>
<br>
Password <input type="text" name="password">
<br>
<br>
Post:
<br>
<textarea name="content" rows="5" cols="40">
</textarea>
<br>
<input type="submit" name="submit" value="post">
</form>

 

I always get the "Error: Either the username or password was incorrect. Please try again." message and it displays the password that it retreved from the database as "Resource id #3."

 

I have already made the mysql database and I have double checked to make sure that the username and password that I am using is already in the database.

Link to comment
https://forums.phpfreaks.com/topic/72908-this-code-wont-work/
Share on other sites

Try changing

 

$password = mysql_query("SELECT password FROM users WHERE username='$username' LIMIT 1") or die(mysql_error());

 

to

 

$password = mysql_fetch_array(mysql_query("SELECT password FROM users WHERE username='$username' LIMIT 1") or die(mysql_error()));

Link to comment
https://forums.phpfreaks.com/topic/72908-this-code-wont-work/#findComment-367695
Share on other sites

When debugging, don't stack calls:

 

Instead of

<?php
$password = mysql_query("SELECT password FROM users WHERE username='$username' LIMIT 1") or die(mysql_error());
?>

 

do something like:

<?php
        $q = "SELECT password FROM users WHERE username='$username' LIMIT 1";
$rs = mysql_query($q) or die("Problem with the query: $q <br>" . mysql_error());
        $rw = mysql_fetch_assoc($rs);
        $password = $rw['password'];
?>

 

Note: the mysql_query() function just performs the query, it doesn't retrieve any data. You need to fetch the data and then get your data from the fetched array.

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/72908-this-code-wont-work/#findComment-367728
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.