2013-03-07 65 views
-1

好,另一個不確定的索引ID未定義指數:

我想在數據庫中更改選擇行,但到目前爲止,它似乎沒有工作,我只得到

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

我已經嘗試了一些修正,但沒有奏效。

<?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> 

var_dump給我int 0。我不確定它應該是什麼。

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>"; 
} 
?> 
+0

通知不能成爲您的更新無法正常工作的原因。嘗試在update.php中向您的瀏覽器回顯或打印查詢。然後在像PHPMyAdmin這樣的程序中運行這個查詢來查看究竟是什麼錯誤。 – Joey 2013-03-07 09:36:00

+0

您正在使用[an **過時的**數據庫API](http://stackoverflow.com/q/12859942/19068)並應使用[現代替換](http://php.net/manual/en/ mysqlinfo.api.choosing.php)。 – Quentin 2013-03-07 09:36:41

+1

爲什麼我們每天都有10次這樣的問題?請先閱讀錯誤消息。 – Peon 2013-03-07 09:36:51

回答

4

這是因爲$_GET['EierID']未設置。

試試這個:

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

在update.php也做同樣的事情:$ud_ID = isset($_POST["ID"])?$_POST["ID"]:"";

0

如果變量不存在,你會得到嘗試施放該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; 
} 
?>