0

My question is, how do I get the form to display an error when the user doesn't input a field into the form. I can get it to work when they don't fill out the form at all and just hit enter, but cannot get it to work if they only fill out a few fields. They could fill out the title field and the run time field and will be able to submit it. How can I get it to make sure that every field is filled out?

<?php
$databaseName = 'movie_database';
$databaseUser = 'root';
$databasePassword = 'root';
$databaseHost = '127.0.0.1';

$conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databaseName);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully";

if(isset($_POST['submit'])) {
$value = mysqli_real_escape_string($conn,$_POST['title']);
$value2 = mysqli_real_escape_string($conn,$_POST['rating']);
$value3 = mysqli_real_escape_string($conn,(int)$_POST['Runtime']);
$value4 = mysqli_real_escape_string($conn,(float)$_POST['movie_rating']);
$value5 = mysqli_real_escape_string($conn,$_POST['release_date']);
// Checks for empty fields 
if (empty($value) && empty($value2) && empty($value3) && empty($value4) && empty($value5)) {
    echo 'Please correct the fields';
    return false;
}
// Concatenate the $values into the string
$sql = "INSERT INTO movies(title,rating,Runtime,movie_rating,release_date) 
	VALUES('$value','$value2','$value3','$value4','$value5')";

if ($conn->query($sql)) {
    $msg = "New record created successfully";
} else {
    $msg = "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}
?>
<form method="post"/>
<p>Enter Movie Title: <input type="text" name="title"/></p> 
<p>Enter Movie Rating(G,PG,PG-13,R): <input type="text" name="rating"/></p> 
<p>Enter Movie Runtime in minutes: <input type="text" name="Runtime"/></p> 
<p>Enter IMDB Movie Rating(0-10.0): <input type="text" name="movie_rating"/></p> 
<p>Enter Movie Release date(YYYY-MM-DD): <input type="text" name="release_date"/></p>  
<button type="submit" name="submit">Submit</button
</form> 

<?php 
if (isset($msg)) {
	echo $msg;
}
?>

4
  • try using required attribute in the frontend, it will throw error automatically Commented Nov 10, 2017 at 5:37
  • e.g: empty($_POST['title']) // returns true if no content Commented Nov 10, 2017 at 5:37
  • Use emtpy on all fields that are expected... consult manual though if 0 or other specific values are expected.. Commented Nov 10, 2017 at 5:38
  • you're using the && operator to test all fields at once. this will return true if just one field has a value. Use the || operator instead. Commented Nov 10, 2017 at 5:45

2 Answers 2

1

From Front side you can add required attribute to each input. So add it in every input field like : <input type="text" name="title" required/>

From back-end side (using PHP) If you want all value must be fill up then change && with || in your condition

// Checks for empty fields 
if (empty($value) || empty($value2) || empty($value3) || empty($value4) || empty($value5)) {
    echo 'Please correct the fields';
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

0
if (empty($value) && empty($value2) && empty($value3) && empty($value4) && empty($value5)) {
    echo 'Please correct the fields';
    return false;
}

This should be 'or'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.