fishfin Posted October 12, 2007 Share Posted October 12, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72908-this-code-wont-work/ Share on other sites More sharing options...
ShoeLace1291 Posted October 12, 2007 Share Posted October 12, 2007 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())); Quote Link to comment https://forums.phpfreaks.com/topic/72908-this-code-wont-work/#findComment-367695 Share on other sites More sharing options...
fishfin Posted October 12, 2007 Author Share Posted October 12, 2007 I tried that and got this error message: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\post.php on line 34 Error: Either the username or password was incorrect. Please try again. Quote Link to comment https://forums.phpfreaks.com/topic/72908-this-code-wont-work/#findComment-367711 Share on other sites More sharing options...
kenrbnsn Posted October 12, 2007 Share Posted October 12, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/72908-this-code-wont-work/#findComment-367728 Share on other sites More sharing options...
fishfin Posted October 12, 2007 Author Share Posted October 12, 2007 Thanks! that solved that problem but now for some reason it won't save the information to the database. Quote Link to comment https://forums.phpfreaks.com/topic/72908-this-code-wont-work/#findComment-367771 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.