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/
<?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>
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.
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!
<?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>