PHP & SQL: функция вставки и обновления таблиц не работает - PullRequest
0 голосов
/ 06 августа 2011

У меня есть простой код, в котором таблицы создаются и изменяются, но таблицы не создаются для начала, и появляются следующие ошибки:

Неопределенная переменная: цена в строке 28
Неопределенная переменная: newbrand в строке 28
Неопределенная переменная: newprice в строке 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);
?>

1 Ответ

1 голос
/ 06 августа 2011

$conexion-> modify("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

...