私は最初に2つのファイルedit.phpとupdate.phpedit.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>
今update.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をプルさせる方法
ここに私のログインコードがあります
<?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();
}
}
?>