I can't get this if/else statement to work when verifying form data before inserting into database. The If section works with no problems when I have tested it by removing the rest
My code;
if (array_key_exists("submit", $_POST)){
if(!$_POST['tiptitle']) {
$error .= "An Title is required<br>";
}
if(!$_POST['tiptext']) {
$error .= "Text is required<br>";
}
if('!$error') {
$dangererror = "<div class='alert alert-danger'>";
$dangererror .= $error;
$dangererror .= "</div>";
} else {
/*
Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password)
*/
$mysqli = new mysqli("localhost", "paul", "pass", "yourcomp");
// Check connection
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
// Prepare an insert statement
............
// Close statement
$stmt->close();
// Close connection
$mysqli->close();
}
}
Form Submit
<form action="addtip.php" method="post" id="AddTipForm">
Form Button
<input type="submit" name="submit" class="btn btn-primary btn-block mb-4" value="Add Tip!">
// you can use the isset() function to see if post values are set
if (isset($_POST['submit'])){
// empty() functions checks if the value is set or not null
if(empty($_POST['tiptitle'])) {
$error .= "An Title is required<br>";
} else{
// etra validation.... mysql espaces...etc
}
// empty() functions checks if the value is set or not null
if(empty($_POST['tiptext'])) {
$error .= "Text is required<br>";
} else{
// etra validation.... mysql espaces...etc
}
// **** an empty string, "", is considered false in PHP
// you had it in a 'string' format, which is always true.
if($error) {
$dangererror = "<div class='alert alert-danger'>";
$dangererror .= $error;
$dangererror .= "</div>";
} else {
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$mysqli = new mysqli("localhost", "paul", "pass", "yourcomp");
// Check connection
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
// Prepare an insert statement
............
// Close statement
$stmt->close();
// Close connection
$mysqli->close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.