1

我有一个带有一些 PHP 代码的简单表单,可以将一些简单的信息发布到数据库中。

与数据库的连接很好,因为它显示了我手动输入的项目,这正是我想要的方式;然而。我无法将其从表单发布到数据库中。我已经修改了很多代码,但没有运气。有人看到我缺少的东西吗?这是代码:

<center>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">

    <strong><h2><br><br>Enter in my guestbook!</h2></strong><br>
    <h3>Nickname:</h3><input type="text" name="guestbook_name" maxlength="25"><br>
    <h3>Write About Your Day:</h3><textarea name="guesbook_message" rows="6" cols="60"       maxlength="255"></textarea><br>
    <input type ="submit" value="POST">

</form>

<br>
<hr>
<br>

<?php

    if (mysql_connect('localhost', 'root', 'root') && mysql_select_db('mydb')) {

    //echo 'Connected to the database!';

    $time = gmdate("l dS \of F Y h:i:s A");
    $errors = array();

    if (isset($_POST['guestbook_name'], $_POST['guestbook_message'])) {

        echo 'Set data!';

        $guestbook_name = mysql_real_escape_string(htmlentities(     $_POST['guestbook_name'])); 
        $guestbook_message = mysql_real_escape_string(htmlentities(     $_POST['guestbook_message'])); 

        if (empty($guestbook_name) || empty($guestbook_message)) {
        $errors[] = 'All fields are required.';
        }

        if (strlen($guestbook_name)>25 || strlen($guestbook_message)>250){
        $errors[] = 'One or more fields exceeded the character limit!'; 
        }

        if (empty($erros)) {
            $insert = "INSERT INTO guestbook VALUES     ('','$time','$guestbook_name','$guestbook_message')";
            if (mysql_query($insert)){
                 header ('Location: '.$_SERVER['PHP_SELF']);
            } else {
                $errors[] = 'Something went wrong. Try again.';
            }

        } else {
            foreach($errors as $error) {
                echo'<p><strong>'.$error.'</strong></p>'; 
            }

        }

    }

    // display entries 
    $entries = mysql_query("SELECT timestamp, name, message FROM guestbook ORDER BY timestamp DESC");

    if (mysql_num_rows($entries)==0){
        echo 'No entries, yet.';
        } else { 
            //echo 'Entries found.'; 

            while ($entries_row = mysql_fetch_assoc($entries)){
                $entries_timestamp = $entries_row['timestamp'];
                $entries_name = $entries_row['name'];
                $entries_message = $entries_row['message'];
                echo '<p><strong>Posted by '.$entries_name.' on     '.$entries_timestamp.'</strong>:<br><br>'.$entries_message.'</p><br><br>';

            }
        }

} else {
    echo 'Could not connect at this time.';
} 


?>

</center>

我的主要问题是提交数据库,任何帮助都会很棒,谢谢!

凯尔 B。

4

2 回答 2

0

我们将需要查看您的数据库表结构与您的插入 sql 语句,以查看您是否正确插入。

插入失败的最可能原因是未设置设置为 NOT NULL 的字段值(通过未插入值或具有空/错误类型的变量)。

但更常见的问题是:

wrong table or field names
not having the enough privelages on your database user account
于 2012-04-17T16:54:58.080 回答
0

可能是错字?

if (empty($erros)) {

我相信正确的名称是 $errors(注意 R)

于 2012-04-17T16:56:28.777 回答