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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.