Page 1 of 1

Help with undefined index: Errors

Posted: Fri Nov 02, 2012 4:38 pm
by Mardonis
How's it going guys? I was looking for tutorials on the net and came across this one below. I have everything running properly sort of and can't seem to figure out and racking my brain with the Undefined index: name_list error on line 24. :?: I will try and attach the code. Also in the Change the money text box there is a MySQL error that says Undefined variable: select_money on line 70. :?: I am thinking there is a "checking" situation i am missing that I am just not seeing. I was reading about this type of error and that the error is not really a problem but id like to figure it out. Please help!!! P.S here is the original place where i found the tutorial in case others would like to see it. http://www.dreamincode.net/forums/topic ... on-part-1/

Code: Select all

<?php include_once("connect.php"); ?>  
<html>  
<head>  
<title>TestPage2</title>  
</head>
  
<body>
  
<?php   
  if (isset($_POST['submit']))
  {  
    $sql = "INSERT INTO users SET id = '', name = '".$_POST['name']."'";  
    $res = mysql_query($sql);  
    echo "".$_POST['name']." ADDED";  
  }  
  
  // the line below is just selecting data from users table and adding it to result  
  $result = mysql_query("SELECT name, id, money FROM users ORDER BY id DESC") or die(mysql_error());  
  
  // this is assigning some html for the drop down menu along with the some php and the data from result  
  $member_list = "";  
  while($row = mysql_fetch_array($result))
  {  
    if($row['name'] == $_POST['name_list'])
    {  
      $member_list .= "<option selected =\"selected\"value=\"".$row['name']."\">".$row['name']."</option>";  
    }
    else
    {  
      $member_list .= "<option value=\"".$row['name']."\">".$row['name']."</option>";       
    }  
  }
  
  // the following below happend when you hit search and what it does is select data on the name you clicked on from the menu.  
  if(isset($_POST['select']) or isset($_POST['update']))
  {  
    $sql = "SELECT * FROM users WHERE name = '".mysql_real_escape_string($_POST['name_list'])."'";  
    $query = mysql_query($sql) or die(mysql_error());  
    $row = mysql_fetch_array($query);  
    $select_money = htmlspecialchars($row->money);  
  }  
  
  // Updating the database with the information entered.  
  if(isset($_POST['update']))
  {  
    $result = mysql_query("UPDATE users SET money = '".mysql_real_escape_string($_POST['money'])."' WHERE name = '" .mysql_real_escape_string($_POST['name_list']). "'") or die(mysql_error());    
    echo "".$_POST['name_list']."'s money have been changed.";  
  }  
?>
  
<form method="post">  
  <p>  
    <center>  
      <p>ENTER NAME<br />  
        <input type="text" name="name" id="name" />  
        <br />  
        <input type="submit" name="submit" id="submit" value="Submit" />  
      </p>  
      <p><br />  
        <span>  
          Select a Name  
          <select name="name_list" class="textbox" id="name_list">  
          <option value="<?php echo htmlspecialchars($_GET['user']); ?>">Select</option>  
          <?php echo $member_list; ?>  
          </select>  
        </span>  
        <input type="submit" name="select" id="select" onfocus="if(this.blur)this.blur()" value="Select">  
        <br />  
        Change the Money  
        <input type="text" name="money" id="money" value="<?php echo $select_money; ?>">  
        <br />  
        <input type="submit" name="update" id="update" value="Update">  
        <br />  
      </p>  
       <p> </p>  
      <table width="50%" border="2" cellspacing="1" cellpadding="0">  
        <tr align="center">  
          <td colspan="3">Users Information</td>  
        </tr>  
        <tr align="center">  
          <td>ID</td>  
          <td>Name</td>  
          <td>Money</td>  
        </tr>  
      <?php   
        $result = mysql_query("SELECT * FROM users") or die(mysql_error());  
        // keeps getting the next row until there are no more to get  
        while($row = mysql_fetch_array($result))
        {  
          // print out the contents of each row into a table?>  
          <tr>  
            <td><label for="<?php echo $row['id']; ?>">
                <?php  
                  $name2=$row['id'];  
                  echo "$name2";
                ?>  
            </label></td>  
            <td><?php echo $row['name'] ?></td>  
            <td><?php echo $row['money'] ?></td>  
          </tr>  
        <?php } ?>  
      </table>  
      <p> </p>  
    </center>  
  </p>  
</form>
</body>  
</html>

Re: Help with undefined index: Errors

Posted: Fri Nov 02, 2012 11:28 pm
by Jackolantern
If you are missing a $_POST variable, we need to see the form that was on the previous page, since that is how the $_PAGE super variable is filled :)

Re: Help with undefined index: Errors

