0

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

pankaj
  • 7,878
  • 16
  • 69
  • 115
  • I am sorry about the formatting, I tried hard but I could not correct it. – pankaj Dec 04 '11 at 15:34
  • 1
    is your table name 'devices' or 'pushdevices'...? – Sudhir Bastakoti Dec 04 '11 at 15:36
  • 2
    The error message and your code do not match, is the tablename `devices` as stated in the error message or `pushDevices` as stated in the query string? – home Dec 04 '11 at 15:37
  • i have checked there is no spcaces, – pankaj Dec 04 '11 at 15:48
  • and I have tables devices and pushDevices both,.... when it was not working with devices table I tried pushDevices but again no luck... – pankaj Dec 04 '11 at 15:52
  • Make sure the case of the table name matches. This might not be a problem on Windows, but it is on other operating systems such as Linux. – knittl Dec 04 '11 at 15:56
  • check that you have created the table in the location you thought you did. also, possibly it is a permission issue between the creating user and the querying user? – Randy Dec 04 '11 at 15:38
  • Does this answer your question? [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Dharman Apr 16 '20 at 21:42

2 Answers2

0

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();}
LeeWazHere
  • 26
  • 4
  • Thanks Lee for answering me... but hard luck for me I still could not get any success. Is there a way to list all the tables of database through query in php? – pankaj Dec 04 '11 at 16:14
  • this link has your answer http://www.brightcherry.co.uk/scribbles/2008/11/14/mysql-php-show-tables-in-database/ – LeeWazHere Dec 04 '11 at 22:21
0

If you are on Linux, check that the case is the same.

On windows MySql is case insensitive, on Linux, it is case sensitive.

Also, you are missing a space after pushDevice: pushDevice(...

Sylverdrag
  • 8,898
  • 5
  • 37
  • 54