-1

我首先有 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,如果该行是不在那里它根本不起作用它不会更新任何信息我如何使它从当前登录的用户中提取ID

这是我的登录代码

<?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();
}
}

?>
4

1 回答 1

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 
于 2012-11-21T10:08:15.263 回答