PHP MySQL未插入数据库 [英] PHP MySQL not inserting into database

查看:48
本文介绍了PHP MySQL未插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过这个问题,但似乎无法弄清楚.我有正确的数据库和表名.我没有发现任何错误,我什至自己在 phpmyadmin 上插入了一个有效的表格,但是当我尝试在我的网站上执行此操作时,它不起作用.我什至测试了连接..不知道现在该怎么办

I have heard of this issue but can't seem to figure it out. I have the database and table names correct. I am not finding any errors and i even inserted a table myself on phpmyadmin that worked but when I tried to do it on my site it doesnt work. I even tested the connection..Not sure what to do now

也许有人可以看看我的代码,看看他们是否注意到了什么

Maybe someone can take a look at my code and see if they notice anything

<?php
if(mysql_connect('<db>', '<un>', '<pw>') && mysql_select_db('smiles'))
{

    $time = time();
    $errors = array();

    if(isset($_POST['guestbook_name'], $_POST['guestbook_message'])){
        $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)>255) {
            $errors[] = 'One or more fields exceed the character limit.';
        }
        if (empty($errors)) {

            $insert = "INSERT INTO 'guestbook'VALUES('','$time','$guestbook_name','$guestbook_message')"; 
            if($insert = mysql_query($insert)){
                header('Location: '.$_SERVER['PHP_SELF']);
            } else{
              $errors[] = 'Something went wrong . Please try again.';
            }
        } else {
            foreach($errors as $error) {
            echo '<p>'.$error.'</p>';
            }
        }

    }
    //display entries
} 
else {
    'Fixing idiot';
}
      ?> 
      <hr />
       <form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="POST">
     <p>Post Somethign</p>
      <br />
          Name:<br /><input type="text" name="guestbook_name" maxlength="25" />
   <br />
    Message:
    <br />
           <textarea name="guestbook_message" rows="6" coles="30"maxlength="255"></textarea>
      <input type="submit" value="Post" />
      </form>

推荐答案

表名不需要引号,假设您使用的是 id 自动增量,请不要插入空字符串.所以应该是:

Table name doesn't need quotes and supossing you're using id autoincrement, don't insert an empty string. So it should be:

$insert = "INSERT INTO guestbook VALUES('$time','$guestbook_name','$guestbook_message')";

另外,看看你的时间价值.MySQL 数据类型是什么?

Also, take a look at your $time value. What MySQL data type is?

插入后,尝试显示mysql错误:

After the insert, try to display the mysql error:

$conn = mysql_connect('<db>', '<un>', '<pw>');
mysql_query($insert)
if (mysql_errno($conn)){
   $errors[] = mysql_error($conn);
}else{
   header('Location: '.$_SERVER['PHP_SELF']);
}

孔片段应类似于:

<?php
$conn = mysql_connect('<db>', '<un>', '<pw>')
if( $conn && mysql_select_db('smiles')) //Note $conn
{

    $time = time();
    $errors = array();

    if(isset($_POST['guestbook_name'], $_POST['guestbook_message'])){
        $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)>255) {
            $errors[] = 'One or more fields exceed the character limit.';
        }
        if (empty($errors)) {
            mysql_query($insert)
            $insert = "INSERT INTO guestbook VALUES('$time','$guestbook_name','$guestbook_message')"; 
            if (mysql_errno($conn)){
               $errors[] = mysql_error($conn);
            }else{
               header('Location: '.$_SERVER['PHP_SELF']);
            }
        } else {
            foreach($errors as $error) {
            echo '<p>'.$error.'</p>';
            }
        }

    }
    //display entries
} 

这篇关于PHP MySQL未插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