doudao8283 2018-11-11 16:31
浏览 43
已采纳

我正在尝试使用php和mysql OOP向数据库添加记录

everyone, I have a hard time to understand what is going on. I'm new on OOPS and wanted to add a record to my database. I have a class customer and in that class, I have a function create() that makes a new record and insert into DB. My connection is working, I instantiate(hope that is the right term) that function and then I call 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;
    }

going to my page to see if I get any result. I have Incorrect integer value: '' for column 'phone_number' at row 11366.

My database fields are: -

ID
first_name
last_name
phone_number
email
codice_fiscale
adress
cap
payment
data_of_join
genre

Really don't know what is the problem. I made the same thing but in procedural same SQL and everything works just fine.

  • 写回答

2条回答 默认 最新

  • doupacan2098 2018-11-11 16:49
    关注

    Your code is open to SQL injection related attacks. Please learn to use Prepared Statements

    Now, in this case, you do not need to use $this-> to use the variables. $this is used when the variables are accessible class members. However, in your cases, these are function parameters.

    In the current code, you can change the SQL string as follows:

    $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')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题