Jump to content

Check for record and redirect to url


elite311

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.