【问题标题】:Insert not working in php插入在php中不起作用
【发布时间】:2018-07-15 18:44:04
【问题描述】:

我以前做过,但这次我不知道出了什么问题!

这是我的 PHP 代码:

      if (isset($_POST['add_sub']) && !empty($_POST['add_sub'])) {
          $word = $_POST['word'];
          $phonetic = $_POST['phonetic'];
          $meaning = $_POST['meaning'];
          $engMeaning = $_POST['engMeaning'];
          $example = $_POST['example'];
          $eMeaning = $_POST['eMeaning'];

          $sqlAdd = "INSERT INTO words (word,meaning,eng-meaning,example,example-meaning,phonetic)
 VALUES ('$word','$meaning','$engMeaning','$example','$eMeaning','$phonetic')";

          $db->query($sqlAdd);
          header('location: index');
        }
      }

这是我的表格:

  <form action="index" method="post">
    <input type="text" name="word" id="word" placeholder="Word" value="">
    <input type="text" name="phonetic" id="phonetic" placeholder="Phonetic" value="">
    <input type="text" name="meaning" id="meaning" placeholder="Meaning" value="">
    <input type="text" name="engMeaning" id="endMeaning" placeholder="English Meaning" value="">
    <textarea type="text" name="example" id="example" placeholder="Example" value="" rows="5"></textarea>
    <textarea type="text" name="eMeaning" id="eMeaning" placeholder="Example Meaning" value="" rows="5"></textarea>
    <button type="submit" name="add_sub">Add</button>
  </form>

当我点击提交按钮时,它只是跳回索引页面,没有添加任何内容。

这里有什么我看不到的吗?!

更新:我可以毫无问题地从数据库中获取数据,只是无法插入。

【问题讨论】:

  • 您对SQL注入持开放态度,请改用Prepared statements
  • @Mehdi 问题是,它对我来说大部分时间都有效,但为什么这次不行?!
  • @Mehdi 以及“您对 SQL 注入持开放态度”是什么意思?
  • 您可能想了解 SQL 注入,这是一个有用的问题 How can I prevent SQL injection in PHP?。正如@mario 所说,您遇到的问题可能与缺乏转义有关
  • 此 SQL 永远无法运行。未引用时,您的标识符名称无效。 dev.mysql.com/doc/refman/8.0/en/identifiers.htmlbasic Latin letters, digits 0-9, dollar, underscore

标签: php html mysql forms crud


【解决方案1】:

在列上使用`,那么它应该可以工作:

$sqlAdd = "INSERT INTO words (`word`,`meaning`,`eng-meaning`,`example`,`example-meaning`,`phonetic`) VALUES ('$word','$meaning','$engMeaning','$example','$eMeaning','$phonetic')"

您应该使用反引号 (`) 表示表名和列名,使用单引号 (') 表示字符串

如果没有修复那么:

  1. 调试查询:

    $result = $sql->query($sqlAdd) or exit("Error code ({$sql->errno}): {$sql->error}");
    
  2. 您确定要传递$_POST['add_sub'] 吗?

  3. 检查您与数据库的连接

    if (mysqli_connect_errno()) {
         echo 'There was an error with your connection: '.mysqli_connect_error();
    }
    

【讨论】:

  • 建议使用反引号,但不是必需的。
  • example-meaningeng-meaning 是唯一需要反引号的。
  • @GoddamnAllien 所以请检查您与数据库的连接,向我们展示更多代码。你确定你正在传递 $_POST['add_sub']?
  • @Davidos 我从我的数据库中获取数据没有问题,我只是无法插入。
  • @Davidos 请看一下他连接 sql 和变量的方式。许多 phps 不支持它。你必须关闭 " 然后使用像 ` '".$var.".` 这样的目录。明白了吗?
【解决方案2】:

首先我在数据库 MyDB 中创建了一个表

CREATE TABLE `words` (
    `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `word` VARCHAR(50) NOT NULL DEFAULT '0',
    `phonetic` VARCHAR(50) NOT NULL DEFAULT '0',
    `meaning` VARCHAR(50) NOT NULL DEFAULT '0',
    `engmeaning` VARCHAR(50) NOT NULL DEFAULT '0',
    `example` VARCHAR(50) NOT NULL DEFAULT '0',
    `eMeaning` VARCHAR(50) NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`)
) ENGINE=InnoDB;

然后我创建了一个文件并将其命名为 index.php 并稍微修改了您的代码,直到它起作用

<?php
    $_names         = array("word", "phonetic", "meaning",  "engmeaning",       "'example", "eMeaning");
    $_ids           = array("word", "phonetic", "meaning",  "engMeaning",       "example",  "eMeaning");
    $_placeholders = array("Word",  "Phonetic", "Meaning",  "English Meaning",  "Example",  "Example Meaning");

    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";


    function x($x){
          if(isset($_POST[$x])){
              return addslashes(strip_tags($_POST[$x]));
    }}
    function i($i){
        global $_names;
        global $_ids;
        global $_placeholders;
        echo "<input type=\"text\" name=\"".$_names[$i]."\" id=\"".$_ids[$i]."\" placeholder=\"".$_placeholders[$i]."\" value=\"".x($_names[$i])."\" /><BR>";}
    function t($i){
        global $_names;
        global $_ids;
        global $_placeholders;
        echo "<textarea type=\"text\" name=\"".$_names[$i]."\" id=\"".$_ids[$i]."\" placeholder=\"".$_placeholders[$i]."\" >".x($_names[$i])."</textarea><BR>";}


    if (isset($_POST['add_sub']) && !empty($_POST['add_sub'])) {
        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }   
        $sql = "INSERT INTO `words` 
                    (   `word`,
                        `meaning`,
                        `engmeaning`,
                        `example`,
                        `eMeaning`,
                        `phonetic`
                    )VALUES(
                        '".x("word")."',
                        '".x("meaning")."',
                        '".x("engMeaning")."',
                        '".x("example")."',
                        '".x("eMeaning")."',
                        '".x("phonetic")."'
                    )"; 

        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
        $conn->close();
    }

?>
<form action="index.php" method="post">
<?php i(0);i(1);i(2);t(3);t(4); ?>
<input type="submit" name="add_sub" value="Add">
</form>

希望对你有用。

【讨论】:

  • 你的逃跑方法是完全错误和危险的。另见:stackoverflow.com/a/7810880/362536
  • @Brad,不要吹嘘,布拉德,提供代码来破解它。
  • 我做了...您是否阅读过我链接到您的答案?我不会称帮助您修复代码“吹牛”。
  • 里面没有杀手锏。证明我的代码很危险。或删除您的帖子。
猜你喜欢
  • 1970-01-01
  • 2015-08-17
  • 2012-08-05
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多