ahaberman25 Posted April 9, 2014 Share Posted April 9, 2014 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 } Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/ Share on other sites More sharing options...
ginerjm Posted April 9, 2014 Share Posted April 9, 2014 1 - your queries have errors in them 2 - you have to do more than just compose a query statement in order to read the data. You need to do some reading to get up to speed. Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475525 Share on other sites More sharing options...
ahaberman25 Posted April 9, 2014 Author Share Posted April 9, 2014 I have done an entire site out of php with several queries the logic on this is just driving me nuts. Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475527 Share on other sites More sharing options...
ginerjm Posted April 9, 2014 Share Posted April 9, 2014 If your post is ALL of the code, then my original two statements still stand. If you have more Relevant code to include, please do so. Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475528 Share on other sites More sharing options...
ahaberman25 Posted April 9, 2014 Author Share Posted April 9, 2014 (edited) 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 April 9, 2014 by ahaberman25 Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475529 Share on other sites More sharing options...
ginerjm Posted April 9, 2014 Share Posted April 9, 2014 (edited) In checkin.php you have: if ($visits = $check_visits) { which will not do what you intend. Need == Also - I don't see where $visits is defined. Edited April 9, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475530 Share on other sites More sharing options...
ahaberman25 Posted April 9, 2014 Author Share Posted April 9, 2014 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. } } } } Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475531 Share on other sites More sharing options...
ginerjm Posted April 9, 2014 Share Posted April 9, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475532 Share on other sites More sharing options...
ahaberman25 Posted April 9, 2014 Author Share Posted April 9, 2014 All I am trying to do is check 2 tables to see if column deals in table customers matches column deals in table deals where admin_id equals the same in both, than send an email if it is equal to each other. I am having issues trying to get this to work. Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475534 Share on other sites More sharing options...
ginerjm Posted April 9, 2014 Share Posted April 9, 2014 (edited) $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 April 9, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475545 Share on other sites More sharing options...
Psycho Posted April 9, 2014 Share Posted April 9, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475546 Share on other sites More sharing options...
ahaberman25 Posted April 9, 2014 Author Share Posted April 9, 2014 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; Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475548 Share on other sites More sharing options...
Psycho Posted April 9, 2014 Share Posted April 9, 2014 (edited) 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 April 9, 2014 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475550 Share on other sites More sharing options...
ahaberman25 Posted April 9, 2014 Author Share Posted April 9, 2014 Well the check is only when someone is "checked in" so It should only send an email to the one person checking in defined by $id Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475554 Share on other sites More sharing options...
ahaberman25 Posted April 9, 2014 Author Share Posted April 9, 2014 I just need to use the query in a if statement to send an email. How do you do that? Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475558 Share on other sites More sharing options...
Solution ahaberman25 Posted April 9, 2014 Author Solution Share Posted April 9, 2014 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) Quote Link to comment https://forums.phpfreaks.com/topic/287641-check-values-from-2-tables/#findComment-1475566 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.