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

8
  • I am sorry about the formatting, I tried hard but I could not correct it. Commented Dec 4, 2011 at 15:34
  • 1
    is your table name 'devices' or 'pushdevices'...? Commented Dec 4, 2011 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? Commented Dec 4, 2011 at 15:37
  • 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? Commented Dec 4, 2011 at 15:38
  • i have checked there is no spcaces, Commented Dec 4, 2011 at 15:48

2 Answers 2

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();}
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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(...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.