Search code examples
phpmysqlfacebookundefined-index

What's causing the mysql undefined index error with this tutorial script?


I'm running this php script at my database verbatim from a tutorial, yet I'm getting an undefined index error on line 27 (the bit that starts with if ($_REQUEST['action'] == "add") {)... I'm just learning mysql so any help would rock!

<?php
/**
 * Copyright 2011 Robert Turrall - robertturrall.com
 *
 * Part of the video2brain course: Facebook Application Development: Learn by Video
 *
 * 
 */

$dbhost     = 'localhost';      // This is the hostname/address for your database. Usually 127.0.0.1 for local testing, or 'localhost' is good
$dbuser     = 'root';       // Login name to your database - anonymous is default for localhost MySQL
$dbpass     = '';               // Login password to your database - default for local MySQL is no password
$data       = 'test';           // Database name

$db = mysql_connect($dbhost, $dbuser, $dbpass);

if (mysql_errno() > 0) {
    if (mysql_errno() == 1203) {
        // 1203 == ER_TOO_MANY_USER_CONNECTIONS (mysqld_error.h)
        die("DB error");
    } else {
        // other error
        die("DB error");
    }
}

if ($_REQUEST['action'] == "add") {
    mysql_select_db($data, $db);
    $Insert = "INSERT INTO FB_birthdays (UID, birthday) VALUES ('".$_REQUEST['uid']."', '".$_REQUEST['birthday']."')";
    $res = mysql_query($Insert, $db) or die ("Save error");
    mysql_close($db);
    echo "Added to DB!";
}

?>


<form action="savedb.php" method="post">
                <input type="hidden" name="request" value="add">
                <input type="hidden" name="uid" value="<?php echo $user; ?>">
                <input type="hidden" name="birthday" value="<?php echo $birthday; ?>">     
                <input name="Submit" type="submit" value="Add me!">
            </form>

Solution

  • Undefined index on that line means that the array $_REQUEST has no key 'action'.

    You'll want something like this:

    if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'add') {
    

    Obviously that means the rest of your code won't run, so something is still wrong with whatever is sending the request.