嘿,我是HTML和PHP的新手,我已经成功完成了我想要实现的目标,但有一件事情出错了:案例是: 请记住,这不是作业,但我正在努力学习!
我有一个mytable.php文件,我必须将记录显示到从mysql db获取的表中
然后我有一个编辑按钮,以便我可以编辑我的详细信息
按编辑按钮我转到edit.php,我必须在mysqli查询中编辑记录数据,其中某些条件应来自mytable.php 我的代码是:
\ mytable.php //
session_start();
<!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>Q4Part a</title>
</head>
<body>
//and for mysqli_connection string will be
$con=mysqli_connect('localhost','root','','db');
if($con){
$result=mysqli_query($con,"select * from users");
if($result){
echo "<table>";
echo "<tr>";
echo "<td>";
echo "Username";
echo "</td>";
echo "<td>";
echo "Password";
echo "</td>";
/*echo "<td>";
echo "Pass";
echo "</td>";
echo "<td>";
echo "Phone";
echo "</td>";
echo "<td>";
echo "Address";
echo "</td>";
echo "<td>";
echo "Action";
echo "</td>"; */
echo "</tr>";
while($row=mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>";
echo $row['myusername'];
echo "</td>";
echo "<td>";
echo $row['mypassword'];
echo "</td>";
echo "<td>";
echo "<a href='edit.php'>edit</a>";
$_SESSION['username']=$row['myusername'];
//to go to the edit.php
echo "</td>";
echo "</tr>";
}
echo "</table>";
}else{
echo mysqli_error($con);
//so that if query won't run!
}
}
else{
echo mysqli_connect_error($con);
//or you can use exit that shows an error message and terminates the current script
//and or die which is a substitue for exit function and also shows the error message and terminates the current script
//so
exit("Connection error!");
//we can also use die(string);
die("Connection error");
}
</body>
</html>
\ edit.php //
session_start();
$username=$_SESSION['username'];
<!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>Edit File</title>
</head>
<body>
<?php
$con=mysqli_connect('localhost','root','','db');
if($con){
$result=mysqli_query($con,"select * from users where myusername='".$username."'");
//where $username contains the current user that has been already signed in in the current time!//
if($result){
echo "<form action='save_edit.php' method='post'>";
echo "<table>";
echo "<th colspan='5'>";
echo "edit your details!";
echo "</th>";
while($row=mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>";
echo "<input type='text' name='username' value='".$row['myusername']."'>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='email' value='".$row['mypassword']."'>";
echo "</td>";
echo "<td>";
echo "<button type='submit'>Edit Changes</button>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
}else{
echo mysqli_error($con);
}
}
else{
echo mysqli_connect_error($con);
}
?>
</body>
</html>
所有工作都很好,但只有当我点击“编辑”链接时才会得到最后的结果
答案 0 :(得分:0)
尝试使用mysqli_fetch_assoc()而不是mysqli_fetch_array()。您正在使用散列图(关联数组),因此请确保相应地访问它。尝试在while语句后执行var_dump($row);
以从阵列中打印对象。
最后,您绝不应将密码以纯文本形式存储在数据库中。密码应存储为哈希值,因此您永远无法再次查看密码。当用户登录时,通过重新散列并比较匹配来检查传递是否有效。例如:$ pass = md5($_POST['password'])
您还应该考虑使用HTML5。您的doctype使您使用HTML4,并没有太多理由再这样做了。我建议更改为HTML5设置。
答案 1 :(得分:0)
$_SESSION['username']
将存储您从数据库/页面上显示的最后一个用户名,因此无法正常工作。
执行此操作的标准方法是为数据库中的每一行使用唯一标识符,并将其作为URL参数传递给edit.php。
如果用户名始终是唯一的,您可以执行以下操作。
在mytable.php中:
//echo "<a href='edit.php'>edit</a>";
echo "<a href='edit.php?username=" . urlencode($row['myusername']) . "'>edit</a>";
//$_SESSION['username']=$row['myusername'];
在edit.php中:
//$username=$_SESSION['username'];
$username = $_GET['username'];