0

我簡單給你的問題... :-) 我想將數據插入到有2代表一個是用戶和第二數據庫是USERPROFILE值插入DB與外鍵

這裏是我的分貝:

enter image description here

正如你看到的,我有外鍵的用戶 - > ID與userprofile->用戶ID 的問題是,當我想使新用戶的註冊,我不能.... 但是當我沒有外鍵,那麼它的一切都好。 但我需要使用外鍵註冊她是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> 
+0

您需要捕獲用戶插入的insert_id,並將其用作userprofile插入中用戶標識的變量。 – Lance

+0

僅供參考,mysql從PHP5.5開始折舊,並將在PHP7中刪除。使用mysqli或PDO。 – Lance

回答

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() 
     )")) { 

通知d添加「last_insert_id()」值和「userid」列名稱。

1

我建議您只使用一張表來存儲有關用戶,密碼和所有信息。

如:

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

那麼當用戶執行certian東西,你可以跟蹤使用用戶ID。這意味着對於每個操作,您需要將該用戶標識作爲外鍵發佈在該表中。

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

,那麼你可以選擇基於會話或UID ...

0

OK我發現

我修改這樣的代碼的方式:

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 
    } 
}