我试图设置一个外键,以便我可以将照片的作者与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;
这里是将照片和一些信息一起插入数据库的代码......
<?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 item has been listed.</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>
Last 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>
答案 0 :(得分:0)
在photo varchar(75)
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;