Jump to content

check values from 2 tables


ahaberman25
Go to solution Solved by ahaberman25,

Recommended Posts

I need to check values from 2 different tables and make sure the admin_id match then do 

if (value1 = value2) {

  //do something 

}

 

this is what I have so far. 

$admin_id = $user_data['id'];

$visits = "SELECT * FROM customers WHERE `admin_id` = $admin_id AND `visits` == $check_visits";
$check_visits = "SELECT * FROM deals WHERE `visits` >= $visits AND `admin_id` = $admin_id";

if ($visits = $check_visits) {
	send email
} 
Link to comment
Share on other sites

checkin.php 

<?php
ob_start();
include 'core/init.php';
protect_page();
// get value of id in URL querystring
$id = isset($_GET['id']) ? $_GET['id'] : null;


$tbl_name = "customers"; // Table name


if (!empty($id)) {
	// Retrieve user from database 
	$sql = "SELECT * FROM $tbl_name WHERE id='$id'";
	$result = mysql_query($sql);
	$user = mysql_fetch_array($result);

	if (empty($user)) {
		// You have no user, do something here to alert the user of this...
		echo '<h1>Your Membership is no longer active.</h1>' . PHP_EOL;
	} else {
		?>
		<form name="form1" id="mainForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
			<input type="hidden" name="id" value="<?php echo $id; ?>"/>
			<input type="submit" value="CHECK IN">
		</form>
		<?php
	}
}  

if (!empty($_POST['id'])) {
	$id = $_POST['id'];
	$query = "UPDATE $tbl_name SET `visits` = `visits` + 1 WHERE id = '$id'";
	mysql_query($query);

$admin_id = $user_data['id'];
$check_deal = "SELECT * FROM deals WHERE `admin_id` = $admin_id";
$check_visits = "";
$req_visits = "SELECT * FROM customers WHERE `admin_id` = $admin_id AND `visits` = $check_visits";


if ($visits = $check_visits) {
	$res = mysql_query("SELECT * FROM $tbl_name WHERE id='$id'");
	while($row = mysql_fetch_array($res))
  	{
 	echo "<p>You have earned " . $row['visits'] . " punch.</p>";


 	// Send notification email.
 	$to = $row['email']; // this is your Email address
    $from = "andrew@websbydrew.com"; // this is the sender's Email address
    // $first_name = $_POST['first_name'];
    // $last_name = $_POST['last_name'];
    $subject = "You have earned a free ??? ";
    $subject2 = "Copy of reward notification";
    $message = "You have " . $row['visits'] . " visits left";
    $message2 = "id " . $row['id'] . " has " . $row['visits'] . " visits";

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); 
    // sends a copy of the message to the sender
    echo "Notification has been sent to " . $row['email'];
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    // You cannot use header and echo together. It's one or the other.
		} 
 	}

 }





?>

init.php

<?php
session_start();
//error_reporting(0);

require 'database/connect.php';
require 'functions/general.php';
require 'functions/users.php';

if (logged_in() === true) {
	$session_user_id = $_SESSION['id'];
	$user_data = user_data($session_user_id, 'id', 'username', 'password', 'fname', 'lname', 'email', 'qr_img');
	if (user_active($user_data['username']) === false) {
		session_destroy();
		header('Location: index.php');
		exit();
	}
}

$errors = array();
?>
Edited by ahaberman25
Link to comment
Share on other sites

ive updated it to this, i think its closer but not completely there. 

$admin_id = $user_data['id'];
$data = "SELECT * FROM customer, deals WHERE `admin_id` = $admin_id";

$info = mysql_fetch_array( $data );

while($info = mysql_fetch_array( $data )) 
 { 

if ($info['customer.visits'] == $info['deals.visits']) {
	$res = mysql_query("SELECT * FROM $tbl_name WHERE id='$id'");
	while($row = mysql_fetch_array($res))
  	{
 	echo "<p>You have earned " . $row['visits'] . " punch.</p>";


 	// Send notification email.
 	$to = $row['email']; // this is your Email address
    $from = "andrew@websbydrew.com"; // this is the sender's Email address
    // $first_name = $_POST['first_name'];
    // $last_name = $_POST['last_name'];
    $subject = "You have earned a free ??? ";
    $subject2 = "Copy of reward notification";
    $message = "You have " . $row['visits'] . " visits left";
    $message2 = "id " . $row['id'] . " has " . $row['visits'] . " visits";

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); 
    // sends a copy of the message to the sender
    echo "Notification has been sent to " . $row['email'];
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    // You cannot use header and echo together. It's one or the other.
		} 
 	}

 }
}
Link to comment
Share on other sites

Line 37 - bad query - selecting all fields from two tables? with no connection between them and no qualifier on the where fields.  Is that what you want to do?

Line 39 - you do a fetch but you have not done a query'

Line 41  and all included lines will not be executed either.

 

Don't see what this code has to do with your original proposition?

Link to comment
Share on other sites

$q = "select a.deals, b.deals, from customers a, deals b where a.admin_id = b.admin_id and a.deals = b.deals

will get you the records where admin_id is the same and each of the columns named 'deals' is the same.  If you need other fields you must add them to the query

 

You must also run a query using this statement.

 

Then - if the query results var is not false you can loop thru the results var and use the fields of each row to send an email.

 

AND - you really should write this using mysqli or pdo since what you are using has been deprecated and will no longer work soon.

 

OMG!  I got lost trying to understand your needs and completely missed the idea of this needing a  join.    Please disregard my post as Psycho has it right!

Edited by ginerjm
Link to comment
Share on other sites

If both tables will not always have matching records, which table has all the records? That is the one you will want to run the query from initially (I am assuming deals). Also, need to know which table the email address is in (again assuming deals)

 

This will return a result set of the email addresses and remaining visits where the admin value and the visits count is the same

 

SELECT deals.email, deals.visits
FROM deals
LEFT JOIN customers
  ON deals.admin_id = customers.admin_id
WHERE deals.visits = customers.visits
Link to comment
Share on other sites

okay I think this is returning the proper data now. how do i do a if ($data === true){

}

 

do i set the query true false like other queries?

 

mysql_result(mysql_query("SELECT deals.email, deals.visits
FROM deals
LEFT JOIN customers
  ON deals.admin_id = customers.admin_id
WHERE deals.visits = customers.visits"), 0) == 1) ? true : false;
Link to comment
Share on other sites

 

okay I think this is returning the proper data now. how do i do a if ($data === true){

 

 

Huh? The query ONLY returns records where the values are equal. There is no need to do any comparison int he code. In other words, you need to send an email to every record in the result set of that query.

Edited by Psycho
Link to comment
Share on other sites

  • Solution

Figured it out thanks guys

 

$data = "SELECT *
FROM deals
LEFT JOIN customers
  ON deals.admin_id = customers.admin_id
WHERE deals.visits = customers.visits AND customers.id = $id";
 
$check = mysql_query($data);
if (mysql_num_rows($check) > 0) 
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.