PHP生成空白屏幕,点击提交按钮

时间:2017-11-08 23:48:39

标签: php

下面我有代码,应该更新数据库中的条目。当我单击提交按钮时,表单会消失,但不会被任何内容替换,更重要的是它不会更新数据库。我似乎无法找到错误的位置,并且非常感谢任何帮助。

The update page before submitting

The update page after submitting

<?php
define('TITLE', 'Quotes Entry!');
// Include the header:
include('header.php');
include('mysqli_connect.php');
// Leave the PHP section to display lots of HTML:
?>
<?php //

mysqli_set_charset($dbc, 'utf8');

if (isset($_GET['id']) && is_numeric($_GET['id']) ) { // Display the entry in a form:
    // Define the query:
    $query = "SELECT title, entry FROM Salinger WHERE entry_id={$_GET['id']}";
    if ($r = mysqli_query($dbc, $query)) { // Run the query.

        $row = mysqli_fetch_array($r); // Retrieve the information.

       //make the form
        print '<form action = "edit_entry.php" method = "post">
                <p> Entry Titles <input type= "text" name = "title" size = "40" maxsize = "100" value = "' . htmlentities($row['title']) . '" /></p>
                <p>Entry Text <textarea name = "entry" cols = "40" rows = "5">'. htmlentities($row['entry']).'</textarea></p>
                <input type = "hidden" name = "id" value = "'.$_GET['id'] .'" />
                <input type = "submit" name = "submit" value = "Update This Entry!" />
                </form>';
    } else { // Couldn't get the information.
        print '<p style="color: red;">Could not retrieve the blog entry because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
    }

} elseif (isset($_POST['id']) && is_numeric($_POST['id'])) { // Handle the form.
    $problem = "false";
    if(!empty($_POST['title']) && !empty($_POST['entry'])){
        $title = mysqli_real_escape_string($dbc, trim(strip_tags($_POST['title'])));
        $entry = mysqli_real_escape_string($dbc, trim(strip_tags($_POST['entry'])));
    } else{
        print '<p style="color: red;">Could not retrieve the blog entry because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
        $problem = true;
    }
    if(!problem){
        $query = "UPDATE Salinger SET title = '$title', entry = '$entry' WHERE entry_id = {$_POST['id']}";
        $r = mysqli_query($dbc, $query); //execute the query

        if(mysqli_affected_rows($dbc) == 1){
            print'<p> The blog entry has been updated.</p>';

            // Report on the result:
        } else {
            print '<p style="color: red;">Could not retrieve the blog entry because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
        }
    }
} else{
    print '<p style="color: red;">Could not retrieve the blog entry because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
}



mysqli_close($dbc); // Close the database connection.
include('footer.php'); // Need the footer.
?>

2 个答案:

答案 0 :(得分:1)

因为你设置$problem = "false";您需要将其设置为$problem= false;

"false"不是假的

而且!问题应该是!$ problem

答案 1 :(得分:0)

GET[id]出现问题。 它在屏幕上显示POST事件的空白原因,因为您的SQL没有找到记录。

测试select语句中的硬编码值。

实施例

$query = "SELECT title, entry FROM Salinger WHERE entry_id=10";