elite311 Posted December 4, 2011 Share Posted December 4, 2011 I'm trying to make what I thought was a simple redirect script but I keep getting error and was wondering if someone can tell me what I have do wrong? I have a database with 3 columns "id", "project", "url" I want to be able to type into a web form the name of the project I'm looking for and if it's found redirect the browser to that project I'm still learning this stuff and cant figure out what I have done wrong and any help would be greatly appreciated. <?php include("project_admin/includes/config.php"); $db = new Database($db_host, $db_username, $db_password, $db_database, $db_table_prefix); $db -> connect(); if ($_POST['findproject']{ $q = $db->mysql_query("SELECT * FROM projects WHERE project LIKE '".$_POST['project']."'"); $r = mysql_fetch_assoc($q); if (mysql_num_rows($q) >= 1){ header('Location:'.$r['url']); }else{ echo "Project Not Found"; } ?> Link to comment https://forums.phpfreaks.com/topic/252429-check-for-record-and-redirect-to-url/ Share on other sites More sharing options...
Pandemikk Posted December 4, 2011 Share Posted December 4, 2011 Be sure to escape $_POST['project'] before actually using this script (http://php.net/manual/en/function.mysql-real-escape-string.php). What exactly goes wrong in your script? Does it echo "Project not found"? In that case, a row was not found. You should be using LIMIT 1 since you're doing one redirect. And you should use die; after header to stop code processing. I don't recommend using * to select everything. Just select what you need. <?php include("project_admin/includes/config.php"); $db = new Database($db_host, $db_username, $db_password, $db_database, $db_table_prefix); $db -> connect(); if ($_POST['findproject']{ $q = $db->mysql_query("SELECT * FROM projects WHERE project LIKE '" . mysql_real_escape_string($_POST['project']) . "' LIMIT 1"); if (!$q){ echo "Project Not Found"; }else{ header('Location:' . $r['url']); die; } ?> Use die($_POST['project']); at the top of the script to make sure its value is correct. Link to comment https://forums.phpfreaks.com/topic/252429-check-for-record-and-redirect-to-url/#findComment-1294236 Share on other sites More sharing options...
Pikachu2000 Posted December 4, 2011 Share Posted December 4, 2011 You're missing a closing parenthesis in your if( conditional. Does that sound like it could be what the error (that you didn't bother to post) could be referring to? Link to comment https://forums.phpfreaks.com/topic/252429-check-for-record-and-redirect-to-url/#findComment-1294360 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.