0

Well, yet another undefined index appears :

I am trying to change a select row in a database, but so far it doesn't seem to work, I only get

Notice: Undefined index: EierID in C:\WampServer\www\Hundeklubben\ChangeO.php on line 19.

I have tried some fixes, but none worked.

<?php require_once('Connections/hundeklubb.php'); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Endring av eier</title>
<link rel="stylesheet" href="index.css" />
</head>

<body>

<?php
if(isset($_GET['EierID'])){ $name = $_GET['EierID']; } 
//Tried with both $_GET and $_POST
?>

<?php
$UID = (int)$_GET['EierID'];
$query = mysql_query("SELECT * FROM eiere WHERE EierID = '$UID'") or die(mysql_error());

if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
    $navn = $row['Navn'];
    $bosted = $row['Bosted'];
}
?>

<form name="form1" action="update.php" method="POST" id="form1">
<input type="hidden" name="ID" value="<?=$UID;?>">
Navn: <input type="text" name="ud_navn" value="<?=$navn?>"><br>
Bosted: <input type="text" name="ud_bosted" value="<?=$bosted?>"><br>
<input type="Submit" value="Oppdater">
</form>
<?php
}else{
echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
}
?>

<?php var_dump($UID); ?> 

</body>
</html>

The var_dump gives me int 0. I'm not sure what it is supposed to be.

update.php

<?php require_once('Connections/hundeklubb.php'); ?>
<?php
$ud_ID = (int)$_POST["ID"];

$ud_navn = mysql_real_escape_string($_POST["ud_navn"]);
$ud_bosted = mysql_real_escape_string($_POST["ud_bosted"]);


$query="UPDATE eiere
        SET navn = '$ud_navn', bosted = '$ud_bosted' 
        WHERE ID='$ud_ID'";


mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=1){
echo "<p>($ud_ID) Record Updated<p>";
}else{
echo "<p>($ud_ID) Not Updated<p>";
}
?>
levelnis
  • 7,665
  • 6
  • 37
  • 61
The Last Melody
  • 165
  • 1
  • 3
  • 10
  • The notice cant be the reason that your update isnt working. Try to echo or print the query in update.php to your browser. Then run this query in a program like PHPMyAdmin to see exactly whats wrong. – Joey Mar 07 '13 at 09:36
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Mar 07 '13 at 09:36
  • 1
    Why do we get exactly this question 10 times a day every day? Just read the error message first. – Peon Mar 07 '13 at 09:36
  • Great, XSS and SQL-injection at the same time =/ – Ja͢ck Mar 07 '13 at 09:52
  • Well excuse me, but I have read the error message, I have tried to fix it, and that only caused more errors, thats why you get so many in a day, Dainis – The Last Melody Mar 14 '13 at 08:18
  • I am giving up, someone close this, too many errors for different reasons and when I remove the ... Computers f***s logic... – The Last Melody Mar 14 '13 at 08:36
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Jocelyn Mar 29 '13 at 11:59

2 Answers2

4

It is because $_GET['EierID'] is not set.

Try this :

$UID = isset($_GET['EierID'])?$_GET['EierID']:"";

In update.php also do the same thing : $ud_ID = isset($_POST["ID"])?$_POST["ID"]:"";

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
0

If your variable doesn't exist you will get an error trying to cast that int.

<?php
if(isset($_GET['EierID'])){ 
    $name = $_GET['EierID']; 
    $UID = (int)$_GET['EierID'];
}else{
    //set to 0 or any default value you want to set when EierID doesn't exists
    $UID = 0; 
} 
?>
Alvaro
  • 40,778
  • 30
  • 164
  • 336