Jump to content

PHP Help needed


Dealmightyera

Recommended Posts

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>

 

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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>

 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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:

  1. Does the transaction ID exist
  2. 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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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.

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.