Click here to Skip to main content
15,895,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
While submitting the form if empty name means error message to show. I using one html page and one php page . What i am getting is eventhough the name is not in also it will storing in other data given like age into database. I need the answer as [* Username missing ] on the side of textfield and not going to next form. KIndly help pls

What I have tried:

<?php

$link = mysqli_connect("localhost", "root", "", "training");

if(isset($_POST['submit']))
{

$fname = $_POST['fname'];

if (empty($_POST["name"])) {
$fnameErr = "Missing";
}
else {
$fname = ($_POST["fname"]);
}
$sql = "INSERT INTO record (Name) VALUES ('$fname')";

$op = mysqli_query($link,$sql);
if($op){
echo "Student info inserted";
}
else{
echo "not inserted";
}

}
mysqli_close($link);
?>
Posted
Updated 19-Feb-18 18:41pm
v2
Comments
ThilinaMD 20-Feb-18 8:16am    
A simple html validation can be done usinng required attribute for input fields. w3 schools
Richard Deeming 20-Feb-18 15:21pm    
How about not executing the INSERT query if the data is missing?
Member 13686337 20-Feb-18 23:16pm    
Thanx Richard for the reply,
Insert is working what ever the field we are giving.But i need if name field is emply need to show err msg and stay on that page as like common form.Expecting a reply ;
Richard Deeming 22-Feb-18 6:26am    
As I said, if the field is missing, don't execute the INSERT.

You're currently executing the INSERT whether or not the field has been completed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900