当输入为空时,phpmyadmin不使用默认值

当输入为空时,phpmyadmin不使用默认值,php,mysql,Php,Mysql,我有一个问题,如果我将“Title”的输入留空,那么当发送到数据库时,它将不会设置默认值:“Untitled”。我在网上查看了一下,确保我在phpmyadmin中的设置是正确的,但它仍然不会设置默认值。任何建议都将不胜感激 以下是“标题”列的PHPmyadmin设置: 这些是我的文件: addart.php <form method="post" action="addtodb.php"> <label for="Title"> <h4>Ti

我有一个问题,如果我将“Title”的输入留空,那么当发送到数据库时,它将不会设置默认值:“Untitled”。我在网上查看了一下,确保我在phpmyadmin中的设置是正确的,但它仍然不会设置默认值。任何建议都将不胜感激

以下是“标题”列的PHPmyadmin设置:

这些是我的文件:

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

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

前面还有一些技巧,因为如果需要默认值,您将无法使用标题列

// assuming use of proper method of sanitizing
// these values so we don't get SQL INJECTED!!
$artwork = 'artwork';
$title = 'title';
$artist = 'artist';
$medium = 'medium';

// make an array with the columns
$cols = explode(',', 'Artwork,Title,Artist,Medium');

// make an array with the values (that you sanitized properly!)
$vars = explode(',', 'artwork,title,artist,medium');

foreach ($cols as $i=>&$col) {
  $var = ${$vars[$i]};
  if ($col == 'Title') {
    if (empty($var)) {
      // don't add this column if empty
      continue;
    }
  }
  // otherwise (if not Title)
  // add it to a column = "value" insert string
  $pcs[] = "`$col` = '$var'";
}

// fortunately, we can insert with update syntax, too!
$query = 'insert into art set ';
$query .= implode(', ', $pcs);

前面还有一点小技巧,因为如果需要默认值,您将无法使用Title列

// assuming use of proper method of sanitizing
// these values so we don't get SQL INJECTED!!
$artwork = 'artwork';
$title = 'title';
$artist = 'artist';
$medium = 'medium';

// make an array with the columns
$cols = explode(',', 'Artwork,Title,Artist,Medium');

// make an array with the values (that you sanitized properly!)
$vars = explode(',', 'artwork,title,artist,medium');

foreach ($cols as $i=>&$col) {
  $var = ${$vars[$i]};
  if ($col == 'Title') {
    if (empty($var)) {
      // don't add this column if empty
      continue;
    }
  }
  // otherwise (if not Title)
  // add it to a column = "value" insert string
  $pcs[] = "`$col` = '$var'";
}

// fortunately, we can insert with update syntax, too!
$query = 'insert into art set ';
$query .= implode(', ', $pcs);

在英语中始终使用小写字母

<input class="u-full-width" 
         type="text" 
         placeholder="Title of art" 
         id="Title"
         name="title">

在文本中始终使用小写字母

<input class="u-full-width" 
         type="text" 
         placeholder="Title of art" 
         id="Title"
         name="title">

你可以试试这个代码

如果省略该列,将设置默认值

因为只有一列具有默认值,所以可以使用此代码

如果有多个列具有默认值,则需要根据需要进行更改

你可以试试这个代码

如果省略该列,将设置默认值

因为只有一列具有默认值,所以可以使用此代码


如果有多个列具有默认值,则需要根据需要进行更改。

添加列或执行插入操作时会发生这种情况?@Perumal93执行插入操作时会发生这种情况。当我将Title的输入留空后,在phpmyadmin中检查数据库表时,它也是空的。下面是一篇文章,解释如何获取默认值,不要将标题添加到列列表或值列表中。不要插入任何内容,默认值将被插入。@WEBjuju啊,我明白你的意思了。似乎我要做多个if语句?这是在添加列或执行insert操作时发生的?@Perumal93这是在我执行insert操作时发生的。当我将Title的输入留空后,在phpmyadmin中检查数据库表时,它也是空的。下面是一篇文章,解释如何获取默认值,不要将标题添加到列列表或值列表中。不要插入任何内容,默认值将被插入。@WEBjuju啊,我明白你的意思了。看来我要做多个if语句了?我试过了,可惜没有成功。我甚至试着将“”从NULL中删除,但这只会使我的网站崩溃。我现在正在摆弄你的解决办法。非常感谢您对我的帮助。如果表单中的标题字段为空,他希望设置
无标题。如果代码为空,则会将其设置为
NULL
。单查询版本具有易于编辑的列列表,因此当数据结构更改时,您不必编辑两个查询;)我试过了,可惜没能奏效。我甚至试着将“”从NULL中删除,但这只会使我的网站崩溃。我现在正在摆弄你的解决办法。非常感谢您对我的帮助。如果表单中的标题字段为空,他希望设置
无标题。如果代码为空,则会将其设置为
NULL
。单查询版本具有易于编辑的列列表,因此当数据结构更改时,您不必编辑两个查询;)