我正在爲我的項目創建編輯用戶配置文件。我遇到了這個錯誤「 注意:未定義的索引:第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");
?>
在此先感謝。
請閱讀本頁右側的類似問題。 PS:盲目複製粘貼,不理解代碼是否會導致不好的結果 – zerkms
「第18行的EditProfile.php」,第18行的內容是什麼? –
@icktoofay它更多的是「想想你有什麼第18行,錯誤在那裏:) :) –