Hey i am building a URL shortener and I have managed to create random strings, but I can't figure out how to create unique custom URLs. I have gotten through the hard part in actually creating custom URLs, but I don't know how to query the database for matches.
here is my code
customercreate.phpI underlined the part that I was having difficulty with. I can't find out how to check for matches in the database. I know I have to query the row 'short' for matches.Code:<?php require("./db_config.php"); $url = $_REQUEST['url']; if(!preg_match("/^[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+$/i", $url)) { $html = "Error: invalid URL"; } else { $db = mysql_connect($host, $username, $password); $short = $_POST["custom_url"]; $match = mysql_query("SELECT * FROM url_redirects WHERE short='$short'") if ($match == $short) { die('Sorry that custom URL is taken already') } if(mysql_query("INSERT INTO `".$database."`.`url_redirects` (`short`, `url`) VALUES ('".$short."', '".$url."');", $db)) { $html = "Your short URL is<br />kyletest.info/".$short; } else { $html = "Error: cannot find database"; } mysql_close($db); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Cut your URLs</title> <link type="text/css" rel="stylesheet" href="./css/style.css" /> </head> <body> <div id="pagewrap"> <h1>shrt<span class="r">r</span>.me</h1> <div class="body"> <?= $html ?> <br /><br /> <span class="back"><a href="./">X</a></span> </div> </div> </body> </html>
Thanks for helping


Reply With Quote

Bookmarks