如何在我的页面中的“我的帐户”链接下仅显示登录用户的信息?我尝试在两个表之间设置一个外键,因为我有一个用于登录的表,另一个用于插入照片、信息的东西,并将照片的作者与我在表中的用户名相关联users
。我的目标是只显示登录用户的用户信息和照片。
问题是当我尝试设置外键时,它设置了外键但数据没有进入表中。在我上传图片并插入一些信息的页面上,它显示上传成功,但是当我去查看我的数据库时,什么都没有。有什么帮助吗?我被困在这很长时间了。
这是我为创建表格所做的。
CREATE TABLE IF NOT EXISTS `users` (
`username` varchar(30) NOT NULL ,
`password` varchar(40) default NULL,
`usersalt` varchar(8) NOT NULL,
`userid` varchar(32) default NULL,
`userlevel` tinyint(1) unsigned NOT NULL,
`email` varchar(50) default NULL,
`timestamp` int(11) unsigned NOT NULL,
`actkey` varchar(35) NOT NULL,
`ip` varchar(15) NOT NULL,
`regdate` int(11) unsigned NOT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS photos (
ref int(10) unsigned NOT NULL auto_increment,
photo varchar(75),
Firstname varchar(35),
Lastname varchar(35),
Age INT(3),
author varchar(30) NOT NULL,
PRIMARY KEY (ref)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
这是我在创建表后尝试添加 FK 的内容。
ALTER TABLE photos
ADD CONSTRAINT FK_photos
FOREIGN KEY (author) REFERENCES users(username)
ON UPDATE CASCADE
ON DELETE CASCADE;
最后是我的 Upload.php
<?php
include("/include/session.php");
if(!$session->logged_in){ header("Location: ../main.php"); } else {
}
?>
<?php
$sub=0;
ini_set( "display_errors", 0);
if(isset($_REQUEST['submited'])) {
// your save code goes here
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2097152)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "";
if (file_exists("pictures/" . $_FILES["file"]["name"]))
{
echo "<font size='4' color='red'><b>We are sorry, the file you trying to upload already exists.</b></font>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"pictures/" . $_FILES["file"]["name"]);
$sub= 1;
$mysqli = new mysqli("localhost", "root", "", "secure_login");
// TODO - Check that connection was successful.
$photo= "pictures/" . $_FILES["file"]["name"];
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$age =$_POST["age"];
$stmt = $mysqli->prepare("INSERT INTO photos (photo, Firstname, Lastname, Age) VALUES (?, ?, ?, ?)");
// TODO check that $stmt creation succeeded
// "s" means the database expects a string
$stmt->bind_param("ssss", $photo, $fname, $lname, $age);
$stmt->execute();
$stmt->close();
$mysqli->close();
echo "<font size='7' color='white'><b> Success! Your Photo has been Uploaded.</b></font>";
echo '<meta http-equiv="refresh" content="2;url=home.php">';
}
}
}
else
{
echo "<font size='4' color='red'><b>We are sorry, the file you trying to upload is not an image or it exceeds 2MB in size.</b></font><br><font color='blue'><i>Only images under size of 2MB are allowed</i></font>.";
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="submited" value="true" />
<?php
ini_set( "display_errors", 0);
if($sub==0)
{
?>
<label for="file"><font size="5"><b>Choose Photo:</b></font></label>
<input id="shiny" type="file" name="file" onchange="file_selected = true;" required><br>
First Name:<input type="text" name="fname" value="<?php echo (isset($_POST['fname']) ? htmlspecialchars($_POST['fname']) : ''); ?>"required><br>
Last Name:<input type="text" name="lname" required><br>
Age:<input type="text" name="age" required><br>
<input id="shiny" type="submit" value="Submit" name="submit">
<?php
}
?>
</form>
</div>
这是 main.php 页面,其中显示用户登录时的用户名
if($session->logged_in){
echo "<h1>Logged In</h1>";
echo "<table border='1' width='100%' bgcolor='red'>";
echo "<tr>";
echo "<td>";
echo "Welcome <b>$session->username</b>, you are logged in. <br><br>"
."[<a href=\"userinfo.php?user=$session->username\">My Account</a>] "
."[<a href=\"useredit.php\">Edit Account</a>] ";
echo "[<a href=\"upload.php\">Upload</a>]";
echo "[<a href=\"test2.php\">My Uploads</a>]";
if($session->isAdmin()){
echo "[<a href=\"admin/index.php\">Admin Center</a>] ";
}
echo "[<a href=\"process.php\">Logout</a>]";
}
else{
?>