0

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>
brunam
  • 825
  • 2
  • 14
  • 26

3 Answers3

1

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.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
  • This cleared that error up nicely, but like you mentioned, it's still not working. Instead of the expected "Added to db" line, I get a blank screen and no database entry added... I added the form code used in this tutorial below the php code above. – brunam Dec 14 '11 at 04:41
  • Are you setting the action via a GET argument or POST? If it's GET, then it should be pretty clear, but for a form, I'm not sure, you'll need to figure it out. I'd recommend picking one and then not using $_REQUEST and using $_GET or $_POST instead. – loganfsmyth Dec 14 '11 at 04:44
0

you can use $_POST["action"] if you are using POST method or $_GET["action"] for GET.

Kamran Ali
  • 5,904
  • 2
  • 26
  • 36
0

Try using

if(isset($_REQUEST['action']) && $_REQUEST['action'] == "add")
Steve
  • 1,371
  • 1
  • 16
  • 38