PHP:帮助修改MySQL数据库上的表

时间:2011-08-05 18:36:46

标签: mysql database-connection

遵循以下示例...

    public function insert($model,$brand,$price){
        mysql_query("INSERT INTO autos (model, brand, price) VALUES ($model,'$brand', $price)",$this->link);} 

$conexion-> insert(05,"Ford",50000000);

...我做了修改/更新代码:

public function modify($model,$brand,$price,$newbrand,$newprice){
            mysql_query("UPDATE 'crautos'.'autos' SET 'brand' = '$newbrand',
'price' = '$newprice' WHERE 'autos'.'model' =5 AND 'autos'.'brand' = '$brand' AND 'autos'.'price' ='$price' LIMIT 1) VALUES ($newbrand, $newprice)",$this->link);}

$conexion-> modify("Mitsubishi", 40000000);

但它不起作用,这是错误输出:

  

警告:缺少MyDataBase :: modify()的参数3,在on中调用   第15行警告:缺少MyDataBase :: modify()的参数4,调用   在第28行并在第15行定义警告:缺少参数5   对于MyDataBase :: modify(),在第28行调用并在线定义   15 AND undefined变量newprice,newbrand,price。插入代码   很好,修改代码导致问题。

这是完整的代码:

    <?php

class MyDataBase{
    private $link;

    public function __construct($server,$user,$password,$base){
        //Conectar
        $this->link = mysql_connect($server,$user,$password);
        mysql_select_db($base,$this->link);
        }

        public function insert($model,$brand,$price){
            mysql_query("INSERT INTO autos (model, brand, price) VALUES ($model,'$brand', $price)",$this->link);}

        public function modify($model,$brand,$price,$newbrand,$newprice){
            mysql_query("UPDATE 'crautos'.'autos' SET 'brand' = '$newbrand',
'price' = '$newprice' WHERE 'autos'.'model' =5 AND 'autos'.'brand' = '$brand' AND 'autos'.'price' ='$price' LIMIT 1) VALUES ($newbrand, $newprice)",$this->link);}

        public function __destruct(){
        //desconectar
        }

}


$conexion = new MyDataBase ('localhost', 'root', '','crcars');
$conexion-> insert(05,"Ford",50000000);
$conexion-> modify("Mitsubishi", 40000000);
?>

2 个答案:

答案 0 :(得分:1)

更新查询中的语法无效。你不需要VALUES部分。这是应该如何:

 mysql_query("UPDATE 'crautos'.'autos' SET 'brand' = '$newbrand', 'price' = '$newprice' WHERE 'autos'.'model' =5 AND 'autos'.'brand' = '$brand' AND 'autos'.'price' ='$price' LIMIT 1");

答案 1 :(得分:0)

你的UPDATE语法无效,就像@Martin指出的那样,你也用两个参数调用modify(),但是函数期望5.将额外的值添加到$conexion-> modify("Mitsubishi", 40000000);

类似的东西:

$conexion-> modify("Mitsubishi", 40000000, $price, $newbrand, $newprice);