按钮单击时未插入数据

时间:2015-02-02 17:12:16

标签: php

如果我不尝试使用按钮点击,我在页面加载时正常工作。但是添加按钮单击,我无法将数据插入到我的数据库中。这是php:

if(isset($_POST['submit']))
{
    $name = "Awesome Name";
    $size = 5.3;
    $color = "black";
    $speciesName = "Tiger";
    $speciesDescription = "Super Test";

    $sql = "INSERT IGNORE INTO tbl_type (Name, Description) VALUES ('$speciesName', '$speciesDescription')";

    $result = mysqli_query($conn, $sql) or die("Query fail: " . mysqli_error($conn));

    $query = "SELECT Id FROM tbl_type WHERE Name = '$speciesName'";

    if($id = $conn->query($query)){
            $row = $id->fetch_assoc();
            $intId = $row['Id'];
    }

    $sql2 = "INSERT INTO tbl_dog (Name, Size, Color, Species) VALUES ('$name', $size, '$color', $intId)";

    $result2 = mysqli_query($conn, $sql2) or die("Query fail: " . mysqli_error($conn));
}

这是我的按钮:

<form action="index.php" method="post">
    <input name="btnInsert" type="submit" value="Insert" />
    <input name="btnUpdate" type="button" value="Update" />
</form>

我感觉我错过了一些愚蠢的东西,我只是不知道它是什么。为什么这不会插入我的数据?

1 个答案:

答案 0 :(得分:2)

试试这个,

<form action="index.php" method="post">
    <input name="submit" type="submit" value="Insert" />
    <input name="submit_update" type="submit" value="Update" />
</form>