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

发布于 2024-11-28 07:17:31 字数 1952 浏览 0 评论 0原文

按照下面的示例...

    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,调用于 第 15 行警告:MyDataBase::modify() 缺少参数 4,调用 在第 28 行中并在第 15 行中定义警告:缺少参数 5 对于 MyDataBase::modify(),在第 28 行调用并在线定义 15 AND 未定义变量 newprice、newbrand、价格。插入代码 没问题,修改代码导致了问题。

这是完整的代码:

    <?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);
?>

Following this following example...

    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);

...I made a modify/update code:

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);

But it didn't work, this is the error output:

Warning: Missing argument 3 for MyDataBase::modify(), called in on
line 15 Warning: Missing argument 4 for MyDataBase::modify(), called
in on line 28 and defined in on line 15 Warning: Missing argument 5
for MyDataBase::modify(), called in on line 28 and defined in on line
15 AND undefined variable newprice, newbrand, price. THE Insert code
is fine, and the modify code is causing the issue.

Here's the full code:

    <?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);
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷路的信 2024-12-05 07:17:31

更新查询中存在无效语法。您不需要 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");

There is an invalid syntax in the update query. You do no need the VALUES part. Here is how it should be:

 mysql_query("UPDATE 'crautos'.'autos' SET 'brand' = '$newbrand', 'price' = '$newprice' WHERE 'autos'.'model' =5 AND 'autos'.'brand' = '$brand' AND 'autos'.'price' ='$price' LIMIT 1");
一袭白衣梦中忆 2024-12-05 07:17:31

您的 UPDATE 语法无效,就像@Martin 指出的那样,您还使用两个参数调用modify(),但该函数需要 5。将额外的值添加到 $conexion->;修改(“三菱”,40000000);

类似:

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

Your UPDATE syntax is invalid, like @Martin pointed out, also you're calling modify() with two parameters, but the function is expecting 5. Add the extra values to $conexion-> modify("Mitsubishi", 40000000);

Something like:

$conexion-> modify("Mitsubishi", 40000000, $price, $newbrand, $newprice);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文