Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:42:41+00:00 2026-06-15T18:42:41+00:00

I have a piece of code below where it performs a couple of queries

  • 0

I have a piece of code below where it performs a couple of queries and if there is a row found from one of the first two queries, then it will display validation errors, if there are no rows found form the 2 queries then it performs an insert and another SELECT query:

                                       // don't use $mysqli->prepare here
       $query = "SELECT StudentUsername FROM Student WHERE StudentUsername = ?";
       // prepare query
       $stmt=$mysqli->prepare($query);
       // You only need to call bind_param once
       $stmt->bind_param("s",$getusername);
       // execute query
       $stmt->execute(); 
       // get result and assign variables (prefix with db)
       $stmt->bind_result($dbStudentUsername);
       //get number of rows
       $stmt->store_result();
       $numrows = $stmt->num_rows();

       $aliasquery = "SELECT StudentAlias FROM Student WHERE StudentAlias = ?";
       // prepare query
       $aliasstmt=$mysqli->prepare($aliasquery);
       // You only need to call bind_param once
       $aliasstmt->bind_param("s",$getalias);
       // execute query
       $aliasstmt->execute(); 
       // get result and assign variables (prefix with db)
       $aliasstmt->bind_result($dbStudentAlias);
       //get number of rows
       $aliasstmt->store_result();
       $aliasnumrows = $aliasstmt->num_rows();

    if ($aliasnumrows == 0){
       if ($numrows == 0){

              $formatdate = date("Y-m-d",strtotime($getdob));
              $studentpassword = md5(md5("93w".$studentpassword."ed0"));  


              $insertsql = "
            INSERT INTO Student
                (StudentForename, StudentSurname, StudentAlias, StudentUsername, StudentPassword, StudentDOB, Year, CourseId)
              VALUES
                (?, ?, ?, ?, ?, ?, ?, ?)
            ";
            if (!$insert = $mysqli->prepare($insertsql)) {
              // Handle errors with prepare operation here
            }                                           

            $insert->bind_param("ssssssis", $getfirstname, $getsurname, $getalias, $getusername, $studentpassword, $formatdate, $getyear, $getcourse);

            $insert->execute();

            if ($insert->errno) {
              // Handle query error here
            }

            $insert->close();

             // don't use $mysqli->prepare here
       $query = "SELECT StudentUsername FROM Student WHERE StudentUsername = ?";
       // prepare query
       $stmt=$mysqli->prepare($query);
       // You only need to call bind_param once
       $stmt->bind_param("s",$getusername);
       // execute query
       $stmt->execute(); 
       // get result and assign variables (prefix with db)
       $stmt->bind_result($dbStudentUsername);
       //get number of rows
       $stmt->store_result();
       $numrowsstmt = $stmt->num_rows();

        }else{
        $errors['username'] = "<span style='color: red'>There is already a Student with that Username</span>";
        }

        }else{
        $errors['alias'] = "<span style='color: red'>There is already a Student with that Alias</span>";
        }
    }

The problem I am having though is that I want to display both the validation errors at the same time if both validation errors are met. At the moment it is only displaying the validation errors one at a time. How can I get the both validation errors displayed together if both validation errors are met?

Below is the code where it stores the validation errors in the form:

$error_alias= (!empty($errors['alias']))?$errors['alias']:"";
$error_username = (!empty($errors['username']))?$errors['username']:"";

$form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
  <table>
  <tr>
  <td>Alias:</td>
  <td><input type='text' name='alias' value='$getalias' /><br/>".$error_alias."</td>
  </tr>
  <tr>
  <td>Username:</td>
  <td><input type='text' name='studentusername' value='$getusername' /><br/>".$error_username."</td>
  </tr>
  <tr>
  <td></td>
  <td><input type='submit' value='Register' name='registerbtn' /></td>
  </tr>
  </table>
  </form>";

  echo $form;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:42:42+00:00Added an answer on June 15, 2026 at 6:42 pm

    You can change the if statement slightly:

    if ($aliasnumrows == 0 && $numrows == 0){
        //Do stuff
    }else{
        if($aliasnumrows){
            $errors['alias'] = "<span style='color: red'>There is already a Student with that Alias</span>";
        }
        if($numrows){
            $errors['username'] = "<span style='color: red'>There is already a Student with that Username</span>";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of matlab code below which reads data from a table.
I have a piece of code below where it contains 26 buttons from A-Z.
I have a piece of code (below) that can get the text of an
I have two OptionMenu widgets in the simple pieces of code shown below: variable
I have a piece of code below where it echos a salted password: $pass
I have a piece of code below where it contains buttons and each button
I have this piece of code from GWT in Action: public void processOperator(final AbstractOperator
I have this piece of code here below: $fq.= + (fritidsboende_uth_from:[$fritids_uth_from TO *] AND
I have a piece of code shown below #include <stdio.h> #include <stdlib.h> void Advance_String(char
I have a piece of ada code shown below which is a simple switch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.