PHP URL Shortener:Shortener.php返回错误消息

时间:2014-05-29 18:09:55

标签: php mysql url

所以我基于这个YouTube教程(PHPAcademy)为我的网站创建了一个URL Shortener,因为缺乏PHP和MySQL的经验,我觉得我很快就接受了它,但是我收到了这个错误提交网址时的消息。

  

解析错误:语法错误,意外'更新'第37行/home/langers/public_html/r/shorten/classes/Shortener.php中的(T_STRING)

但是,在代码中,它提到的行和T_STRING'更新'这意味着意外,在教程中需要。

<?php
class Shortener {
    protected $db;

    public function __construct() {
        //Demo Purposes
        $this->db = new mysqli('localhost', 'langers_langers', 'password','langers_website');
    }

    protected function generateCode($num){
        return base_convert($num, 10, 36);
    }

    public function makeCode($url){
        $url = trim($url);

        if(!filter_var($url, FILTER_VALIDATE_URL)) {
            return '';
        }

        $url = $this->db->escape_string($url);

        //Check if URL already exists
        $exists = $this->db->query("SELECT code FROM links WHERE url ='{$url}'");

        if($exists->num_rows){
            return $exists->fetch_object()->code;
        } else {

        //Insert record without a code
        $insert->$this->db->query("INSERT INTO links (url, created) VALUES ('{$url}', NOW());

        //Generate code based on id
        $code = $this->generateCode($this->db->insert_id);

        //Update Record
        $this->db->query("UPDATE links SET code = '{$code}' WHERE url = '$url'");

        return $code;
        }
    }

    public function getUrl($code){

    }
}

?>

关于如何修复它以便仍然更新MySQL数据库的任何想法?

由于

1 个答案:

答案 0 :(得分:1)

错误发生在您错过

的收盘价之前
//Insert record without a code
$insert->$this->db->query("INSERT INTO links (url, created) VALUES ('{$url}', NOW());

请注意,您使用$ insert就好像它是一个对象,但它不是。也许你的意思是:

$insert = $this->db->query("INSERT INTO links (url, created) VALUES ('{$url}', NOW())");