phpmyadmin 在输入为空时不使用 DEFAULT 值 [英] phpmyadmin not using DEFAULT value when input is left empty
问题描述
我遇到了这个问题,如果我将标题"的输入留空,那么当发送到数据库时它不会设置默认值:无标题".我在网上查看并确保我在 phpmyadmin 中的设置正确,但它仍然不会设置默认值.任何建议表示赞赏!
I have this problem where if I leave my input for 'Title' blank, then it won't set the default value: "Untitled" when sent to the database. I've looked online and have made sure that my settings were correct in phpmyadmin but it still won't set the default value. Any piece of advice is appreciated!
这是我的标题"列的 PHPmyadmin 设置:
Here are my PHPmyadmin settings for the "Title" column:
这些是我的文件:
addart.php
<form method="post" action="addtodb.php">
<label for="Title">
<h4>Title</h4>
</label>
<input class="u-full-width"
type="text"
placeholder="Title of art"
id="Title"
name="Title">
</form>
addtodb.php
addtodb.php
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'testdb';
$dbConnection = new mysqli($host, $user, $pass, $db);
if (mysqli_connect_errno()) {
printf("Could not connect to the mySQL database: %s\n", mysqli_connect_error());
exit();
}
if($_POST) {
$artwork = $_POST["Artwork"];
$medium = $_POST["Medium"];
$artist = $_POST["Artist"];
$title = $_POST["Title"];
$results = $dbConnection->query("INSERT INTO art
(Artwork, Title, Artist, Medium) VALUES
('$artwork','$title','$artist','$medium');");
if (!$results) {
echo 'Unable to insert into database.';
exit();
} else {
echo 'Successfully added!';
}
mysqli_close($dbConnection);
header("Location: galleryonly.php"); /* Redirect browser */
exit();
}
?>
推荐答案
$artwork = $_POST["Artwork"];
$medium = $_POST["Medium"];
$artist = $_POST["Artist"];
$title = $_POST["Title"];
if(!empty($title)) {
$sql = "INSERT INTO art (Artwork, Title, Artist, Medium) VALUES ('$artwork', '$title', '$artist', '$medium')";
} else {
$sql = "INSERT INTO art (Artwork, Artist, Medium) VALUES ('$artwork', '$artist', '$medium')";
}
$results = $dbConnection->query($sql);
你可以试试这个代码.
如果您省略该列,则将设置默认值.
If you're omitting the column, the default value will be set.
因为您只有一列具有默认值,所以您可以坚持使用此代码.
Because you have only one column with default value, you can stick with this code.
如果您有多于一列的默认值,则需要根据您的要求进行更改.
If you have more than one column with default value, you will need to make changes according to your requirements.
这篇关于phpmyadmin 在输入为空时不使用 DEFAULT 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!