【问题标题】:phpmyadmin not using DEFAULT value when input is left empty当输入为空时,phpmyadmin 不使用默认值
【发布时间】:2016-12-04 12:39:53
【问题描述】:

我有这个问题,如果我将“标题”的输入留空,那么当发送到数据库时它不会设置默认值:“无标题”。我在网上查看并确保我的设置在 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();
}
?>

【问题讨论】:

  • 添加列或执行插入操作时会出现这种情况?
  • @Perumal93 它发生在我执行插入操作时。当我在将 Title 的输入留空后检查 phpmyadmin 中的数据库表时,它也是空白的。
  • 这里有一篇文章解释了如何avoid SQL Injection
  • 要获取默认值,请勿将标题添加到列列表或值列表中。不要插入任何东西,将插入默认值。
  • @WEBjuju 啊,我明白你在说什么。似乎我将不得不制作多个 if 语句?

标签: php mysql


【解决方案1】:
$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);

你可以试试这个代码。

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

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

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

【讨论】:

    【解决方案2】:

    您有一些技巧,因为如果您需要默认值,您将无法使用 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);
    

    【讨论】:

    • 我试过了,但遗憾的是它并没有奏效。我什至尝试从 NULL 中删除“”,但这只是让我的网站崩溃。我现在正在摆弄你的解决方案。感谢您对我的大力帮助。
    • 如果表单中的标题字段为空,他想设置Untitled。您的代码将设置 NULL 以防它为空。
    • 具有易于编辑的列列表的单一查询版本,因此当您的数据结构发生变化时,您不必编辑两个查询;)
    【解决方案3】:

    中始终使用小写字母
    <input class="u-full-width" 
             type="text" 
             placeholder="Title of art" 
             id="Title"
             name="title">
    

    【讨论】:

      猜你喜欢
      • 2013-06-07
      • 2010-09-18
      • 2017-12-25
      • 2014-04-19
      • 2013-07-27
      • 1970-01-01
      • 2017-09-06
      • 2012-09-27
      相关资源
      最近更新 更多