我创建了一个存储用户详细信息的表
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>
所以我的目标是登录或注册,填写页面然后将记录添加到数据(带有数据)和可连接(带有记录的ID)的用户。问题在于我拥有的注册和登录页面,我可以插入用户数据,也可以在 DATA 表中插入数据,但我没有可连接的记录。为什么会这样?