I am trying to create a user inserted database using php and sql.
Here is the sql code -
CREATE TABLE `presidingOfficers` (
`id` int(11) NOT NULL,
`presidingOfficers_id` varchar(15) NOT NULL,
`password` varchar(60) NOT NULL,
`firstname` varchar(30) NOT NULL,
`lastname` varchar(30) NOT NULL,
`photo` varchar(150) NOT NULL,
`contactNumber` varchar(20) NOT NULL UNIQUE,
`roomNumber` varchar(20) NOT NULL UNIQUE,
`polling1` varchar(50) NOT NULL,
`polling2` varchar(50) NOT NULL,
`polling3` varchar(50) NOT NULL,
`ballotValue` int(10) NOT NULL,
`mockValue` int(10) NOT NULL,
`total` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Here is the php code -
$sql = "INSERT INTO presidingOfficers (presidingOfficers_id, password, firstname, lastname, photo, contactNumber, roomNumber, polling1, polling2, polling3, ballotValue, total) VALUES ('$presidingOfficer', '$password', '$firstname', '$lastname', '$filename', '$contactNumber', '$roomNumber', '$polling1', '$polling2', '$polling3', 0, 0)";
if($conn->query($sql)){
$_SESSION['success'] = 'Presiding Officer added successfully';
}else{
$_SESSION['error'] = 'Please enter unique values';
}
I want to have unique inputs for contactNumber and roomNumber and so I used the UNIQUE constraint. If the inputs are unique its shows a success message like this - Success Image
But for non unique entries I get an error message in a different tab like this - Error message
I want the error message similar to the success message but in red colour. Thankyou
CodePudding user response:
Check the documentation , It shows that its returns false on an error. It will return false if it fails, which you can log or handle that and then continue.
$sql = "INSERT INTO presidingOfficers (presidingOfficers_id, password, firstname, lastname, photo, contactNumber, roomNumber, polling1, polling2, polling3, ballotValue, total) VALUES ('$presidingOfficer', '$password', '$firstname', '$lastname', '$filename', '$contactNumber', '$roomNumber', '$polling1', '$polling2', '$polling3', 0, 0)";
$rv = mysql_query($sql);
if ( $rv === false ){
//handle the error here
}
if($rv){
}
I hope this will work :)
CodePudding user response:
You can use the bootstrap class to show an error message
like if(!empty($fnameerror)){ echo "<div class='alert alert-danger'>" . $fnameerror . "</div>"; }