Dealmightyera Posted April 25, 2023 Share Posted April 25, 2023 Please I've been trying to get his done for more than five days but can and it's very urgent.... I have 2 DB (A and B). DB A, will have 2 columns 'id' and 'transactioncode' and DB B(user registration info in different columns such as name, last, nickname, regcode) Now I want to validate if $_POST['code'] has already been used and exist in 'regcode' column in the user registration information table of DB B And if not used(not exist) in 'regcoe' column of DB A but exist in the column 'transactioncode' in DB A I've not mastered php yet. i need it in pdo Thanks in anticipation <?php session_start(); require_once 'INC/DBcon.php'; if(ISSET($_POST['Submit1'])){ if($_POST['transactioncode'] != ""){ $trr = $_POST['transactioncode']; $stmt = $conn->prepare("SELECT * FROM Candidates WHERE transactioncode=?"); $stmt->execute([$trr]); $user = $stmt->fetch(); // $trr1 = $_POST['transactioncode']; $stmt1 = $conn->prepare("SELECT * FROM tsc WHERE transactioncode=?"); $stmt1->execute([$trr1]); $user1 = $stmt1->fetch(); if($user = $trr && $user1 = $trr1) { echo "<script>alert('Already use!')</script>"; }elseif($user1 = $trr1 and $user != $trr) { echo "<script>alert('Validation Succcessful')</script>"; $_SESSION['tsc'] = $fetch['transactioncode']; header("location: Sign-up.php"); } }else{ echo " <script>alert('Please complete the required field!')</script> <script>window.location = 'VLL.php'</script> "; } } ?> <!DOCTYPE html> <html class="no-js" lang="en"> <head> </head> <body> <form action="" name="regForm" onsubmit="return validateForm()" method="POST"> <h4 class="text-success"> <b>N/B:</b> Enter Session/Transaction ID/Number Correctly and click on PROCEED/FETCH, candidates will now commence registration.</h4> <br> <br> <div class="fCont"> <div class="fContL"> <label for="tsc"> TRANSACTION NUMBER/<br>SESSION ID/<br>TRACKING CODE: </label> </div> <div class="fContR"> <input type="text" id="tsc" name="transactioncode" placeholder="Your TRANSACTION NUMBER/SESSION ID/TRACKING CODE.."> </div> </div> <br> <br> <div class="fCont"> <input type="submit" id="submit" value="PROCEED/FETCH" class="submit" name="Submit1"> </div> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/ Share on other sites More sharing options...
ginerjm Posted April 25, 2023 Share Posted April 25, 2023 First of all - you have TWO TABLES, not 2 dbs. Next - you run the query but you never check if it ran or not nor do you check if any rows were returned so the fetch may very well fail. Then - you do a second query from your 2nd table but why use a second copy of the user-entry 'transactioncode'? You already have the value in the $trr var so why do you need $trr1? After the queries are run (hopefully) you are doing a rather fruitless test if the 2 results match the user provided code which if you did received results from your queries is unnecessary. But since you didn't do any checks on those results you can't be sure of. PHP - when testing if something is "equal to" something else you must use == not just =. You elseif if flawed. Lastly (cause that is far as I read) you are referencing a variable $fetch. Where is that defined? Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607690 Share on other sites More sharing options...
Dealmightyera Posted April 25, 2023 Author Share Posted April 25, 2023 3 hours ago, ginerjm said: First of all - you have TWO TABLES, not 2 dbs. Next - you run the query but you never check if it ran or not nor do you check if any rows were returned so the fetch may very well fail. Then - you do a second query from your 2nd table but why use a second copy of the user-entry 'transactioncode'? You already have the value in the $trr var so why do you need $trr1? After the queries are run (hopefully) you are doing a rather fruitless test if the 2 results match the user provided code which if you did received results from your queries is unnecessary. But since you didn't do any checks on those results you can't be sure of. PHP - when testing if something is "equal to" something else you must use == not just =. You elseif if flawed. Lastly (cause that is far as I read) you are referencing a variable $fetch. Where is that defined? Please, I beg you help me implement it in the code. I'm still a beginner in PHP. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607692 Share on other sites More sharing options...
Psycho Posted April 25, 2023 Share Posted April 25, 2023 1 hour ago, Dealmightyera said: Please, I beg you help me implement it in the code. I'm still a beginner in PHP. Thanks. This is a PHP "Help" forum, not a PHP "Do it for me" forum. But, I'll give you some code samples to start with. I don't have your database and am simply not going to take the effort to put one together to test the code. So, what I provide might not work out of the box. I'll leave it to you to fix any errors I miss. <?php session_start(); require_once 'INC/DBcon.php'; $error = ""; //Variable to hold error messages // Check if the user submitted a transaction code if(ISSET($_POST['transactioncode'])) { // Trim the transaction code and assign to variable $tcode = trim($_POST['transactioncode']); //Create a query to identify if the transaction code exists //AND if it has been used for a candidate by JOINing the tables $query = "SELECT t.transactioncode AS tCode, c.transactioncode AS cCode FROM tsc AS t LEFT JOIN Candidates AS c USING (transactioncode) WHERE transactioncode = ?" $stmt = $conn->prepare($query)->execute([$tcode]); $result = $stmt->fetch(); //Check if results were empty if(!$result) { //The transaction code does not exist $error = "The transaction code does not exist"; } //Check if the cCode (transaction code in candidate table) is not empty elseif (!empty($result['cCode'])) { //The transaction code has been used $error = "That code has already been used."; } else { //The transaction code exists and has not been used $_SESSION['tsc'] = $tcode; echo "<script>alert('Validation Succcessful')</script>"; header("location: Sign-up.php"); exit(); } } ?> <!DOCTYPE html> <html class="no-js" lang="en"> <head> </head> <body> <?php echo $error; ?> <form action="" name="regForm" onsubmit="return validateForm()" method="POST"> <h4 class="text-success"> <b>N/B:</b> Enter Session/Transaction ID/Number Correctly and click on PROCEED/FETCH, candidates will now commence registration.</h4> <br> <br> <div class="fCont"> <div class="fContL"> <label for="tsc"> TRANSACTION NUMBER/<br>SESSION ID/<br>TRACKING CODE: </label> </div> <div class="fContR"> <input type="text" id="tsc" name="transactioncode" placeholder="Your TRANSACTION NUMBER/SESSION ID/TRACKING CODE.."> </div> </div> <br> <br> <div class="fCont"> <input type="submit" id="submit" value="PROCEED/FETCH" class="submit" name="Submit1"> </div> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607693 Share on other sites More sharing options...
ginerjm Posted April 25, 2023 Share Posted April 25, 2023 I told you several things that were wrong. Look them up in the manual to learn how to code them correctly. Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607694 Share on other sites More sharing options...
Dealmightyera Posted April 26, 2023 Author Share Posted April 26, 2023 11 hours ago, Psycho said: This is a PHP "Help" forum, not a PHP "Do it for me" forum. But, I'll give you some code samples to start with. I don't have your database and am simply not going to take the effort to put one together to test the code. So, what I provide might not work out of the box. I'll leave it to you to fix any errors I miss. <?php session_start(); require_once 'INC/DBcon.php'; $error = ""; //Variable to hold error messages // Check if the user submitted a transaction code if(ISSET($_POST['transactioncode'])) { // Trim the transaction code and assign to variable $tcode = trim($_POST['transactioncode']); //Create a query to identify if the transaction code exists //AND if it has been used for a candidate by JOINing the tables $query = "SELECT t.transactioncode AS tCode, c.transactioncode AS cCode FROM tsc AS t LEFT JOIN Candidates AS c USING (transactioncode) WHERE transactioncode = ?" $stmt = $conn->prepare($query)->execute([$tcode]); $result = $stmt->fetch(); //Check if results were empty if(!$result) { //The transaction code does not exist $error = "The transaction code does not exist"; } //Check if the cCode (transaction code in candidate table) is not empty elseif (!empty($result['cCode'])) { //The transaction code has been used $error = "That code has already been used."; } else { //The transaction code exists and has not been used $_SESSION['tsc'] = $tcode; echo "<script>alert('Validation Succcessful')</script>"; header("location: Sign-up.php"); exit(); } } ?> <!DOCTYPE html> <html class="no-js" lang="en"> <head> </head> <body> <?php echo $error; ?> <form action="" name="regForm" onsubmit="return validateForm()" method="POST"> <h4 class="text-success"> <b>N/B:</b> Enter Session/Transaction ID/Number Correctly and click on PROCEED/FETCH, candidates will now commence registration.</h4> <br> <br> <div class="fCont"> <div class="fContL"> <label for="tsc"> TRANSACTION NUMBER/<br>SESSION ID/<br>TRACKING CODE: </label> </div> <div class="fContR"> <input type="text" id="tsc" name="transactioncode" placeholder="Your TRANSACTION NUMBER/SESSION ID/TRACKING CODE.."> </div> </div> <br> <br> <div class="fCont"> <input type="submit" id="submit" value="PROCEED/FETCH" class="submit" name="Submit1"> </div> </form> </body> </html> Thanks very much for the time. Everything seems to work very except that when the submit button is triggered it brings a Fatal error message concerning the fetch in line 23. Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607709 Share on other sites More sharing options...
Dealmightyera Posted April 26, 2023 Author Share Posted April 26, 2023 Everything seems to work very except that when the submit button is triggered it brings a Fatal error message concerning the fetch in line 23. I'm just still a novice in PHP, Can't still figure it out now. Thanks, I've really learnt a lot. Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607716 Share on other sites More sharing options...
Dealmightyera Posted April 26, 2023 Author Share Posted April 26, 2023 <?php $db_username = 'root'; $db_password = ''; $conn = new PDO('mysql:host=localhost;dbname=db_login', $db_username, $db_password); if(!$conn){ die("Fatal Error: Connection Failed!"); } ?> The above is the content of my DBCon.php Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607717 Share on other sites More sharing options...
ginerjm Posted April 26, 2023 Share Posted April 26, 2023 Show us the code around that error line as well as the FULL EXACT error messge Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607718 Share on other sites More sharing options...
ginerjm Posted April 26, 2023 Share Posted April 26, 2023 This query seems strange to me: SELECT t.transactioncode AS tCode, c.transactioncode AS cCode FROM tsc AS t LEFT JOIN Candidates AS c USING (transactioncode) WHERE transactioncode = ?" You are selecting 2 values that are the same and nothing else. I picture a set of records for your chosen code that are nothing but that and nothing of value. Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607720 Share on other sites More sharing options...
Dealmightyera Posted April 26, 2023 Author Share Posted April 26, 2023 Here's the error Fatal error: Uncaught Error: Call to a member function fetch() on bool in C:\XAMPP\htdocs\Test.php:23 Stack trace: #0 {main} thrown in C:\XAMPP\htdocs\Test.php on line 23 Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607722 Share on other sites More sharing options...
ginerjm Posted April 26, 2023 Share Posted April 26, 2023 (edited) Can you not read english? I asked for the code pertaining to this error and you sent me nothing. Sounds like the query did not run or else your code is broken. If this is the problem area: $query = "SELECT t.transactioncode AS tCode, c.transactioncode AS cCode FROM tsc AS t LEFT JOIN Candidates AS c USING (transactioncode) WHERE transactioncode = ?" $stmt = $conn->prepare($query)->execute([$tcode]); $result = $stmt->fetch(); I suggest (especially for a noob): $query = "SELECT t.transactioncode AS tCode, c.transactioncode AS cCode FROM tsc AS t LEFT JOIN Candidates AS c USING (transactioncode) WHERE transactioncode = :trancd" $parms = array(':trancd'=>$tcode); if(!$stmt = $conn->prepare($query)) { echo "Error doing prepare - Aborting"; exit(); } else { if (!$stmt->execute($parms)) { echo "Error running query - query was<br>$query<br>"; exit(); } } // process results now if (!$result = $stmt->fetch()) ... ... Makes for much clearer reading. Edited April 26, 2023 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607725 Share on other sites More sharing options...
Dealmightyera Posted April 26, 2023 Author Share Posted April 26, 2023 Sorry, didn't read thru it well. <?php session_start(); require_once 'INC/DBcon.php'; $error = ""; //Variable to hold error messages // Check if the user submitted a transaction code if(ISSET($_POST['transactioncode'])) { // Trim the transaction code and assign to variable $tcode = trim($_POST['transactioncode']); //Create a query to identify if the transaction code exists //AND if it has been used for a candidate by JOINing the tables $query = "SELECT t.transactioncode AS tCode, c.transactioncode AS cCode FROM tsc AS t LEFT JOIN Candidates AS c USING (transactioncode) WHERE transactioncode = ?" $stmt = $conn->prepare($query)->execute([$tcode]); $result = $stmt->fetch(); //Check if results were empty if(!$result) { //The transaction code does not exist $error = "The transaction code does not exist"; } //Check if the cCode (transaction code in candidate table) is not empty elseif (!empty($result['cCode'])) { //The transaction code has been used $error = "That code has already been used."; } else { //The transaction code exists and has not been used $_SESSION['tsc'] = $tcode; echo "<script>alert('Validation Succcessful')</script>"; header("location: Sign-up.php"); exit(); } } ?> <!DOCTYPE html> <html class="no-js" lang="en"> <head> </head> <body> <?php echo $error; ?> <form action="" name="regForm" onsubmit="return validateForm()" method="POST"> <h4 class="text-success"> <b>N/B:</b> Enter Session/Transaction ID/Number Correctly and click on PROCEED/FETCH, candidates will now commence registration.</h4> <br> <br> <div class="fCont"> <div class="fContL"> <label for="tsc"> TRANSACTION NUMBER/<br>SESSION ID/<br>TRACKING CODE: </label> </div> <div class="fContR"> <input type="text" id="tsc" name="transactioncode" placeholder="Your TRANSACTION NUMBER/SESSION ID/TRACKING CODE.."> </div> </div> <br> <br> <div class="fCont"> <input type="submit" id="submit" value="PROCEED/FETCH" class="submit" name="Submit1"> </div> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607726 Share on other sites More sharing options...
ginerjm Posted April 26, 2023 Share Posted April 26, 2023 And since there is only one fetch that must be the line. Try doing it my way. If you were able to read thru that post. Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607727 Share on other sites More sharing options...
Dealmightyera Posted April 26, 2023 Author Share Posted April 26, 2023 Thanks Sir, I really really appreciate. You're one in a million. I've learnt a lot. Please can you just help me implement in the whole so I'll I've a full grasph. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607728 Share on other sites More sharing options...
ginerjm Posted April 26, 2023 Share Posted April 26, 2023 (edited) Huh? Take the code I gave you and put in place of the code you are attempting to use. And then try it. If it still fails, post it all Edited April 26, 2023 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607729 Share on other sites More sharing options...
gizmola Posted April 26, 2023 Share Posted April 26, 2023 You have this code: $query = "SELECT t.transactioncode AS tCode, c.transactioncode AS cCode FROM tsc AS t LEFT JOIN Candidates AS c USING (transactioncode) WHERE transactioncode = ?" $stmt = $conn->prepare($query)->execute([$tcode]); $result = $stmt->fetch(); Look at the 3rd line here. Your code assumes that $stmt is a valid result set that you can use to fetch results. The problem is that if your query fails, $stmt will not be a result set, and you will not be able to fetch with it. This is what is happening. You need to figure out what is wrong with the executed query, but also as @ginerjm showed you, you also should not have code that assumes a query worked to generate a valid result, as your current code does. You should check that $stmt is true/valid first. Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607730 Share on other sites More sharing options...
ginerjm Posted April 26, 2023 Share Posted April 26, 2023 Which my last post is doing for you..... Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607731 Share on other sites More sharing options...
Psycho Posted April 26, 2023 Share Posted April 26, 2023 5 hours ago, ginerjm said: This query seems strange to me: SELECT t.transactioncode AS tCode, c.transactioncode AS cCode FROM tsc AS t LEFT JOIN Candidates AS c USING (transactioncode) WHERE transactioncode = ?" You are selecting 2 values that are the same and nothing else. I picture a set of records for your chosen code that are nothing but that and nothing of value. Yeah, that was my revised query I sent to him and I can see why it is confusing. While it as not what I would ideally write it should work for him. Based on what I deduced from his code there is a transaction table and a candidates table. A transaction ID will be included in any transaction record created. Then, when that transaction ID is "USED" it will be included (or updated) in a record within the candidates table. The functionality he is trying to create is to identify if: Does the transaction ID exist Has the transaction ID been used That query will take the supplied ID from the form input and pull the transaction ID from both the transition table and the candidates table using a LEFT join. So, if the transaction ID does not exist (within the transaction table) then no records will be returned. If a record is returned, then the logic just needs to check the transaction ID from the Candidates table. If NULL then the transaction code has not been used. I probably would have pulled something like the candidate ID from the candidates table to check (or use a calculated group by to return a 0 or 1 value. But, I had no visibility of any other field names from that table and was too lazy to ask or provide an explanation when using some mock field name for him to change. Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607732 Share on other sites More sharing options...
ginerjm Posted April 26, 2023 Share Posted April 26, 2023 So Dealmightera: Is this no longer "urgent" as you said in your opening post? Cause if it is I have to wonder why you keep disappearing. You have my attention - but you are doing your best to lose it. Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607733 Share on other sites More sharing options...
Dealmightyera Posted April 26, 2023 Author Share Posted April 26, 2023 I'm still giving a head's up on the code Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607734 Share on other sites More sharing options...
ginerjm Posted April 26, 2023 Share Posted April 26, 2023 Twenty lines of new code? 1 Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607735 Share on other sites More sharing options...
Dealmightyera Posted April 26, 2023 Author Share Posted April 26, 2023 Yes sir. Twenty line Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607737 Share on other sites More sharing options...
Dealmightyera Posted April 26, 2023 Author Share Posted April 26, 2023 Hers the new code <?php error_reporting(E_ALL); ini_set('display_errors', '1'); session_start(); require_once 'INC/DBcon.php'; $error = ""; //Variable to hold error messages // Check if the user submitted a transaction code if(ISSET($_POST['transactioncode'])) { // Trim the transaction code and assign to variable $tcode = trim($_POST['transactioncode']); //Create a query to identify if the transaction code exists //AND if it has been used for a candidate by JOINing the tables $query = "SELECT t.transactioncode AS tCode, c.transactioncode AS cCode FROM tsc AS t LEFT JOIN Candidates AS c USING (transactioncode) WHERE transactioncode = :trancd"; $stmt = $conn->prepare($query)->execute([$tcode]); $parms = array(':trancd'=>$tcode); if(!$stmt = $conn->prepare($query)) { echo "Error doing prepare - Aborting"; exit(); } else { if (!$stmt->execute($parms)) { echo "Error running query - query was<br>$query<br>"; exit(); } } // process results now if (!$result = $stmt->fetch()) { } } ?> <!DOCTYPE html> <html class="no-js" lang="en"> <head> </head> <body> <?php echo $error; ?> <form action="" name="regForm" onsubmit="return validateForm()" method="POST"> <h4 class="text-success"> <b>N/B:</b> Enter Session/Transaction ID/Number Correctly and click on PROCEED/FETCH, candidates will now commence registration.</h4> <br> <br> <div class="fCont"> <div class="fContL"> <label for="tsc"> TRANSACTION NUMBER/<br>SESSION ID/<br>TRACKING CODE: </label> </div> <div class="fContR"> <input type="text" id="tsc" name="transactioncode" placeholder="Your TRANSACTION NUMBER/SESSION ID/TRACKING CODE.."> </div> </div> <br> <br> <div class="fCont"> <input type="submit" id="submit" value="PROCEED/FETCH" class="submit" name="Submit1"> </div> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607738 Share on other sites More sharing options...
Dealmightyera Posted April 26, 2023 Author Share Posted April 26, 2023 I'm just a noob in Php/mysql... and have never executed such project was just trying see if someone could help me out with the full code structure because of time... I just have less than 3hours to go. maybe I just have to end it here on this project even if i didn't succeed. Thanks everyone for the time you made, I appreciate. Just have to say good bye. Quote Link to comment https://forums.phpfreaks.com/topic/316209-php-help-needed/#findComment-1607739 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.