AisRuss
AisRuss

Reputation: 269

Website works fine online, but running on localhost displays undefined index and variable

having a panic here! my dissertation website works fine online, but an hour before submission i found out it doesnt work on a local host..two or 3 pages keep coming up with undefined index or variable. could anyone help? Here is a snipped of an error: Notice: Undefined index: tables in C:\Users..\Desktop\USBWebserver v8_en\root\SnysbArchive\Search.php on line 76

<?php
include 'UserFunction.php';
..html omitted..
//write connect function in here
include('Connect.php'); //Connects to database
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);

if (!$result) 
{
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
if (mysql_select_db($dbname, $conn))
{
?>
    <form method="post" action="SearchResults.php?go" id="searchform">
    In <select name="tables">
    <?php
    while ($row = mysql_fetch_row($result)) 
    {
    if ($row[0] != 'user_details' && $row[0] != 'request') 
    {
        echo '<option value="'.$row[0].'">'.$row[0].'</option>';
    }
    }
}
    ?>
    </select>
        Seach for: <input type="text" name="name"> 
    <input type="submit" name="submit" value="Search" />
</form>

<br>
<?php
$tbl=$_POST['tables'];
echo $tbl;
?>

it doesnt like this line $tbl=$_POST['tables']; thanks

Upvotes: 1

Views: 2742

Answers (2)

Chris
Chris

Reputation: 5605

You could always put

error_reporting(E_ALL ^ E_NOTICE);

In the top of the page, this will prevent PHP complaining about variables that are referenced but do not yet exist.

Better to do what @john conde suggested, my suggestion will remove the error but allow you to code as sloppily. Sloppy code isn't the best..

Upvotes: 3

John Conde
John Conde

Reputation: 219864

Your local server has error reporting set to be higher then on your production server. This is resulting in you getting error messages for "lesser" errors like notices that you won't get on your production server. That's a good thing as your production server shouldn't be showing error messages will may be helpful to hackers and your local server should be telling you everything you are doing wrong, big and little, so you can build your application properly before going live.

In your case $_POST['tables'] isn't set and is being reported by your local server. It's also being caught by your production server but because of your error reporting settings you aren't being notified (except probably in your error logs).

UPDATE

To fix your error, check to see if that value exists before assigning it to a variable:

$tbl = (isset($_POST['tables'])) ? $_POST['tables'] : ''; 

Upvotes: 4

Related Questions