插入在php中不起作用

vi4fp9gy  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(674)

我以前做过,但这次我不知道怎么了!
以下是我的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>

当我点击提交按钮,它只是跳回索引页,没有添加任何内容。
有什么我看不见的吗?!
更新:我可以从我的数据库没有问题的数据,我只是不能插入。

gpnt7bae

gpnt7bae1#

首先,我在数据库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>

我希望它有用。

yhqotfr8

yhqotfr82#

在列中使用`at,则应该可以:

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

表和列名应该使用反引号(`),字符串应该使用单引号(')
如果没有修复:
调试查询:

$result = $sql->query($sqlAdd) or exit("Error code ({$sql->errno}): {$sql->error}");

你确定你要通过吗 $_POST['add_sub'] ?
检查与数据库的连接

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

相关问题