PHP 和 SQL : Function to insert and update tables not working

标签 php mysql sql database database-connection

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

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

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

最佳答案

$conexion-> 修改("三菱",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

关于PHP 和 SQL : Function to insert and update tables not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6962876/

相关文章:

php - 使用 PHP 从数组中删除项目

javascript - 如何在同一个php文件中的php代码之后运行javascript代码?

php - mysql组之间的用户注册计数

PHP ("SELECT * FROM Table WHERE ... AND ...")

sql - 在 postgres 表中存储指数

php - 在 Woocommerce 中使用短代码显示带有动态产品 ID 的自定义按钮

php - 我怎样才能把粘在一起的单词分开?

java - 根据表单值从数据库中获取数据

php - SELECT 列中的最后一个值始终为 null 或 false SQL

MYSQL - 查询选择随机行限制父ID