Alex Mack
Alex Mack

Reputation: 1

Another Little Error (Probably Obvious)

I'm writing a URL shortener. I'm a Noob, keep in mind ;)

UPDATED AFTER ANSWER #1. GOT A NEW ERROR

Here is my error:

Unknown column 'Unknown column '' in 'field list''

Here is my code

<?php
include("db.inc.php");

function is_min($url)
{

return(preg_match("/jbgc\.me/i", $url)) ? true : false; 

}


function gen_code()
{
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';    
return substr(str_shuffle($charset), 0, 6);
}



function exists($code) {
$code = mysql_real_escape_string($code);
$code_exists = mysql_query("SELECT COUNT(`url_id`) FROM `mini_shortener` WHERE   
`code`='$code' LIMIT 1") or die(mysql_error());
return (mysql_result($code_exists, 0) == 1) ? true : false;
 }



function shorten($url, $code){
$url = mysql_real_escape_string($url);
$code = mysql_real_escape_string($code);
mysql_query("INSERT INTO `mini_shortener` (`url`,`code`) VALUES('$url', '$code')")    
 or die (mysql_error());
return $code;


}

?>

Upvotes: 0

Views: 28

Answers (1)

Jacob
Jacob

Reputation: 43299

SELECT COUNT(`url_id`) FROM `mini_shortener` WHERE     
 `code`='$code' LIMIT 1

INSERT INTO `mini_shortener` VALUES('', '$url', '$code')

Back ticks are for table/column names, not for literals. Enclose literals (like $code) in single/double quotes and use back ticks for column names. Or better, just don't use reserved words and you can remove the back ticks.

Also, if the first column of mini_shortener is a AUTO_INCREMENT primary key, just don't insert it and specify the columns you want to insert to like this:

INSERT INTO `mini_shortener` (`url`,`code`) VALUES('$url', '$code')

MySQL Doc about object names

Upvotes: 3

Related Questions