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

  • Home
  • SEARCH
  • 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 8958347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:11:31+00:00 2026-06-15T15:11:31+00:00

I just need help on finishing this validation I have where the php checks

  • 0

I just need help on finishing this validation I have where the php checks to see if there are any errors when user submits form:

$errors = array();


      if (!$getcourseid){
          $errors[] = "You must enter in Course's ID";
  }else if (!$getcoursename){
      $errors[] = "You must enter in Course's Name";
  }else if (!$getduration){
          $errors[] = "You must select Course's Duration";
      }     

      if(!$errors) {

     $insertsql = "
    INSERT INTO Course
        (CourseNo, CourseName, Duration)
      VALUES
        (?, ?, ?)
    ";
    if (!$insert = $mysqli->prepare($insertsql)) {
      // Handle errors with prepare operation here
    }                                           

    $insert->bind_param("sss", $getcourseid, $getcoursename, $getduration);

    $insert->execute();

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

    $insert->close();

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

          }

$form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
  <table>
  <tr>
  <td></td>
  <td id='errormsg'>$errormsg</td>
  </tr>
  <tr>
  <td>Course ID:</td>
  <td><input type='text' name='courseid' value='$getcourseid' /></td>
  </tr>
  <tr>
  <td>Course Name:</td>
  <td><input type='text' id='nameofcourse' name='coursename' value='$getcoursename' /></td>
  </tr>
  <tr>
  <td>Duration (Years):</td>
  <td>{$durationHTML}</td>
  </tr>
  <tr>
  <td></td>
  <td><input type='submit' value='Create Course' name='createbtn' /></td>
  </tr>
  </table>
  </form>";

  echo $form;

My question is that I want to display a success message if there are no errors and the database stuff have been successfully completed or display an error message if the form has been completed with no errors but the database stuff could not be completed. Also I want to display all messages to the $errormsg variable? Can I ask how that can be done?

Below is something I want to achieve but not sure if its correct for the code above:

               if ($numrows == 1){

               $errormsg = "<span style='color: green'>Course " . $getcourseid .  " - "  . $getcoursename . " has been Created</span>";
               $getcourseid = "";
               $getcoursename = "";
               $getduration = "";

           }else{

               $errormsg = "An error has occured, Course has not been Created";

           }
  • 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-15T15:11:32+00:00Added an answer on June 15, 2026 at 3:11 pm

    The logic goes like this, suppose you are having an array of errors simply you can do this

    if(empty($errormsg)) {
       //Go Ahead
    } elseif(!empty($errormsg)) {
      //if(isset($errormsg['index'])) {
         echo 'Error'; 
      }
    }
    

    Detailed Explanation

    Supposing you have 3 errors like

    $errors[0] = 'Username is invalid';
    $errors[1] = 'Password is invalid';
    $errors[2] = 'Third error';
    
    if(empty($errors)) {
       //Go Ahead
    } else {
        if(isset($errors[0])) {
           echo $errors[0]; 
        } elseif (isset($errors[1])) {
           echo $errors[1];
        } elseif (isset($errors[1])) {
           echo $errors[1]; 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class and I just need help with the toString() inside the
Just need help as I have been trying sort this out for ages now.
I just need help on this PHP error which I do not quite understand:
I am trying to convert this to python. I just really need help with
I have the fiddle all ready to go I just need help with the
Need some help finishing up this program, everything works and runs like I want
I need help creating the best possible regular expression for this problem. I have
I'm working on a custom filter in WordPress and just need help sorting this
I don't want code, just need help finding where to start this project. I
I just need help. Basically I am creating a windows appllication that sends bulk

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.