gpt4 book ai didi

PHP if/else 语句插入 MYSQL 表不起作用

转载 作者:行者123 更新时间:2023-11-29 17:00:44 27 4
gpt4 key购买 nike

在插入数据库之前验证表单数据时,我无法使用此 if/else 语句。当我通过删除其余部分进行测试时,If 部分工作没有问题

我的代码;

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 action="addtip.php" method="post" id="AddTipForm">

表单按钮

<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();
}
}

关于PHP if/else 语句插入 MYSQL 表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52280601/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com