Raj Kumar
Raj Kumar

Reputation: 21

Query working in PHPMyAdmin but not in PHP

I am new to PHP and I have taken up an online tutorial, till now I had been working fine but now my database is not returning the query, though when I go to PHPmyadmin there I can get the query working fine.

Following is the code

    <?php 
ob_start();
//Delete Item question to admin and delete product

include"../storescripts/connect_to_mysql.php";

if (isset($_GET['deleteid'])) {
    echo 'Do you really want to delete the item with ID '.$_GET['deleteid'].'?<a href="inventory_list.php?yesdelete='.$_GET['deleteid'].'">Yes</a>|<a href="inventory_list.php">No</a>';
    exit();
    } 

if(isset($_GET['yesdelete'])){
    // Delete the actual product and delete picture also
    //delete from database
    //$id_to_delete = $_GET['yesdelete'];
    //echo  $id_to_delete;

     $sql =mysqli_query( "DELETE * FROM `products` WHERE `id`=2  LIMIT1 ");

    //mysql_query("DELETE * FROM `products` WHERE `id`='$id_to_delete'LIMIT1") or (mysql_error());

    //mysqli_query("DELETE * FROM products WHERE id=`$id_to_delete`LIMIT1");// or (mysql_error());

    //Unlink file from server
    $pictodelete=("../inventory_images/$id_to_delete");
    //echo $pictodelete;
    if(file_exists($pictodelete)){
        unlink($pictodelete);
        }

        header("location:inventory_list.php");
        exit();

    }   


?>

I would really appreciate the help, my server reads PHP Extension :mysqli .

Upvotes: 2

Views: 1013

Answers (3)

logsv
logsv

Reputation: 544

Error at query : $sql =mysqli_query( "DELETE * FROM products WHERE id=2 LIMIT1 ");

  • replace DELETE * FROM products with DELETE FROM products.DELETE delete row from table.
  • Procedure like mysqli_query takes at least two argument
    1. Link identifier returned form mysqli_connect
    2. Query string

And you haven't specified link as first arguments you should use returned link in to mysqi_query.

$con = mysqli_connect('localhost','root','password','db');
$sql =mysqli_query( $con,"DELETE FROM `products` WHERE `id`=2  LIMIT1 "); 

This link helps you link mysqli_query

Upvotes: 0

Maaz Rehman
Maaz Rehman

Reputation: 704

i dont know what is inside connect_to_mysql.php but at first there is a procedure to connect to a database which i am assuming that you have done correctly, it consist of code which looks something like that at default settings

<?php
$servername = "localhost";
$username = "root";
$password = "";
$databasename="abc";

// Create connection
$conn = mysqli_connect($servername, $username, $password,$databasename);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

the second things i see in your code

$sql =mysqli_query( "DELETE * FROM `products` WHERE `id`=2  LIMIT1 ");

it contains syntax errors,it should be

$sql =mysqli_query( $conn,"DELETE FROM `products` WHERE `id`=2  LIMIT 1 ");

Upvotes: 1

Antony
Antony

Reputation: 512

A space after Limit.

You have not specified the connection in mysqli_query() function.

eg:

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Perform queries 
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) 
VALUES ('Glenn','Quagmire',33)");

mysqli_close($con);
?>

In your case

$sql =mysqli_query( $connectionname,"DELETE * FROM `products` WHERE `id`=2  LIMIT 1 ");

Upvotes: 0

Related Questions