ianhaney Posted December 31, 2015 Share Posted December 31, 2015 Hi Sorry I have been trying to work this one out for a while, in a php page, I want to be able to have the php page check if a db table column has a value of 1 in the paid column and if it has to stay on the same php page and if it has a 0 to redirect to another php page Is that possible? Thank you in advance Ian Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/ Share on other sites More sharing options...
ginerjm Posted December 31, 2015 Share Posted December 31, 2015 This may be semantics but what you are saying is you want a script to check a db table and depending upon what it is, go to a specific page. As you wrote it you make it sound like you are doing this while looking at the page which you can't do since php is running server-side. Of course the whole idea is elementary. Do your query. Write an if statement. Make a header() call to the appropriate script/page. Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/#findComment-1528898 Share on other sites More sharing options...
ianhaney Posted December 31, 2015 Author Share Posted December 31, 2015 (edited) Thank you for the reply, appreciate it I was thinking if it would be something like the following <?php if($paid == 1) { header: (stay on page); } else { header: (redirect to another page); ?> Knowing me I am prob way off Edited December 31, 2015 by ianhaney Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/#findComment-1528899 Share on other sites More sharing options...
maxxd Posted December 31, 2015 Share Posted December 31, 2015 You don't need a header to stay on the same page. Check if $paid == 0; if it does, redirect the user. If not, just continue the script execution on that page. Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/#findComment-1528900 Share on other sites More sharing options...
ginerjm Posted December 31, 2015 Share Posted December 31, 2015 You need to be careful how you use the words page and script. The script is where your code resides. IMHO, the 'page' is what you see on the client screen. When your code above executes you make a choice between showing page 1 or page 2. Now if the script is triggered from a URL then the two headers that you propose are correct. If you are in the running script with the above code because say, page 1, triggered it, and your script decides that page 1 needs to be displayed again, then let your script do that like it already did perhaps. If the other page is desired, then the header call will do it for you. This question makes me wonder what you are thinking about since it is a pretty elementary concept. Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/#findComment-1528903 Share on other sites More sharing options...
ianhaney Posted December 31, 2015 Author Share Posted December 31, 2015 (edited) Hmm think I have got it wrong somehow it don't seem to go to the job advert page, it redirects to the profile page instead, it don't seem to be checking the script, below is what I have at the top of my php job advert page <?php ini_set('display_startup_errors',1); ini_set('display_errors',1); error_reporting(-1); // connect to the database include("connect-db.php"); if($paid == 0) { header("Location: employer-profile.php"); } ?> Edited December 31, 2015 by ianhaney Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/#findComment-1528904 Share on other sites More sharing options...
ianhaney Posted December 31, 2015 Author Share Posted December 31, 2015 Sorry will remember to say either page or script It does seem simple but I can't work it out Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/#findComment-1528905 Share on other sites More sharing options...
Barand Posted December 31, 2015 Share Posted December 31, 2015 I am guessing it is to do with another post of yours (http://forums.phpfreaks.com/topic/300074-paypal-payment-access-page-issue/?do=findComment&comment=1528883). If so it is not helpful for others if you keep spreading the problem over separate posts so that we have to search the forum for relevant information Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/#findComment-1528906 Share on other sites More sharing options...
ianhaney Posted December 31, 2015 Author Share Posted December 31, 2015 Sorry yeah is related to that one but think I sussed that issue on the forum post below as it is updating in the db table http://forums.phpfreaks.com/topic/300074-paypal-payment-access-page-issue/?do=findComment&comment=1528883 Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/#findComment-1528907 Share on other sites More sharing options...
ginerjm Posted December 31, 2015 Share Posted December 31, 2015 Where is $paid given a value? Your posted code doesn't set it here. And if as Barand says you have code elsewhere, I will step away since I don't like to waste my time. Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/#findComment-1528908 Share on other sites More sharing options...
ianhaney Posted December 31, 2015 Author Share Posted December 31, 2015 I now have the following script in the job advert php page and payment has been completed as has a 1 in the paid column in the db table but it still redirects to the profile page instead of staying on the job advert page below is the whole script from the job advert php page <?php ini_set('display_startup_errors',1); ini_set('display_errors',1); error_reporting(-1); // connect to the database include("connect-db.php"); $paid = ''; if($paid == 0) { header("Location: employer-profile.php"); } ?> <?php require('config-db-employers.php'); //if not logged in redirect to login page if(!$user->is_logged_in()){ header('Location: employer-login.php'); } ?> <?php /* Allows the user to both create new records and edit existing records */ // connect to the database include("connect-db.php"); // creates the new/edit record form // since this form is used multiple times in this file, I have made it a function that is easily reusable function renderForm($memberid = '', $ref = '', $role ='', $division ='', $location ='', $salary ='', $description ='', $datecreated ='', $dateupdated ='', $error = '', $id = '') { ?> <?php $title = "Add/Edit Job Advert - Security Site"; include ( 'includes/header.php' ); ?> <!--CONTENT--> <div id="container"> <div class="profile-left"> <?php if ($error != '') { echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error . "</div>"; } ?> <form action="" method="post"> <div> <?php if ($id != '') { ?> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <p>ID: <?php echo $id; ?></p> <?php } ?> <label>Ref: <input type="text" name="ref"value="<?php echo $ref; ?>"/> </label> <label>Role: <input type="text" name="role" value="<?php echo $role; ?>"/> </label> <label>Division: <input type="text" name="division" value="<?php echo $division; ?>"/> </label> <label>Location: <input type="text" name="location" value="<?php echo $location; ?>"/> </label> <label>Salary: <input type="text" name="salary" value="<?php echo $salary; ?>"/> </label> <label>Description:</label> <textarea name="description"><?php echo $role; ?></textarea> <input type="submit" name="submit" value="Add Job" class="button" /> </div> </form> <?php } $memberid = $_SESSION["memberID"]; // store the user id into session /* EDIT RECORD */ // if the 'id' variable is set in the URL, we know that we need to edit a record if (isset($_GET['id'])) { // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { // make sure the 'id' in the URL is valid if (is_numeric($_POST['id'])) { // get variables from the URL/form $id = $_POST['id']; $ref = htmlentities($_POST['ref'], ENT_QUOTES); $role = htmlentities($_POST['role'], ENT_QUOTES); $division = htmlentities($_POST['division'], ENT_QUOTES); $location = htmlentities($_POST['location'], ENT_QUOTES); $salary = htmlentities($_POST['salary'], ENT_QUOTES); $description = htmlentities($_POST['description'], ENT_QUOTES); // check that firstname and lastname are both not empty if ($ref == '' || $role == '' || $division == '' || $location == '' || $salary == '' || $description == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($ref, $role, $division, $location, $salary, $description, $error, $id); } else { // if everything is fine, update the record in the database if ($stmt = $mysqli->prepare("UPDATE jobs SET memberID = ?, ref = ?, role = ?, division = ?, location = ?, salary = ?, description = ?, datecreated = ?, dateupdated = ? WHERE id=?")) { $stmt->bind_param("issssssssi", $memberid, $ref, $role, $division, $location, $salary, $description, $datecreated, $dateupdated, $id); $stmt->execute(); $stmt->close(); } // show an error message if the query has an error else { echo "ERROR: could not prepare SQL statement."; } // redirect the user once the form is updated header("Location: employer-profile.php"); } } // if the 'id' variable is not valid, show an error message else { echo "Error!"; } } // if the form hasn't been submitted yet, get the info from the database and show the form else { // make sure the 'id' value is valid if (is_numeric($_GET['id']) && $_GET['id'] > 0) { // get 'id' from URL $id = $_GET['id']; // get the recod from the database if($stmt = $mysqli->prepare("SELECT * FROM jobs WHERE id=?")) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->bind_result($memberid, $id, $ref, $role, $division, $location, $salary, $description, $datecreated, $dateupdated); $stmt->fetch(); // show the form renderForm($memberid, $ref, $role, $division, $location, $salary, $description, $datecrated, $dateupdated, NULL, $id); $stmt->close(); } // show an error if the query has an error else { echo "Error: could not prepare SQL statement"; } } // if the 'id' value is not valid, redirect the user back to the view.php page else { header("Location: employer-profile.php"); } } } /* NEW RECORD */ // if the 'id' variable is not set in the URL, we must be creating a new record else { // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { // get the form data $ref = htmlentities($_POST['ref'], ENT_QUOTES); $role = htmlentities($_POST['role'], ENT_QUOTES); $division = htmlentities($_POST['division'], ENT_QUOTES); $location = htmlentities($_POST['location'], ENT_QUOTES); $salary = htmlentities($_POST['salary'], ENT_QUOTES); $description = htmlentities($_POST['description'], ENT_QUOTES); // check that firstname and lastname are both not empty if ($ref == '' || $role == '' || $division == '' || $location == '' || $salary == '' || $description == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($ref, $role, $division, $location, $salary, $description, $error); } else { $datecreated = date ('Y-m-d H:i:s'); // insert the new record into the database if ($stmt = $mysqli->prepare("INSERT jobs (memberID, ref, role, division, location, salary, description, datecreated) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")) { $stmt->bind_param("ssssssss", $memberid, $ref, $role, $division, $location, $salary, $description, $datecreated); $stmt->execute(); $stmt->close(); } // show an error if the query has an error else { echo "ERROR: Could not prepare SQL statement."; } // redirec the user header("Location: employer-profile.php"); } } // if the form hasn't been submitted yet, show the form else { renderForm(); } } // close the mysqli connection $mysqli->close(); ?> <?php echo "<div class='home-content'>"; echo "<center><div style='float: left;'><a href='employer-logout.php'>Log Out</a></div></center>"; echo "</div>"; echo "<br><br>"; ?> </div> <div class="profile-right"> </div> </div> <!--CONTENT--> <?php include( 'includes/footer.php' ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/#findComment-1528909 Share on other sites More sharing options...
maxxd Posted January 1, 2016 Share Posted January 1, 2016 (edited) $paid = ''; if($paid == 0) { header("Location: employer-profile.php"); } $paid will never equal 0 if you just set it to an empty string the line above the if() statement... Edited January 1, 2016 by maxxd Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/#findComment-1528924 Share on other sites More sharing options...
Barand Posted January 1, 2016 Share Posted January 1, 2016 Actually, doing that means $paid is always == 0 (but not === 0) $paid = ''; if ($paid == 0) { echo "Not paid"; // always echoes } Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/#findComment-1528925 Share on other sites More sharing options...
maxxd Posted January 1, 2016 Share Posted January 1, 2016 Actually, doing that means $paid is always == 0 (but not === 0) $paid = ''; if ($paid == 0) { echo "Not paid"; // always echoes } That's what I get for posting early in the morning. Good point. Quote Link to comment https://forums.phpfreaks.com/topic/300078-check-value-of-1-or-0-in-db-and-redirect/#findComment-1528927 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.