2012-05-03 65 views
-1

我正在爲我的項目創建編輯用戶配置文件。我遇到了這個錯誤「 注意:未定義的索引:第18行 的C:\ xampp \ htdocs \ HelloWorld \ EditProfile.php中的userid」。我花了一個小時試圖找出錯誤的原因,但我似乎無法找到它。我遵循這個指南這裏php -'Edit' function for forum posts and such這裏是我的代碼:未定義的索引:userid

EditProfile.php

<?php 
// connect to SQL 
$dbcnx = mysql_connect("localhost", "root", "2345fypj"); 
if (!$dbcnx) 
    { 
    echo("<P>Unable to connect to the database server at this time.</P>"); 
    exit(); 
} 

// connect to database 
$dbcon = mysql_select_db("my_db", $dbcnx); 
if (!$dbcon) { 
    echo("<P>Unable to locate DB table at this time.</P>"); 
    exit(); 
} 

//data preparation for the query 
$id = intval($_GET['userid']); 

// selects title and description fields from database 
$sql = "SELECT * FROM user_profile WHERE userid=$id"; 
$result = mysql_query($sql) or die(mysql_error());   
# retrieved by using $row['col_name'] 
$row = mysql_fetch_array($result); 

?> 

<h3>Edit</h3> 
<form action="save_edit.php" enctype="multipart/form-data" method="post" name="myForm" /> 
    <table> 
    <tr> 
     <td><b>Name</b></td> 
     <td><input type="text" size="70" maxlength="100" name="title" value="<?php echo $row['name'] ?>"></td> 
    </tr> 
    <tr> 
     <td><b>Age</b></td> 
     <td><input type="text" size="70" maxlength="100" name="title" value="<?php echo $row['age'] ?>"></td> 
    </tr> 
    </table> 
    <input type="hidden" name="id" value="<?php echo $id; ?>" /> 
    <input name="enter" type="submit" value="Edit"> 
</form> 

<?php 
mysql_close($dbcnx); 
?> 

save_edit.php

<?php 
// connect to SQL 
$con = mysql_connect("localhost", "root", "2345fypj"); 
if (!$con) { 
    echo("<P>Unable to connect to the database server at this time.</P>"); 
    exit(); 
} 
// connect to database 
$dbcon = @mysql_select_db("user_profile", $con); 
if (!$dbcon) { 
    echo("<P>Unable to locate DB table at this time.</P>"); 
    exit(); 
} 

#data preparation for the query 
$id = intval($_POST["userid"]); 
foreach ($_POST as $key => $value) $_POST[$key] = mysql_real_escape_string($value); 

$sql = "UPDATE user_profile SET 
     name='$_POST[name]', 
     age='$_POST[age]', 
     WHERE userid=$id"; 

if (!mysql_query($sql,$con)) { 
    die('Error: ' . mysql_error()); 
} 

mysql_close($con); 
header ("location: http://www.domain.com/url_to_go_to_after_update"); 
?> 

在此先感謝。

+1

請閱讀本頁右側的類似問題。 PS:盲目複製粘貼,不理解代碼是否會導致不好的結果 – zerkms

+0

「第18行的EditProfile.php」,第18行的內容是什麼? –

+0

@icktoofay它更多的是「想想你有什麼第18行,錯誤在那裏:) :) –

回答

0

問題是你正在嘗試使用變量$_GET['userid']而未定義。此變量引用URL中的GET參數(例如EditProfile.php?userid=42)。如果此參數未在URL中傳遞,您將收到此警告。您應該檢查是否存在變量:

if (!isset($_GET['userid'])) { 
    die("Parameter is missing!"); 
} 

$id = intval($_GET['userid']); 
+0

謝謝。我不知道我必須在網址中添加'userid = 3'。 –

1
$id = intval($_GET['userid']); 

它意味着設置$id變量的 「用戶id」 變量在您的網址。例如,如果您的網址是mySite.com?userid=12,$ id將被設置爲「12」。如果您的網址在其末尾部分沒有「用戶名= aValue」,則會看到您所看到的錯誤。 :)

你可以將其更改爲這對設置默認值:

$id = (isset($_GET['userid']) ? intval($_GET['userid']) : -1); 
+0

我試過你的解決方案,但「:」給出了語法錯誤。 –

+0

哎呀,拼寫錯誤。修正了 – DACrosby

+0

相信我,你真是太棒了...... – fresher

0

與您的代碼的問題是除了使用可變其使用之前,你繼續待辦事項查詢設置爲默認時值與intval(將始終返回atleast 0),如果傳遞一個非數字字符,你總是要更新表中的第0行用戶,你應該做的是不更新任何內容並返回錯誤。您在查詢中的age列之後還有一個胭脂蟲,

<?php 
if(isset($_POST["userid"]) && is_numeric($_POST["userid"])){ 

    $_POST = array_walk($_POST,'mysql_real_escape_string'); 

    $sql = "UPDATE `user_profile` SET `name`='{$_POST['name']}', `age`='{$_POST['age']}' 
      WHERE `userid` = {$_POST['userid']}"; 
    mysql_query($sql); 

}else{ 
    header('Location: ./failed'); 
} 
?>