0

我创建了一个存储用户详细信息的表

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 表中插入数据,但我没有可连接的记录。为什么会这样?

4

1 回答 1

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();
}

提示:

  1. 摆脱 mysql_* 函数并继续使用PDOMySQLi
  2. 对于jointable不需要自增主键的用户,只需将用户 ID 和数据 ID 作为主键即可,因为同一用户不能有多个数据记录。不是1-1的关系吗?
于 2013-08-09T18:48:02.063 回答