dongqiang5865 2015-10-05 07:46
浏览 31
已采纳

PHP OOP插入数据[关闭]

I'm facing a problem about inserting data into MySQL database.

public function insert($setTable, $setRow, $setValues) {

        $row = join(", ", $setRow);
        $values = join(", ", $setValues);       
        $result = mysqli_query($this->connect, "INSERT INTO $setTable ($row) VALUES ($values)");

        return $result;     
}

$currentLink = "test";
$st = 2;
$ide = 0;

$objMysql->insert("url", array("id_transaction", "id_user", "url"), array("$st", "$ide", "$currentLink"));

This is not working. What's wrong with my code?

  • 写回答

1条回答 默认 最新

  • dongwusang0314 2015-10-05 08:08
    关注

    You need to use implode() for rows / values also proper quotations. using join doest not implode comma to string. i dont know where you use $this->connect or $objMysql but you've to make sure it is properly defined. so below functions will work properly.

    function insert($setTable, $setRow, $setValues) {
    
            $row = implode(",", $setRow);
            $values = implode('","',$setValues);
            $prepareQuery = 'INSERT INTO '.$setTable.' ('.$row.') VALUES ("'.$values.'")';
            $result = mysqli_query($this->connect, $prepareQuery);
            return $result;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)