Jump to content

check value of 1 or 0 in db and redirect


ianhaney

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by ianhaney
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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' ); ?>
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.