Reputation: 1
So I am creating a URL Shortener for my site based off of this YouTube tutorial (PHPAcademy) due to a lack of experience in PHP and MySQL, I feel I'm picking it up quickly, however I receive this error message when submitting the URL.
Parse error: syntax error, unexpected 'UPDATE' (T_STRING) in /home/langers/public_html/r/shorten/classes/Shortener.php on line 37
However ,in the code, the line it mentions and the T_STRING 'UPDATE' that implies is unexpected, is needed in the tutorial.
<?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){
}
}
?>
Any ideas on how I can fix it so it still updates the MySQL database?
Thanks
Upvotes: 0
Views: 212
Reputation: 17481
The error happens before, when you miss the closing quotes at
//Insert record without a code
$insert->$this->db->query("INSERT INTO links (url, created) VALUES ('{$url}', NOW());
Please note also that you're using $insert as if it was an object, but it isn't. Perhaps you meant:
$insert = $this->db->query("INSERT INTO links (url, created) VALUES ('{$url}', NOW())");
Upvotes: 1