Having trouble with PHP script that searches a database... Please help.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jpizzolato
    New Member
    • Feb 2015
    • 8

    Having trouble with PHP script that searches a database... Please help.

    Here is my form:

    Code:
     
    <form name="zipcode_search" id="form" class="form" method="post" action="process.php">
    <input name="zip" id="zip" type="text" placeholder="Zip Code" maxlength="5"/>
    <button name="submit" id="search" type="submit"> Search </button>
    
    
    </form>
    and script

    Code:
    <?php
    $host="localhost";
    $base="mydb";  
    $user="myusername";
    $pass="mypassword"; 
    
    $connect=mysql_connect($host, $user, $pass) or die("Could not connect to database!!!!");
    mysql_select_db($base,$connect) or die("Count not find database table!");
    
    if(isset($_POST['zip'])){
    	$zip=htmlspecialchars(mysql_real_escape_string($_POST['zip']));
    	$res=mysql_fetch_assoc(mysql_query("SELECT url FROM redirect WHERE zip='$zip'"));
    	header('Location: '.$res['url']);
    	exit;
    }
    
    else{ 
    	header('Location: http://www.bettercabletv.com/not-listed.html');
    }
    ?>
    I get these errors

    Warning: mysql_fetch_ass oc() expects parameter 1 to be resource, boolean given in /home3/spizz/public_html/bettercabletv.c om/process.php on line 12

    Warning: Cannot modify header information - headers already sent by (output started at /home3/spizz/public_html/bettercabletv.c om/process.php:12) in /home3/spizz/public_html/bettercabletv.c om/process.php on line 13

    Any pointers?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I don't see anything that jumps out at me as wrong with the code. But I would follow the template in the PHP manual just to make sure every error is covered. http://php.net/manual/en/function.mysql-fetch-assoc.php

    The header information error is caused by something being output to the client before the header. In this case, it is probably because the SQL error is output before the header. It should go away once you figure out the SQL error.

    Comment

    • Jpizzolato
      New Member
      • Feb 2015
      • 8

      #3
      Rabbit - thanks for your assistance. I figured out what the issue was and it was simply just a letter typed wrong in the table. Thanks for your help.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        No problem

        Comment

        • Jpizzolato
          New Member
          • Feb 2015
          • 8

          #5
          Rabbit - would there be any reason that above script isn't going to the else function when say I type a zip code that is not on the list?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            We try to limit it to one question per thread so it makes it easier for other people to find answers. I assume the new thread you made is for this latest issue. If not, please create a new one for this new question.

            Comment

            Working...