Panta Posted February 5, 2018 Share Posted February 5, 2018 Please i dont know why the my little code do not run the header function - header('Location: http://www.example.com/'); Please i need ans. thanks <?php session_start(); //start the session for the page include("db.php"); //include database file //Check if page was entered by a submit button $user_name=$_POST['user_name']; //Get username !!FROM FORM!! $password=($_POST['password']); //Get name !!FROMFORM!! if (!empty($user_name) && !empty($password)) { $ck=$flash->prepare("SELECT * FROM `data` WHERE `user`=:user_name AND `password`=:password "); //get rows where the username or email address is allready registered $ck->bindParam(':user_name',$user_name); $ck->bindParam(':password',$password); $ck->execute(); //if email address allready excists if($ck->rowCount() > 0) { header('Location: http://www.example.com/'); }} ?> Link to comment Share on other sites More sharing options...
iwato Posted February 5, 2018 Share Posted February 5, 2018 Eliminate all blank spaces before the header() function is invoked. There should be no blank lines, and no blank spaces after each EOL delimiter. This is, by far, the most common source of a failed header() function. Well, at least in my experience. Roddy 1 Link to comment Share on other sites More sharing options...
justsomeguy Posted February 5, 2018 Share Posted February 5, 2018 What is $flash? Is that a PDO object? 1 Link to comment Share on other sites More sharing options...
Panta Posted February 10, 2018 Author Share Posted February 10, 2018 On 2/5/2018 at 7:19 PM, justsomeguy said: What is $flash? Is that a PDO object? yes is PDO object. Link to comment Share on other sites More sharing options...
Ingolme Posted February 10, 2018 Share Posted February 10, 2018 The rowCount() method usually only tells you how many rows were updated or deleted in the most recent query. See details here: http://php.net/manual/en/pdostatement.rowcount.php If you want to check that there were any results, you should use the fetch() method. Since all you're doing is checking that a row exists, it is inefficient to select *, because that's pulling all of the data in the row, which you don't need. Instead you should count the rows. // Count rows where the user exists $ck=$flash->prepare("SELECT COUNT(*) AS `amount` FROM `data` WHERE `user`=:user_name AND `password`=:password "); $ck->bindParam(':user_name',$user_name); $ck->bindParam(':password',$password); $ck->execute(); $row = $cf->fetch(PDO::FETCH_ASSOC); if($row['amount'] > 0) { header('Location: http://www.example.com/'); } 1 Link to comment Share on other sites More sharing options...
Panta Posted February 12, 2018 Author Share Posted February 12, 2018 On 2/10/2018 at 9:11 PM, Ingolme said: The rowCount() method usually only tells you how many rows were updated or deleted in the most recent query. See details here: http://php.net/manual/en/pdostatement.rowcount.php If you want to check that there were any results, you should use the fetch() method. Since all you're doing is checking that a row exists, it is inefficient to select *, because that's pulling all of the data in the row, which you don't need. Instead you should count the rows. // Count rows where the user exists $ck=$flash->prepare("SELECT COUNT(*) AS `amount` FROM `data` WHERE `user`=:user_name AND `password`=:password "); $ck->bindParam(':user_name',$user_name); $ck->bindParam(':password',$password); $ck->execute(); $row = $cf->fetch(PDO::FETCH_ASSOC); if($row['amount'] > 0) { header('Location: http://www.example.com/'); } I tried this and it will not relocate to the address Link to comment Share on other sites More sharing options...
Panta Posted February 12, 2018 Author Share Posted February 12, 2018 On 2/5/2018 at 3:58 PM, iwato said: Eliminate all blank spaces before the header() function is invoked. There should be no blank lines, and no blank spaces after each EOL delimiter. This is, by far, the most common source of a failed header() function. Well, at least in my experience. Roddy tried this and is not relocating Link to comment Share on other sites More sharing options...
iwato Posted February 12, 2018 Share Posted February 12, 2018 Insert the following code echo $row['amount']; after the following line of code $row = $cf->fetch(PDO::FETCH_ASSOC); and see if its value is, indeed, greater than zero. For, if it is not, you have not met the condition of the if-statement in which the header() function is located. Roddy 1 Link to comment Share on other sites More sharing options...
Ingolme Posted February 13, 2018 Share Posted February 13, 2018 You need to do some debugging. You can't just copy some code and give up when the code doesn't work. Look for solutions. You have one single condition to test for here: if($row['amount'] > 0) { header('Location: http://www.example.com/'); } If it redirects, then the count is greater than zero, if it does not redirect then the count is zero or less. You have to do some debugging to find out why this number is zero or less. To start off, print the number. Since it's not redirecting, my guess is that the number is zero, but you should check to make sure. Once you have verified that the number is actually zero then that's clear evidence that the database table does not have any rows that match your query. Maybe there's something wring with your query. The query has two inputs, $user_name and $password. Print out both their values, then check the database table for yourself using phpMyAdmin or similar software to see if there is a row that contains both of those values. There are two possibilities: The values you passed into the query are wrong or the database actually does not contain that data. I can't tell you why the code is not working, but I've just told you how you can find out. 1 Link to comment Share on other sites More sharing options...
Panta Posted February 16, 2018 Author Share Posted February 16, 2018 On 2/13/2018 at 2:53 AM, Ingolme said: You need to do some debugging. You can't just copy some code and give up when the code doesn't work. Look for solutions. You have one single condition to test for here: if($row['amount'] > 0) { header('Location: http://www.example.com/'); } If it redirects, then the count is greater than zero, if it does not redirect then the count is zero or less. You have to do some debugging to find out why this number is zero or less. To start off, print the number. Since it's not redirecting, my guess is that the number is zero, but you should check to make sure. Once you have verified that the number is actually zero then that's clear evidence that the database table does not have any rows that match your query. Maybe there's something wring with your query. The query has two inputs, $user_name and $password. Print out both their values, then check the database table for yourself using phpMyAdmin or similar software to see if there is a row that contains both of those values. There are two possibilities: The values you passed into the query are wrong or the database actually does not contain that data. I can't tell you why the code is not working, but I've just told you how you can find out. On 2/12/2018 at 4:23 PM, iwato said: Insert the following code echo $row['amount']; after the following line of code $row = $cf->fetch(PDO::FETCH_ASSOC); and see if its value is, indeed, greater than zero. For, if it is not, you have not met the condition of the if-statement in which the header() function is located. Roddy Thanks for effort, i printed it and the number is "1", i wish to know if there is another possible cause for it not redirecting Link to comment Share on other sites More sharing options...
iwato Posted February 16, 2018 Share Posted February 16, 2018 Yes. Is there anything in the included file db.php that produces printed output -- say, an echo(), print_r(), or similar such statement? Roddy 1 Link to comment Share on other sites More sharing options...
Panta Posted February 16, 2018 Author Share Posted February 16, 2018 5 minutes ago, iwato said: Yes. Is there anything in the included file db.php that produces printed output -- say, an echo(), print_r(), or similar such statement? Roddy thanks so much, i checked db.php and saw spaces causing it. Link to comment Share on other sites More sharing options...
iwato Posted February 16, 2018 Share Posted February 16, 2018 Great! I am glad that I could be of help. Now, hover over the heart and when you see the trophy, click on it! This will advance my W3Schools "notoriety".... Link to comment Share on other sites More sharing options...
Panta Posted February 16, 2018 Author Share Posted February 16, 2018 1 minute ago, iwato said: Great! I am glad that I could be of help. Now, hover over the heart and when you see the trophy, click on it! This will advance my W3Schools "notoriety".... done already Link to comment Share on other sites More sharing options...
iwato Posted February 16, 2018 Share Posted February 16, 2018 Thanks! I now rated as "Good", rather than "Neutral". Roddy Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now