Forum Moderators: coopster

Message Too Old, No Replies

undefined index...

         

Flolondon

12:51 pm on Apr 14, 2005 (gmt 0)

10+ Year Member



<body>
<?
$name= $_POST['name'];
$birthday= $_POST['birthday'];

I keep getting errors saying undefined index. please help....

dreamcatcher

2:46 pm on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

You are trying to declare something that hasn`t previously been set. Try using the isset function:

if (isset($_POST['name']))
{
$name= $_POST['name'];
}
if (isset($_POST['birthday']))
{
$birthday= $_POST['birthday'];
}

Hope that helps.

dc

Flolondon

2:52 pm on Apr 14, 2005 (gmt 0)

10+ Year Member



<form method="POST" action="birthdays_insert_record.php">
<!--The hidden fields are provided to maintain state. They are used to pass the username and password from script to script.-->
<input type="hidden" name="username" value= "<?print $_POST['username']?>">
<input type="hidden" name="password" value= "<?print $_POST['password']?>">
<?

sorry, i should have put the whole codes. can you help me out with this one please...

dreamcatcher

3:45 pm on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The values are only necessary if for example you are doing error checking and you want the page to reload with the info that was typed in. In this instance the isset function and also a ternary operator will do nicely.

<input type="hidden" name="username" value= "<?print ((isset($_POST['username']))? $_POST['username'] : "")?>">
<input type="hidden" name="password" value= "<?print ((isset($_POST['password']))? $_POST['password'] : "")?>">

dc

Flolondon

5:29 pm on Apr 14, 2005 (gmt 0)

10+ Year Member



thanks it work. god bless

Flolondon

6:24 pm on Apr 14, 2005 (gmt 0)

10+ Year Member



mysql_query("DELETE FROM birthdays where id = $id")or die("Delete Error: ".mysql_error());

and now with this one, its saying undefined variable...

your help would be appreciated..of how to define it please. thanks.

mcibor

7:05 pm on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suppose you get your id by get or post:

if(isset($_POST["id"])) $id = (int)$_POST["id"];
elseif(isset($_GET["id"])) $id = (int)$_GET["id"l;
else $id = 0;//or without this line. Depends only on you. However if you don't have this line then before DELETE you should write:

if($id)
{
mysql_query...
}

(int)$ ... is to converge data from post to strict integer. This way nobody will be able to do sth badly to your db.

BTW "Is not defined" error means, that never in the script you wrote, what the $id exactly is.

best regards
Michal Cibor

Flolondon

7:25 pm on Apr 14, 2005 (gmt 0)

10+ Year Member



<html><head><title>Birthdays Delete Record</title></head>
<body>
<?
if (isset($_POST['username']))
{
$username= $_POST['username'];
}
if (isset($_POST['password']))
{
$password= $_POST['password'];
}
if (isset($_POST['id']))
{
$id= $_POST['id'];
}

$db="mydatabase";
$link = mysql_connect("localhost","root", "");
//$link = mysql_connect("localhost",$_POST['username'],$_POST['password']);
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
mysql_query("DELETE FROM birthdays where id = $id")or die("Delete Error: ".mysql_error());
mysql_close($link);
print "Record Removed.\n";
?>
<br><br>
<form method="POST" action="birthdays_delete_form.php">
<input type="hidden" name="username" value="<?php print $_POST['username']?>">
<input type="hidden" name="pass" value="<?php print $_POST['password']?>">
<input type="submit" value="Delete Another">
</form><br>
<form method="POST" action="birthdays_dbase_interface.php">
<input type="hidden" name="username" value="<?php print $_POST['username']?>">
<input type="hidden" name="pass" value="<?php print $_POST['password']?>">
<input type="submit" value="DBase Interface">
</form>
</body>
</html>

Please tell me what have i done wrong... it keeps saying undefined variable line 24...please tell...

mcibor

7:31 pm on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$id is not set, it's not in the POST: you don't have anything where you store the id, eg

<input type="hidden" name="id" value="<?php echo $id?>">

And I would still put:

or die("Couldn't open $db: ".mysql_error());
if($id)mysql_query("DELETE FROM birthdays where id = $id")or die("Delete Error: ".mysql_error());
mysql_close($link);

Best wishes
Michal Cibor

Flolondon

7:00 am on Apr 15, 2005 (gmt 0)

10+ Year Member



<html><head><title>Birthdays Delete Record</title></head>
<body>
<?
if (isset($_POST['username']))
{
$username= $_POST['username'];
}
if (isset($_POST['password']))
{
$password= $_POST['password'];
}
if (isset($_POST['id']))
{
$id= $_POST['id'];
}

$db="mydatabase";
$link = mysql_connect("localhost","root", "");
//$link = mysql_connect("localhost",$_POST['username'],$_POST['password']);
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
if($id)mysql_query("DELETE FROM birthdays where id = $id")or die("Delete Error: ".mysql_error());
mysql_close($link);
print "Record Removed.\n";
?>
<br><br>
<form method="POST" action="birthdays_delete_form.php">
<input type="hidden" name="id" value="<?php print $_POST ['id']?>">
<input type="hidden" name="username" value="<?php print $_POST['username']?>">
<input type="hidden" name="pass" value="<?php print $_POST['password']?>">
<input type="submit" value="Delete Another">
</form><br>
<form method="POST" action="birthdays_dbase_interface.php">
<input type="hidden" name="username" value="<?php print $_POST['username']?>">
<input type="hidden" name="pass" value="<?php print $_POST['password']?>">
<input type="submit" value="DBase Interface">
</form>
</body>
</html>

PLEASE.... i am still having problems. its staying undefined variables and undefined index... please help.

dreamcatcher

7:39 am on Apr 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change:


<form method="POST" action="birthdays_delete_form.php">
<input type="hidden" name="id" value="<?php print $_POST ['id']?>">
<input type="hidden" name="username" value="<?php print $_POST['username']?>">
<input type="hidden" name="pass" value="<?php print $_POST['password']?>">
<input type="submit" value="Delete Another">
</form><br>
<form method="POST" action="birthdays_dbase_interface.php">
<input type="hidden" name="username" value="<?php print $_POST['username']?>">
<input type="hidden" name="pass" value="<?php print $_POST['password']?>">
<input type="submit" value="DBase Interface">
</form>

to this:


<form method="POST" action="birthdays_delete_form.php">
<input type="hidden" name="id" value="<?php print ((isset($_POST['id']))? $_POST['id'] : "")?>">
<input type="hidden" name="username" value="<?php print ((isset($_POST['username']))? $_POST['username'] : "")?>">
<input type="hidden" name="pass" value="<?php print ((isset($_POST['password']))? $_POST['password'] : "")?>">
<input type="submit" value="Delete Another">
</form><br>
<form method="POST" action="birthdays_dbase_interface.php">
<input type="hidden" name="username" value="<?php print ((isset($_POST['username']))? $_POST['username'] : "")?>">
<input type="hidden" name="pass" value="<?php print ((isset($_POST['password']))? $_POST['password'] : "")?>">
<input type="submit" value="DBase Interface">
</form>

dc

Flolondon

8:17 am on Apr 15, 2005 (gmt 0)

10+ Year Member



ok thanks - lets see how it goes..