使用外键将值插入DB

时间:2015-09-22 17:15:34

标签: php mysql foreign-keys registration foreign-key-relationship

我对你的问题很简单...... :-) 我想将数据插入到数据库中,该数据库有2个表,一个是用户,第二个是userprofile

这是我的db:

streams

如您所见,我有外键用户 - > id with userprofile-> userid 问题是,当我想要注册新用户时,我不能.... 但是当我没有外键时,那一切都还可以。 但我需要注册外键她是register.php代码希望你能骗我:-) 并且请求告诉我代码的例子是我犯了错误。

<?php
session_start();
if (isset($_SESSION['user']) != "") {
    header("Location: home.php");
}
include_once 'dbconnect.php';

if (isset($_POST['btn-signup'])) {

    $y = $_POST['yer'];
    $m = $_POST['month'];
    $d = $_POST['day'];
    $dob = $d.'-'.$m.'-'.$y;

    $uname = mysql_real_escape_string($_POST['uname']);
    $upass = md5(mysql_real_escape_string($_POST['pass']));

    $email = mysql_real_escape_string($_POST['email']);
    $fname = mysql_real_escape_string($_POST['fname']);
    $lname = mysql_real_escape_string($_POST['lname']);
    $country = mysql_real_escape_string($_POST['country']);
    $height = mysql_real_escape_string($_POST['height']);
    $hair_color = mysql_real_escape_string($_POST['hair_color']);
    $eye_color = mysql_real_escape_string($_POST['eye_color']);
    $Body_type = mysql_real_escape_string($_POST['Body_type']);
    $gend = mysql_real_escape_string($_POST['gend']);

    if (mysql_query("INSERT INTO users(Username,Pasword) VALUES('$uname','$upass')") &&
        mysql_query("INSERT INTO userprofile(gender, dateofbirth, firstname, Lastname, height, eyecolor, haircolor, bodytype, country_town, email) VALUES ('$gend','$dob' ,'$fname','$lname','$height','$eye_color','$hair_color','$Body_type','$country','$email')")) {
?>

        <script>alert('successfully registered '); </script>

                    <?php
        $_SESSION['user'] = $row['id'];
        header("Location: Index.php"); ?>



        <?php
        $_SESSION['user'] = $row['id'];
        header("Location: Index.php");
    }
    else {
?>
        <script>alert('error while registering you...');</script>
        <?php
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Login & Registration System</title>
    <link rel="stylesheet" href="style.css" type="text/css" />

</head>
<body>
<center>
    <div id="login-form">

        <form method="post">
            <table align="center" width="30%" border="0">
                <tr>
                    <td>
                        <select name="gend">
                            <option value="Male">Male</option>
                            <option value="Female">Female</option>
                        </select></td>
                </tr>
                <tr>
                    <td>
                        <select name="day">
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
                            <option value="6">6</option>
                            <option value="7">7</option>
                            <option value="8">8</option>
                            <option value="9">9</option>
                            <option value="10">10</option>
                            <option value="11">11</option>
                            <option value="12">12</option>
                            <option value="13">13</option>
                            <option value="14">15</option>
                            <option value="16">16</option>
                            <option value="17">17</option>
                            <option value="18">18</option>
                            <option value="19">19</option>
                            <option value="20">20</option>
                            <option value="21">21</option>
                            <option value="22">22</option>
                            <option value="23">23</option>
                            <option value="24">24</option>
                            <option value="25">25</option>
                            <option value="26">26</option>
                            <option value="27">27</option>
                            <option value="28">28</option>
                            <option value="29">29</option>
                            <option value="30">30</option>
                            <option value="31">31</option>
                        </select>


                        <select name="month">
                            <option value="1">January</option>
                            <option value="2">February</option>
                            <option value="3">March</option>
                            <option value="4">April</option>
                            <option value="5">May</option>
                            <option value="6">June</option>
                            <option value="7">July</option>
                            <option value="8">August</option>
                            <option value="9">September</option>
                            <option value="10">October</option>
                            <option value="11">November</option>
                            <option value="12">December</option>

                        </select>
                        <input class="yer" type="text" name="yer" placeholder=" yer" required />
                    </td>
                </tr>
                <tr>
                    <td><input type="text" name="uname" placeholder="User Name" required /></td>
                </tr>
                <tr>
                    <td><input type="text" name="email" placeholder="Your Email" required /></td>
                </tr>
                <tr>
                    <td><input type="password" name="pass" placeholder="Your Password" required /></td>
                </tr>
                <tr>
                    <td><input type="text" name="fname" placeholder="Your firstname" required /></td>
                </tr>
                <tr>
                    <td><input type="text" name="lname" placeholder="Your Lastname" required /></td>
                </tr>
                <tr>
                    <tr>
                    <td>
                        <select name="country">
                            <option value="Greece-Athens">Greece-Athens</option>
                            <option value="Italy-Rome">Italy-Rome</option>
                            <option value="France-Paris">France-Paris</option>
                        </select>
                     <select name="hair_color">
                            <option value="Black">Black</option>
                            <option value="Blonde">Blonde</option>
                            <option value="Dark-Brown">Dark Brown</option>
                        </select>
                    <select name="eye_color">
                            <option value="Black">Black</option>
                            <option value="Blue">Blue</option>
                            <option value="Green">Green</option>
                            <option value="Brown">Brown</option>
                            <option value="Hazel">Hazel</option>
                            <option value="Gray">Gray</option>
                        </select>
                     <select name="Body_type">
                            <option value="Slim">Slim</option>
                            <option value="Athletic">Athletic</option>
                            <option value="Average">Average</option>
                            <option value="Overweight">Overweight</option>
                        </select>
                        <select name="height">
                            <option value="152">152</option>
                            <option value="154">154</option>
                            <option value="156">156</option>
                            <option value="158">158</option>
                            <option value="160">160</option>
                            <option value="162">162</option>
                            <option value="164">164</option>
                            <option value="166">166</option>
                            <option value="168">168</option>
                            <option value="170">170</option>
                            <option value="172">172</option>
                            <option value="172">172</option>
                            <option value="174">174</option>
                            <option value="176">176</option>
                            <option value="178">178</option>
                            <option value="180">180</option>
                            <option value="182">182</option>
                            <option value="184">184</option>
                            <option value="186">186</option>
                            <option value="188">188</option>
                            <option value="190">190</option>
                            <option value="192">192</option>
                            <option value="192">192</option>
                            <option value="194">194</option>
                            <option value="196">196</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td><button type="submit" name="btn-signup">Sign Me Up</button></td>
                </tr>
                <tr>
                    <td><a href="index.php">Sign In Here</a></td>
                </tr>
            </table>
        </form>
    </div>
</center>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

请改为尝试:

if (mysql_query("INSERT INTO users(Username,Pasword) VALUES('$uname','$upass')") &&
        mysql_query("INSERT INTO userprofile(
                    gender,
                    dateofbirth,
                    firstname,
                    Lastname,
                    height,
                    eyecolor,
                    haircolor,
                    bodytype,
                    country_town,
                    email,
                    userid)
        VALUES (
                    '$gend',
                    '$dob',
                   '$fname',
                    '$lname',
                    '$height',
                    '$eye_color',
                    '$hair_color',
                    '$Body_type',
                    '$country',
                    '$email',
                    last_insert_id()
         )")) {

注意到“last_insert_id()”值和添加的“userid”列名称。

答案 1 :(得分:1)

我建议您只使用一个表来存储有关用户,密码和所有信息的信息。

例如:

   $sql = "insert into user (username,email,password,acctype) values ('$uname','$email','$password_hash','$acctype')";
   $result = mysqli_query($conn,$sql);

然后,当用户执行certian事项时,您可以使用用户ID跟踪该事件。这意味着对于每个操作,您需要在该表中将用户ID作为外键发布。

   $sql = "select * from bookings WHERE uid ='".$_SESSION['user_id']."' order by id desc limit 15";
   $result = mysqli_query($conn,$sql);

然后你可以根据session或uid选择...

答案 2 :(得分:0)

好的,我找到了方法

我修改了这样的代码:

if (mysql_query("INSERT INTO users(Username,Pasword) VALUES('$uname','$upass')")) {

    //$sql="select id from users";
    $test=mysql_result(mysql_query("SELECT id FROM users LIMIT 1"),0);
    if (mysql_query("INSERT INTO userprofile(userid,gender, dateofbirth, firstname, Lastname, height, eyecolor, haircolor, bodytype, country_town, email) VALUES ('$test','$gend','$dob' ,'$fname','$lname','$height','$eye_color','$hair_color','$Body_type','$country','$email')"))
     {

        ?>

        <script>alert('successfully registered '); </script>

        <?php
        $_SESSION['user'] = $row['id'];
        header("Location: Index.php"); ?>


        <?php
        $_SESSION['user'] = $row['id'];
        header("Location: Index.php");
    } else {
        ?>
        <script>alert('error while registering you...');</script>
        <?php
    }
}