各位,我很难理解这是怎么回事。我是 OOPS 的新手,想在我的数据库中添加一条记录。我有一个类(class)客户,在那个类(class)中,我有一个创建新记录并插入数据库的函数 create()。我的连接正常,我实例化(希望这是正确的术语)该函数,然后调用 create()。
$costumer = new Customer($args);
$date = date("Y-m-d");
$result = $costumer->create("Nome", "Cognome", 2, "email3@email.com", "12", "address", 00133, "payment", $date, "male");
public function create($first_name, $last_name, $phone_number, $email, $codice_fiscale, $adress, $cap, $payment, $data_of_join, $genre) {
$sql = "INSERT INTO costumers (first_name, last_name, phone_number, email, codice_fiscale, adress, cap, payment, data_of_join, genre)
VALUES ('$this->first_name','$this->last_name','$this->phone_number','$this->email','$this->codice_fiscale','$this->adress','$this->cap','$this->payment','$this->data_of_join','$this->genre')";
$result = self::$database->query($sql);
if(!$result) {
echo self::$database->error;
echo self::$database->errno;
}
return $result;
}
去我的页面看看我是否得到任何结果。我的整数值不正确:第 11366 行的“phone_number”列的“”。
我的数据库字段是:-
ID
first_name
last_name
phone_number
email
codice_fiscale
adress
cap
payment
data_of_join
genre
真不知道是什么问题。我做了同样的事情,但在程序相同的 SQL 中,一切正常。
最佳答案
您的代码对 SQL injection 开放相关攻击。请学会使用Prepared Statements
现在,在这种情况下,您不需要使用 $this->
来使用变量。 $this
当变量是可访问的类成员时使用。但是,在您的情况下,这些是函数参数。
在当前代码中,您可以按如下方式更改 SQL 字符串:
$sql = "INSERT INTO costumers (first_name, last_name, phone_number, email, codice_fiscale, adress, cap, payment, data_of_join, genre)
VALUES ('$first_name','$last_name','$phone_number','$email','$codice_fiscale','$adress','$cap','$payment','$data_of_join','$genre')";
关于php - 我正在尝试使用 php 和 mysql OOP 将记录添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53250795/