使用PHP提交表单时MySQL数据库不更新

时间:2016-07-25 04:24:14

标签: php html mysql runtime-error

这是我的网页:

// Info to connect to the Wishlist database
$servername = "em";
$dbusername = "";
$password = "!19";
$dbname = "";

// To connect to the database please
$conn = new mysqli($servername, $dbusername, $password, $dbname);

// If unable to connect to the database display this error
if ($conn->connect_error) {
    echo "Connection to wishlist failed";
    die("Connection failed: " . $conn->connect_error);
}

echo "Once you have added creatures to your wishlist, click " .
    "<strong><a href='http://eggcavity.com/edit-wishlist'>here</a></strong> to edit your wishlist.";

// Get current user's username
$current_user = wp_get_current_user();
$username = $current_user->user_login;

// Retrieve data from the database
$sql = "SELECT Name, Stage1 FROM Creatures";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    // Display all of the data from the database
    echo '<form method="POST">';
    while($row = $result->fetch_assoc()) {
        echo '<div style="display: inline-block; width: 30%;">' .
            '<img src="' . $row["Stage1"] . '"><br>'.
            $row["Name"] .
            '<br><input type="checkbox" name="creautres[]" value="' .
            $row["Name"] .
            '"></div>';
    }
    echo '<br><br><input type="submit" value="Submit"></form>';
} else {
    echo "Creatures not found";
}

if(isset($_POST['submit'])){
    foreach($_POST['creatures'] as $selected){
        $sql = "INSERT INTO " . $username .
            " (Creature, Picture, Stage, Gender, Frozen, Notes) VALUES ('" .
            $selected . "', 'http://static.eggcave.com/90x90/" . $selected .
            "_1', 'Stage1', 'Unspecified', 'Unspecified', 'Unspecified', '')";
        if ($conn->query($sql) === FALSE) {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
}

// Close the connection to the database
$conn->close();

它显示为我希望它: Display 但是,当我单击“提交”按钮时,它不会更新数据库。

我已经尝试回应$ stmt,它似乎是应该写的。 当我尝试回显$ selected时,在循环中它似乎没有输出任何东西。

你能帮助我吗?

我已更新代码以使用一个数据库。请帮我。它仍然没有添加。

3 个答案:

答案 0 :(得分:1)

您的复选框名称中有拼写错误。我重新填写了表单中的提交行和我相信的isset()

以下内容包括激活错误报告,try / catch,针对sql注入的安全绑定。数据保存。当然,您需要处理多次保存的唯一数据。例如,(生物,用户名)上的唯一键。我会重新考虑Id的专栏,但这是你的桌面设计。感谢您允许我们向您展示桌子的重复使用。祝你好运。

架构(来自你):

drop table if exists Wishlists;
CREATE TABLE `Wishlists` ( 
`ID` int(11) NOT NULL AUTO_INCREMENT, 
`Creature` varchar(100) DEFAULT NULL, 
`Picture` varchar(200) DEFAULT NULL, 
`Stage` varchar(100) DEFAULT NULL, 
`Gender` varchar(100) DEFAULT NULL, 
`Frozen` varchar(100) DEFAULT NULL, 
`Notes` varchar(500) DEFAULT NULL, 
`Username` varchar(100) DEFAULT NULL, 
PRIMARY KEY (`ID`) 
) ENGINE=InnoDB; -- <------------------------ went with InnoDB
-- truncate table Wishlists; -- used during early testing

PHP(eg​​gs01.php):

// Info to connect to the Wishlist database
$servername = "serve it up";
$dbusername = "dbu dbu dbu";
$password = "OpenSesame";
$dbname = "my db name";

try {
    // To connect to the database please
    $conn = new mysqli($servername, $dbusername, $password, $dbname);
    if ($conn->connect_error) {
        die('Connect Error (' . $conn->connect_errno . ') '
            . $conn->connect_error);
    }
    echo "I am connected and feel happy.<br/>";

    if(isset($_POST['submit'])){
        // Postback - submit

        // Get current user's username
        //$current_user = wp_get_current_user(); // remmed out, I don't have your system
        //$username = $current_user->user_login; // remmed out, I don't have your system
        $theCount=0;
        foreach($_POST['creatures'] as $selected){
            $Creature=$selected;
            $Picture="http://static.eggcave.com/90x90/" . $selected . "_1";
            $Stage="Stage1";
            $Gender="Unspecified";
            $Frozen="Unspecified"; 
            $Notes="Unspecified"; 
            $Username="Stackoverflow123";
            $sql = "INSERT Wishlists (Creature, Picture, Stage, Gender, Frozen, Notes, Username) " .
                   " VALUES (?,?,?,?,?,?,?)";
            $stmt = $conn->prepare($sql); // SQL Injection - safe prepare / bind / execute
            // 7 s's means 7 strings:
            $stmt->bind_param('sssssss', $Creature, $Picture, $Stage, $Gender, $Frozen, $Notes, $Username);
            $stmt->execute();
            $theCount++;
        }
        echo "<br>Santa has been notified, count = ".$theCount."<br>";
    }
    else {
        // Just display the form

        // Retrieve data from the database
        $result = $conn->query("SELECT Name, Stage1 FROM Creatures");
        if ($result->num_rows > 0) {
            // Display all of the data from the database
            echo '<form method="POST">';
            while($row = $result->fetch_array(MYSQLI_ASSOC)) {
                echo '<div style="display: inline-block; width: 30%;">' .
                    '<img src="' . $row["Stage1"] . '"><br>'.
                    $row["Name"] .
                    '<br><input type="checkbox" name="creatures[]" value="' .
                    $row["Name"] .
                    '"></div>';
            }
            echo '<br><br><button type="submit" name="submit">Submit</button></form>';
            $result->close();
        } else {
            echo "Creatures not found";
        }
    }
} catch (mysqli_sql_exception $e) { 
    throw $e; 
} 

提交后选择3个鸡蛋:

enter image description here

数据库图片:

enter image description here

答案 1 :(得分:0)

首先,看一下代码

<i>

在这里你缺少&#34; : 所以用

替换它
$dbname1 = *****";

再试一次

答案 2 :(得分:0)

第一个问题是你永远不应该在循环中运行SQL语句总是避免这种情况 - 因为它会给你的服务器带来很大的压力。

第二次尝试这个并告诉我输出

        foreach($_POST['creatures'] as $selected){
        $stmt = "INSERT INTO " . $username . " (Creature, Picture, Stage, Gender, Frozen, Notes) VALUES ('" . $selected . "', 'http://static.eggcave.com/90x90/" . $selected . "_1', 'Stage1', 'Unspecified', 'Unspecified', 'Unspecified', '')";
        if ($conn->query($stmt) === TRUE) {
        }
    }

    foreach($_POST['creatures'] as $selected){
        $stmt = "INSERT INTO " . $username . " (Creature, Picture, Stage, Gender, Frozen, Notes) VALUES ('" . $selected . "', 'http://static.eggcave.com/90x90/" . $selected . "_1', 'Stage1', 'Unspecified', 'Unspecified', 'Unspecified', '')";
         $result = $conn->query($stmt) OR die(var_dump($conn));    
var_dump($result->fetch_array(MYSQLI_ASSOC));    
die;

}