duan89197 2011-12-04 15:33
浏览 36
已采纳

查询无效:表不存在

I am trying to insert data in table in mysql database through php code but I am always getting following error:
Invalid query: Table 'whatsup_wp1.pushDevices' doesn't exist

I am using following code:

    <?php 
    $deviceid = $_GET["deviceid"];
    $link = mysql_connect('localhost', 'whatsup_wp1', 'XSvUCl0FugzV4');
    if (!$link) {
        die('Not connected : ' . mysql_error());
    }

    // make foo the current db
    $db_selected = mysql_select_db('whatsup_wp1', $link);
    if (!$db_selected) {
        echo 'Can\'t use whatsup_wp1 : ' . mysql_error();
    }
    else
        {
    //echo 'connect';
    }
    //$query = "select count(*) from city";
    //$query = "insert into devices (pushID) values('".$deviceid."')";
    $query = "INSERT INTO pushDevices(device) VALUES ('".$deviceid."')";
    echo $query;
    $result = mysql_query($query);
    if (!$result){
        die('Invalid query: ' . mysql_error());
    }
    echo $result;
    ?>

This database have more tables and I am able to use them. I am having problem with the tables that I am creating today. They appears in phpmyadmin but somehow I am not able to get use them through my php code.

Any help may be vital for me. I have spent complete day on it.

Thanks
Pankaj

  • 写回答

2条回答 默认 最新

  • duanliu6083 2011-12-04 15:50
    关注

    Its hard to tell by What your saying but i have a suggestion.... It looks like theres no table selected try this

    it formatted like this

    $query = "INSERT INTO mydb.mytable
    (mytablefield)
    VALUES
    ('myfieldvalue')"
    $result = mysql_query($query);
    if (!$result){
        die('Invalid query: ' . mysql_error());
    }
    

    My guess is you meant for it to be like this?

    $query = "INSERT INTO whatsup_wp1.devices 
    (device)
    VALUES
    ('".$deviceid."')"
    $result = mysql_query($query);
    if (!$result){
        die('Invalid query: ' . mysql_error());
    }
    

    And for security reasons i recommend this...

    else
        {
    //echo 'connect';
    $deviceid = mysql_real_escape_string(stripslashes($deviceid));
    }
    

    Change to

    else
        {
    //echo 'connect';
    $deviceid = mysql_real_escape_string(stripslashes($deviceid));
    }
    

    Personally i just use it like this

    $result = mysql_query("INSERT INTO mytable
    (mytablefield)
    VALUES
    ('myfieldvalue')");
    if($result){echo "Works!";}
    else{die('Invalid query: ' . mysql_error());exit();}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用