douyi4991 2015-08-02 23:16
浏览 29

使用mysql_real_escape_string成功插入新记录后获取空白字段

I insert new records to a MySQL database/table using PHP and every thing is fine. Collation of DB and tables are utf8_unicode_ci. Here is the insertion code:

if($_SERVER['REQUEST_METHOD'] = "POST") 
{
    $category = mysql_real_escape_string($_POST['category']);

    mysql_connect('localhost', 'xxx','xxxx') or die(mysql_error()); //Connect to server
    mysql_select_db('xxxx') or die("Cannot connect to database"); //Connect to database

    mysql_query("INSERT INTO categories (category) VALUES ('$category')"); //SQL query
    header("location: home.php");
}
else
{
    header("location:home.php"); //redirects back to home
}

I create same database in my hosting service, I use WAMP/phpMyAdmin/Export + data. Every thing looks fine in my hosting/phpMyAdmin and the records are there in tables. Collations are the same. BUT when I try to insert a record, the new text fields are blank, I have a new record with new id but text field content is empty. No error message. There is a warning:

Warning: mysql_real_escape_string(): Access denied for user 'yyyy'@'localhost' (using password: NO) in /home/yyyyyy/public_html/xxxxx/add.php on line 12

'yyyy' is a different name from xxxx in mysql_select_db('xxxx') that I do not know where is it coming from?

  • 写回答

3条回答 默认 最新

  • dtbc37573 2015-08-02 23:33
    关注

    Check to see if $category is null

    if($_SERVER['REQUEST_METHOD'] ==  "POST" && isset($_POST['category'])) 
    {
        $category = mysql_real_escape_string($_POST['category']);
    
        mysql_connect('localhost', 'xxx','xxxx') or die(mysql_error()); //Connect to server
        mysql_select_db('xxxx') or die("Cannot connect to database"); //Connect to database
    
        mysql_query("INSERT INTO categories (category) VALUES ('$category')"); //SQL query
        //header("location: home.php");
        echo 'successful'
    }
    else
    {
        //header("location:home.php"); //redirects back to home
        echo 'problem'
    }
    

    Also fix:

     if($_SERVER['REQUEST_METHOD'] = "POST") 
    

    To

     if($_SERVER['REQUEST_METHOD'] == "POST") 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line