使用PHP在mySQL的多个表中输入表单中的值
我正在做一个注册表,我必须在mysql的3个表中输入信息。我试过几件事,但似乎做不好。这就是我目前所拥有的。我的登记表如下:使用PHP在mySQL的多个表中输入表单中的值,php,mysql,insert,Php,Mysql,Insert,我正在做一个注册表,我必须在mysql的3个表中输入信息。我试过几件事,但似乎做不好。这就是我目前所拥有的。我的登记表如下: function fix($str){ $str = trim($str); $str = stripslashes($str); return $str; } if($_POST['submit']) { $first = fix($_POST['firstName']); $last = fix($_PO
function fix($str){
$str = trim($str);
$str = stripslashes($str);
return $str;
}
if($_POST['submit'])
{
$first = fix($_POST['firstName']);
$last = fix($_POST['lastName']);
$email = fix($_POST['email']);
$userName = fix($_POST['userName']);
$passWord = fix($_POST['passWord']);
$reTyped = fix($_POST['confPassword']);
$secA = $_POST['secA'];
$secQ = $_POST['secQ'];
require_once('user_registration.php');
}
?>
<!DOCTYPE, Head, Body & Nav here>
<div class="account-container register">
<div class="content clearfix">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<h1>New User Registration</h1>
<div class="login-fields">
<div class="field">
<label for="firstName">First Name:</label>
<input type="text" id="firstName" name="firstName" placeholder="First Name" class="login" />
</div> <!-- /field -->
<div class="field">
<label for="lastName">Last Name:</label>
<input type="text" id="lastName" name="lastName" placeholder="Last Name" class="login" />
</div> <!-- /field -->
<div class="field">
<label for="userName">User Name:</label>
<input type="text" id="userName" name="userName" placeholder="User Name" class="login" />
</div><!-- /field -->
<div class="field">
<label for="email">Email Address:</label>
<input type="text" id="email" name="email" placeholder="Email" class="login" />
</div> <!-- /field -->
<div class="field">
<label for="passWord">Password:</label>
<input type="password" id="passWord" name="passWord" placeholder="Password" class="login" />
</div> <!-- /field -->
<div class="field">
<label for="confPassword">Confirm Password:</label>
<input type="password" id="confPassword" name="confPassword" placeholder="Confirm Password" class="login" />
</div><!--field-->
<div class="field">
<label for="secQ">Security Question</label>
<select name="secQ" id="secQuestion" placeholder="Security" class="dropdown">
<option value="No Selection"
<?php if(!$_POST || $_POST['secQ'] == 'No Selection')
{ echo 'selected'; }?>>--Select A Security Question--</option>
<option value="0"
<?php if(!$_POST || $_POST['secQ'] == '0')
{ echo 'selected'; }?>>What is your mothers maiden name</option>
<option value="1"
<?php if(!$_POST || $_POST['secQ'] == '1')
{ echo 'selected'; }?>>In what city were you born</option>
<option value="2"
<?php if(!$_POST || $_POST['secQ'] == '2')
{ echo 'selected'; }?>>What is your favorite color</option>
<option value="3"
<?php if(!$_POST || $_POST['secQ'] == '3')
{ echo 'selected'; }?>>What year did you graduate HighSchool</option>
<option value="4"
<?php if(!$_POST || $_POST['secQ'] == '4')
{ echo 'selected'; }?>>What is the name of your first boy/girl friend</option>
<option value="5"
<?php if(!$_POST || $_POST['secQ'] == '5')
{ echo 'selected'; }?>>What was the name of your first pet</option>
<option value="6"
<?php if(!$_POST || $_POST['secQ'] == '6')
{ echo 'selected'; }?>>What street did you grow up on</option>
</select>
</div>
<div class="field">
<label for="secA">Security Answer</label>
<input type="text" id="secA" name="secA" placeholder="Answer" class="login" />
</div>
</div> <!-- /login-fields -->
<div class="login-actions">
<span class="login-checkbox">
<input id="Field" name="terms" type="checkbox" class="field login-checkbox" value="First Choice" tabindex="4" />
<label class="choice" for="Field">Agree with the Terms & Conditions.</label>
</span>
<input type="submit" id="register" name="submit" class="button btn btn-primary btn-large" value="Register" />
</div> <!-- .actions -->
</form>
</div> <!-- /content -->
然后,我附加到表单的PHP页面(如第一条if语句中所述)是-user_registration.PHP:
<?php
require_once('includes/checkPassword.php');
$usernameMinChars = 6;
$errors = array();
if (strlen($userName) < $usernameMinChars)
{
$errors[] = "Username must be at least $usernameMinChars characters.";
}
if (preg_match('/\s/', $userName))
{
$errors[] = 'Username should not contain spaces.';
}
$checkPwd = new CheckPassword($passWord);
$checkPwd->requireMixedCase();
$passwordOK = $checkPwd->check();
if (!$passwordOK) {
$errors[] = array_merge($errors, $checkPwd->getErrors());
}
if ($passWord != $reTyped) {
$errors[] = "Your passwords don't match.";
}
if (!$errors) {
//include connection file
require('includes/usrConnect.php');
$conn = dbConnect('write');
//create salt using current timestamp
$salt = SALT .time();
//encrypt pwd with salt
$hashword = sha1($passWord . $salt);
//prepare sql statement
$sql = "INSERT INTO User (User_Name) VALUES('$userName')";
$sql .= "INSERT INTO User_Parameter (User_Identity, User_Password, User_Salt) VALUES('$User_Identity', '$hashword', '$salt')";
$sql .= "INSERT INTO User_Profile (User_Identity, Profile_FirstName, Profile_LastName, Profile_Email, Profile_Question, Profile_Answer)
VALUES('$User_Identity', '$first', '$last', '$email', '$secQ', '$secA')";
if($stmt = $conn->query($sql))
{
$User_Identity = $stmt->insert_id;
do{
if($result = $conn->affected_rows())
{
while ($result > 0)
{
$success = 'User successfully created '.$User_Identity;
header('Location: login.php');
}
} $result->free();
}while($conn->next_result());
}
elseif($stmt->errno == 1062)
{
$errors[] = "$userName already exists, Please select another username.";
}else{
$errors[] = 'Sorry, there was a problem processing your request.';
}
}
?>
问题是我有3个表要插入信息。用户名为useru,密码和salt为user\u参数,名称、电子邮件和安全问题/答案为user\u配置文件。
我就是不知道如何正确地进行查询。我试过这样做,也试过给每个查询命名不同的名称,但无法正确嵌套,以便从第一次插入中正确获取用户标识,以便在其他查询中输入信息。有人能帮忙吗?我认为您必须分别执行这3个查询,它实际上会起作用
$conn->query("INSERT INTO User (User_Name) VALUES('$userName')");
$conn->query("INSERT INTO User_Parameter (User_Identity, User_Password, User_Salt) VALUES('$User_Identity', '$hashword', '$salt')");
$stmt = $conn->query("INSERT INTO User_Profile (User_Identity, Profile_FirstName, Profile_LastName, Profile_Email, Profile_Question, Profile_Answer) VALUES('$User_Identity', '$first', '$last', '$email', '$secQ', '$secA')");
if ($stmt) {
// then your code goes
}
我想把这三张桌子合在一起就行了
使用一个insert查询Uff,为什么2014年的人们仍在编写类似2000年的代码?无论如何,问题的解决方案是分别运行每个查询。只有username列的表user是完全冗余的,因为user\u name列可能位于user\u profile表中,或者user\u profile中的列应该移动到user,而表user\u profile应该被删除。并与表user_标识合并—这样您就只有一个表要插入。我无法正确回答你的问题,因为你的数据库模型很可能是错误的。我是新的ro programming@shadyyx为什么像你这样没有建设性内容的巨魔仍然来到这四个人面前?因为像你这样有非建设性问题的巨魔仍然来这里问新手问题并偷走我们的时间。。。无论如何,我的评论是尽可能有建设性的——您的数据库模型不正确,我建议对其进行改进——将这三个表合并为一个表。如果我没有什么建设性的话要说,我就不会有现在的代表了。你让我开心