PHP 与SQL:插入和更新表的功能不起作用

发布于 2024-11-28 07:45:53 字数 1321 浏览 0 评论 0原文

我有一个简单的代码,其中创建和修改表,但表不是一开始就创建的,并且出现以下错误:

未定义的变量:第 28 行的价格
未定义的变量:第 28 行的 newbrand
未定义变量:第 28 行的 newprice

第 28 行:

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

完整代码:

<?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" ,$this->link);}

        public function __destruct(){
        //desconectar
        }

}


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

I have a simple code where tables are created and modified, but the tables are not created to start with, and the following errors are appearing:

Undefined variable: price on line 28
Undefined variable: newbrand on line 28
Undefined variable: newprice on line 28

Line 28:

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

Complete 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" ,$this->link);}

        public function __destruct(){
        //desconectar
        }

}


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

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

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

发布评论

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

评论(1

月寒剑心 2024-12-05 07:45:53

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

您从未设置过 $price、$newbrand、$newprice 的值。而且你也没有转义你的数据:

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

同样的修改,你应该转义你的数据,请参阅: http://php.net/manual/fr/function.mysql-real-escape-string.php

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

You never set the value of $price, $newbrand, $newprice. And also you're not escaping your data :

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

And same for modify you should escape your datas see : http://php.net/manual/fr/function.mysql-real-escape-string.php

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