0

I am having below error when trying to delete an item.

Fatal error: Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'databaseName.tableName' doesn't exist in /storage/h1/735/500735/public_html/delete.php:21 Stack trace: #0 /storage/h1/735/500735/public_html/delete.php(21): PDOStatement->execute() #1 {main} thrown in /storage/h1/735/500735/public_html/delete.php on line 21

Note : tableName here is tblcart

File delete.php

<?php
    session_start();
    require("dbconfig.php");
    if (isset($_GET['delete_id'])) {
        $stmt_select = $DB_con->prepare('SELECT * FROM tblcart WHERE productID =:id');
        $stmt_select->execute(array(':id'=>$_GET['delete_id']));
        $result=$stmt_select->fetch(PDO::FETCH_ASSOC);

        $stmt_delete = $DB_con->prepare('DELETE FROM tblCart WHERE productID =:id AND userID =:userID');
        $stmt_delete->bindParam(':id', $_GET['delete_id']);
        $stmt_delete->bindParam(':userID', $_SESSION['userid']);
        $stmt_delete->execute();

        header("Location: cart.php");
    }
?>

Refer to the error note. The error occurred on line $stmt_delete->execute();. If I run the code through my local PC localhost, it is functioning well, but when running on a web hosting server I am getting this error.

File dbconfig.php

<?php
    $DB_HOST = 'localhost';
    $DB_USER = 'root';
    $DB_PASS = 'password';
    $DB_NAME = 'databaseName';

    try{
        $DB_con = new PDO("mysql:host={$DB_HOST};dbname={$DB_NAME}",$DB_USER,$DB_PASS);
        $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e){
        echo $e->getMessage();
    }
?>
Community
  • 1
  • 1
Eaten Taik
  • 868
  • 2
  • 12
  • 35
  • 1
    `Table 'databaseName.tableName' doesn't exist` what's unclear? – u_mulder Jan 27 '17 at 08:44
  • It might be useful to see the code in `dbconfig.php` as well – RiggsFolly Jan 27 '17 at 08:44
  • NOTE: If you need a `productID` and a `userID` to make the delete unique then you probably also need the `userID` in the `SELECT` or you might get more than one row selected by the SELECT and be deleting randon rows – RiggsFolly Jan 27 '17 at 08:46
  • I'm assuming you can find `tableName` somewhere in your code, since it's the table missing in the error, and the code you're showing only refer `tblcart`. Maybe you should start with fixing that. – fpietka Jan 27 '17 at 09:02
  • @fpietka the `tableName` is referring to `tblcart`. So the error note should be `databaseName.tblcart` but I do on purpose replacing the `databaseName.tblcart` as `databaseName.tableName` – Eaten Taik Jan 27 '17 at 09:16
  • Are you indeed using root access? else you may check your user's permission – fpietka Jan 27 '17 at 09:18
  • Is it Unix/Linux the server which is running? – Antonios Tsimourtos Jan 27 '17 at 09:26
  • You have `tblcart` in the select and `tblCart` in the delete (upper case C). If you're running on Unix/Linux, this will be case sensitive. – gabe3886 Jan 27 '17 at 09:32

1 Answers1

3

Database and table names on Linux servers are case-sensitive.

Database and table names on Windows servers are not case-sensitive.

This is why it works on your computer (Windows) and not on the web hosting server (Linux)

Change the delete statement from:

$stmt_delete = $DB_con->prepare('DELETE FROM tblCart WHERE productID =:id AND userID =:userID');

To:

$stmt_delete = $DB_con->prepare('DELETE FROM tblcart WHERE productID =:id AND userID =:userID');

The difference is tblCart to tblcart.

A reference is here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Antonios Tsimourtos
  • 1,676
  • 1
  • 14
  • 37