0

There is a code of crud operations in php using classes. plz help me to correct the error

Warning: mysql_query() expects parameter 1 to be string, resource given in C:\wamp\www\new\customers.php on line 44

<?php

class Customers
 {

private $name;

private $id;

private $desc;

private $con;

public function __construct($name,$desc)
{

   $this -> name = $name;
   $this-> desc = $desc;
   $this -> con = mysql_connect("localhost", "root", "");
   $this -> db  = mysql_select_db('cust');
}

public function insertData()
{

   $sql = " insert into desc_tbl values  ( 'name','desc')";
   $res = mysql_query($sql);
   mysql_query($res);
echo "<center>Record Inserted</center><br>";

}

public function deleteData($id)
{
    $sql = " delete from desc_tbl where id='$id'";
    $res = mysql_query($sql);
    mysql_query($res);
}

public function fetchData($id)
{
    $sql = " select * from desc_tbl where id='$id'";
    $res = mysql_query($sql);
    mysql_query($res);
}

public function updateData($name, $desc, $id)
{
    $sql = " update desc_tbl set name='$name' , desc='$desc' where id='$id'";
    $res = mysql_query($sql);
    mysql_query($res);
    echo "<center>Record Updated</center><br>";
}



}
?>

test.php

<?php 
require_once("customers.php");
{

$name = "John";
$desc ="Test Desc";

$c = new Customers($name,$desc);
$id = $c->insertData($desc);
echo "New Id is " .$id, '<br>'; 

$c = new Customers($name, $desc);
$id = $c -> deleteData($id);
echo "<center>Record Deleted</center>" .$id, '<br>';

$c = new Customers($name,$desc);
$id = $c -> fetchData($id);
echo "Result is" .$id, '<br>';

$c = new Customers($name,$desc, $id);
$id = $c -> updateData($name,$desc,$id);
echo "Updated Record is" .$id;
}

?>
3
Contributors
3
Replies
27
Views
3 Years
Discussion Span
Last Post by annyangel
1

You have (in every function):

$res = mysql_query($sql);
mysql_query($res);

You need (in every function):

$res = mysql_query($sql);

You try to execute it twice, but the second you use the wrong parameter.

-1

Also take note that it is not recommended to use the mysql_* functions (see the warning in this article, for example, on PHP's official website).

Edited by minitauros

0

now, plz see this code:

the insert function is working... now to run delete function?

customers.php

<?php 

class Customers
 {

private $name;

private $id;

private $desc;

private $con;

public function __construct($name,$desc)
{

   $this -> name = $name;
   $this-> desc = $desc;
   $this -> con = mysql_connect("localhost", "root", "");
   $this -> db  = mysql_select_db('cust');
}

public function insertData($desc_tbl)
{

   $sql = " insert into $desc_tbl values (NULL,'".$this->name."','".$this->desc."');";
   $res = mysql_query($sql);
   mysql_query($res);
echo "<center>Record Inserted</center><br>";

}

public function deleteData($id)
{
    $sql = " delete from desc_tbl where id='$id'";
    $res = mysql_query($sql);
    mysql_query($res);
echo "<center>Record Deleted</center><br>";
}

public function fetchData($id)
{
    $sql = " select * from desc_tbl where id='$id'";
    $res = mysql_query($sql);
    mysql_query($res[$id]);
echo "<center>Data is</center><br>";   
}

public function updateData($name, $desc, $id)
{
    $sql = " update desc_tbl set name='$name' , desc='$desc' where id='$id'";
    $res = mysql_query($sql);
    mysql_query($res);
echo "<center>Record Updated</center><br>";
}

}
?>

test.php

<?php 
require_once("customers.php");
$name = "John";
$desc ="Test Desc";
$id = "6";
$c =new Customers($name,$desc);

echo $c->insertData("desc_tbl");

//$c =new Customers($name,$desc,$id);

//echo $c->deleteData("desc_tbl");

?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.