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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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