Posted: Mon Nov 05, 2012 2:19 pm
by Mardonis
Here is a image of what the form looks like if that's what you are needing to view. The errors show up as soon as you just open test.php.
Undefined index errors.png

Re: Help with undefined index: Errors

Posted: Mon Nov 05, 2012 7:01 pm
by brockfanning
Like Jackolantern said, it does seem to be a problem with a missing $_POST['name_list'] variable. To try and debug it, you could put something like:

Code: Select all

print_r($_POST);
Which should print the contents of the $_POST variable. It can be difficult to read in the browser, but if you click "View Source" it's formatted better.

Re: Help with undefined index: Errors

Posted: Tue Nov 06, 2012 12:20 am
by Jackolantern
Lets starts with the form that leads to the page you posted above, and then work back from there as needed :) If you could post the source to that form, we will take a look!

Re: Help with undefined index: Errors

Posted: Tue Nov 06, 2012 7:14 am
by Winawer
Try

Code: Select all

<?php include_once("connect.php"); ?>  
<html>  
<head>  
<title>TestPage2</title>  
</head>
  
<body>
  
<?php   
  if (isset($_POST['submit']))
  {  
    $sql = "INSERT INTO users SET id = '', name = '".$_POST['name']."'";  
    $res = mysql_query($sql);  
    echo "".$_POST['name']." ADDED";  
  }  
  
  // the line below is just selecting data from users table and adding it to result  
  $result = mysql_query("SELECT name, id, money FROM users ORDER BY id DESC") or die(mysql_error());  
  
  // this is assigning some html for the drop down menu along with the some php and the data from result  
  $member_list = "";  
  while($row = mysql_fetch_array($result))
  {  
    if(isset($_POST['name_list']) && $row['name'] == $_POST['name_list'])
    {  
      $member_list .= "<option selected =\"selected\"value=\"".$row['name']."\">".$row['name']."</option>";  
    }
    else
    {  
      $member_list .= "<option value=\"".$row['name']."\">".$row['name']."</option>";       
    }  
  }
  
  // the following below happend when you hit search and what it does is select data on the name you clicked on from the menu.  
  $select_money = 0;
  if(isset($_POST['select']) or isset($_POST['update']))
  {  
    $sql = "SELECT * FROM users WHERE name = '".mysql_real_escape_string($_POST['name_list'])."'";  
    $query = mysql_query($sql) or die(mysql_error());  
    $row = mysql_fetch_array($query);  
    $select_money = htmlspecialchars($row->money);  
  }  
  
  // Updating the database with the information entered.  
  if(isset($_POST['update']))
  {  
    $result = mysql_query("UPDATE users SET money = '".mysql_real_escape_string($_POST['money'])."' WHERE name = '" .mysql_real_escape_string($_POST['name_list']). "'") or die(mysql_error());    
    echo "".$_POST['name_list']."'s money have been changed.";  
  }  
?>
  
<form method="post">  
  <p>  
    <center>  
      <p>ENTER NAME<br />  
        <input type="text" name="name" id="name" />  
        <br />  
        <input type="submit" name="submit" id="submit" value="Submit" />  
      </p>  
      <p><br />  
        <span>  
          Select a Name  
          <select name="name_list" class="textbox" id="name_list">  
          <option value="<?php echo htmlspecialchars($_GET['user']); ?>">Select</option>  
          <?php echo $member_list; ?>  
          </select>  
        </span>  
        <input type="submit" name="select" id="select" onfocus="if(this.blur)this.blur()" value="Select">  
        <br />  
        Change the Money  
        <input type="text" name="money" id="money" value="<?php echo $select_money; ?>">  
        <br />  
        <input type="submit" name="update" id="update" value="Update">  
        <br />  
      </p>  
       <p> </p>  
      <table width="50%" border="2" cellspacing="1" cellpadding="0">  
        <tr align="center">  
          <td colspan="3">Users Information</td>  
        </tr>  
        <tr align="center">  
          <td>ID</td>  
          <td>Name</td>  
          <td>Money</td>  
        </tr>  
      <?php   
        $result = mysql_query("SELECT * FROM users") or die(mysql_error());  
        // keeps getting the next row until there are no more to get  
        while($row = mysql_fetch_array($result))
        {  
          // print out the contents of each row into a table?>  
          <tr>  
            <td><label for="<?php echo $row['id']; ?>">
                <?php  
                  $name2=$row['id'];  
                  echo "$name2";
                ?>  
            </label></td>  
            <td><?php echo $row['name'] ?></td>  
            <td><?php echo $row['money'] ?></td>  
          </tr>  
        <?php } ?>  
      </table>  
      <p> </p>  
    </center>  
  </p>  
</form>
</body>  
</html>

Re: Help with undefined index: Errors

Posted: Wed Nov 07, 2012 4:12 pm
by Mardonis
Thank you for the help. Looks like its working. :D