我创建了一个存储用户详细信息的表
CREATE TABLE IF NOT EXISTS `users` (
`userid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`username` varchar(25) NOT NULL,
`password` varchar(25) NOT NULL,
PRIMARY KEY (`userid`),
KEY `userid` (`userid`),
KEY `userid_2` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;
这个是保留两个文本框中的名字和姓氏
CREATE TABLE IF NOT EXISTS `data` (
`dataid` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(25) NOT NULL,
`lastname` varchar(25) NOT NULL,
PRIMARY KEY (`surveyid`),
KEY `firstname` (`firstname`),
KEY `firstname_2` (`firstname`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
,此表将userid和dataid作为外键。
CREATE TABLE IF NOT EXISTS `jointable` (
`jointableid` int(11) NOT NULL AUTO_INCREMENT,
`dataid` int(11) NOT NULL,
`userid` int(11) NOT NULL,
PRIMARY KEY (`jointableid`),
KEY `surveyid` (`dataid`,`userid`),
KEY `userid` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
ALTER TABLE `jointable`
ADD CONSTRAINT `lookup_ibfk_2` FOREIGN KEY (`userid`) REFERENCES `users` (`userid`) ON DELETE CASCADE ON UPDATE NO ACTION,
ADD CONSTRAINT `lookup_ibfk_1` FOREIGN KEY (`dataid`) REFERENCES `data` (`dataid`) ON DELETE CASCADE ON UPDATE NO ACTION;
我插入数据的页面是
<?php
session_start();
if ($_SESSION['username'])
{
echo "Welcome, ".$_SESSION['username']."! <a href='logout.php'>Logout</a>";
}
else
die("You must be logged in!!");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src="question.js"></script>
<title>Questionnaire</title>
<style type="text/css">
span {color: #FF00CC}
</style>
</head>
<body background="images/good.jpg">
<h1></h1>
<form name="quiz" method="post" action="submitdata.php">
First Name: <input type="text" name="firstname" id="fname"/>
<p></p>
Last Name: <input type="text" name="lastname" id="lname"/>
<input type="submit" name="submitbutton" value="Go"></input>
<input type="reset" value="clear all"></input>
</form>
</body>
</html>
最后,submitdata.php是将数据存储在数据库中的页面
<?php
session_start();
if ($_SESSION['username'])
{
echo "Welcome, ".$_SESSION['username']."! <a href='logout.php'>Logout</a>";
}
else
die("You must be logged in!!");
$con=mysql_connect ("localhost","****","****");
mysql_select_db("project",$con);
@$firstname=$_POST['firstname'];
@$lastname=$_POST['lastname'];
$s="INSERT INTO data(`firstname`,`lastname`) VALUES ('$firstname','$lastname')";
echo "You have successfully submit your questions";
mysql_query ($s);
?>
此外,我有这个登录页面
<?php
session_start();
@$username = $_POST['username'];
@$password = $_POST['pass'];
if(@$_POST['Submit']){
if($username&&$password)
{
$connect = mysql_connect("localhost","****","****") or die("Cannot Connect");
mysql_select_db("project") or die("Cannot find the database");
$query = mysql_query("SELECT * FROM users WHERE username='$username'");
$numrows = mysql_num_rows($query);
if($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
if($username==$dbusername&&$password==$dbpassword)
{
echo "You are login!!!!! Continue now with the survey <a href='mainpage.php'>here</a>";
$_SESSION['username']=$username;
}
else
{
echo "<b>Incorrect Password!!!!</b>";
}
}
else
//die("That user does not exist");
echo "<b>That user does not exist</b>";
}
else
echo "<b>You must enter a username and a password</b>";
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login Page</h2>
<form name="loginform" method='POST'>
<fieldset>
<legend>Form</legend>
<label>Username: <input type="text" name="username"/><span>*</span></label><br/>
<label>Password: <input type="password" name="pass"/><span>*</span></label>
<input class="placeButtons" type="reset" value='Reset'/>
<input class="placeButtons" type="submit" name="Submit" value='Login'/>
<a href='registration.php'>Register</a>
</fieldset>
</form>
</body>
</html>
所以我的目标是登录或注册的用户,填写页面,然后同时将记录添加到数据(包含数据)和jointable(与记录的ID)。问题是我有注册和登录页面我可以插入用户数据,我也可以在DATA表中插入数据,但我没有连接表中的记录。为什么会这样?
答案 0 :(得分:1)
回答你的问题:“为什么会这样?”
因为您没有在 join_table 中插入任何数据,因此它不会为您执行此操作。
<强>解决方案:强>
在数据表中插入记录后,您需要返回名为 lastInsertId 的内容。这是PDO中的一个示例,我建议使用PDO:
//prepare statement
$stmt_data = $this->db->prepare('
INSERT INTO data(`firstname`,`lastname`)
VALUES (:fname, :lname)
');
//bind parameter/variables
$stmt_data->bindParam(':fname', $firstName, PDO::PARAM_STR);
$stmt_data->bindParam(':lname', $lastName, PDO::PARAM_STR);
//insert row
$res = $stmt_data->execute();
//get last isnerted ID
if ($res) {
$id = $this->db->lastInsertId();
//NOW INSERT INTO jointable with this id
$stmt_jointable = $this->db->prepare('
INSERT INTO join_table(`userid`, dataid`)
VALUES (:uid, :dataid)
');
$stmt_jointable->bindParam(':uid', $userID, PDO::PARAM_INT);
$stmt_jointable->bindParam(':dataid', $id, PDO::PARAM_INT);
//insert
$stmt->execute();
}
<强> TIPS:强>
jointable
,您不需要自动增量主键,您只需将用户ID和数据ID作为主键,因为同一用户不能拥有多个数据记录。不是1-1关系吗?