我有2个文件edit.php和update.php edit.php第一个
<?php
session_start();
$_SESSION['id'] = '9';
$id = $_SESSION["id"];
$username = $_POST["username"];
$fname = $_POST["fname"];
$password = $_POST["password"];
$email = $_POST["email"];
mysql_connect('mysql13.000webhost.com', 'a2670376_Users', 'PASSWORD') or die(mysql_error());
echo "MySQL Connection Established! <br>";
mysql_select_db("a2670376_Pass") or die(mysql_error());
echo "Database Found!<br>";
$query = "UPDATE members SET username = '$username', fname = '$fname',
password = '$password' WHERE id = '$id'";
$res = mysql_query($query);
if ($res)
echo "<p>Record Updated $rows[id]<p>";
else
echo "Problem updating record MySQL Error: " . mysql_error();
?>
<form action="update.php" method="post">
<input type="hidden" name="id" value="<?=$id;?>"/>
ScreenName:<br> <input type='text' name='username' id='username' maxlength='25' style='width:247px' name="username" value="<?=$username;?>"/><br>
FullName:<br> <input type='text' name='fname' id='fname' maxlength='20' style='width:248px' name="fname" value="<?=$fname;?>"/><br>
Email:<br> <input type='text' name='email' id='email' maxlength='50' style='width:250px' name="email" value="<?=$email;?>"/><br>
Password:<br> <input type='text' name='password' id='password' maxlength='25' style='width:251px' value="<?=$password;?>"/><br>
<input type="Submit">
</form>
现在更新.php
<?php
session_start();
mysql_connect('mysql13.000webhost.com', 'a2670376_Users', 'PASSWORD') or die(mysql_error());
mysql_select_db("a2670376_Pass") or die(mysql_error());
$id = (int)$_SESSION["id"] = $_SESSION['id'];
$username = mysql_real_escape_string($_POST["username"]);
$fname = mysql_real_escape_string($_POST["fname"]);
$email = mysql_real_escape_string($_POST["email"]);
$password = mysql_real_escape_string($_POST["password"]);
$query="UPDATE members
SET username = '$username', fname = '$fname', email = '$email', password = '$password'
WHERE id = '$id'";
mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=1){
echo "<p>($id) Record Updated<p>";
}else{
echo "<p>($id) Not Updated $_SESSION[username]<p>";
}
?>
在edit.php中提到我的问题$_SESSION['id'] = '9';
现在,如果该行没有说'9',它将不会更新具有id 9的用户,即我唯一的用户我在数据库中的id是9,如果该行不存在则根本不会更新它不会更新任何一个信息我如何从当前用户登录
继承我的登录代码
<?php
session_start();
// Check if he wants to login:
if (!empty($_POST[username]))
{
require_once("connect.php");
// Check if he has the right info.
$query = mysql_query("SELECT * FROM members
WHERE username = '$_POST[username]'
AND password = '$_POST[password]'")
or die ("<center>Error - Couldn't login user!!");
$row = mysql_fetch_array($query)
or die ("<center>Error - Couldn't login user!!");
if (!empty($row[username])) // he got it.
{
$_SESSION[username] = $row[username];
echo "<center>Welcome $_POST[username]! You've been successfully logged in. <br><h2 style='color: Yellow;'>REDIRECTING.....</H2><br><img src='http://i1261.photobucket.com/albums/ii586/jacob1899/ajax-loader-1.gif'></center> <meta http-equiv='REFRESH' content='1;url=/index.htm'>";
exit();
}
else // bad info.
{
echo "<center>Error - Couldn't login user!!";
exit();
}
}
?>
答案 0 :(得分:0)
将以下行写入您的登录文件
$_SESSION['id'] = $row[id];
以下是完整的代码段
if (!empty($row[username])) // he got it.
{
$_SESSION[username] = $row[username];
$_SESSION['id'] = $row[id];
echo "<center>Welcome $_POST[username]! You've been successfully logged in. <br><h2 style='color: Yellow;'>REDIRECTING.....</H2><br><img src='http://i1261.photobucket.com/albums/ii586/jacob1899/ajax-loader-1.gif'></center> <meta http-equiv='REFRESH' content='1;url=/index.htm'>";
exit();
}
else // bad info.
{
echo "<center>Error - Couldn't login user!!";
exit();
}
现在将以下行注释到编辑文件中。
//$_SESSION['id'] = '9'; from your edit.php