外键不工作

ux6nzvsh  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(129)

我正在创建一个包含主键的表和另一个包含外键的表。
一张table是登记用的,另一张也是登记用的。
在添加外键之前,两个表都可以正常工作。
添加外键之后,与外键注册相关的表就不起作用了。
它不能做任何更新。
我想从两个表中提取数据,但我不能。
如果有人能帮我,我会非常感激的。
下面,我包括了包含外键的表和包含主键的用户。

以下是我获取数据的代码:

<?php
$mysqli=new MySQLi('127.0.0.1','root','','accounts');
$sql = "SELECT * FROM users INNER JOIN try ON users.user_ID = try.user_ID";
$result= mysqli_query($mysqli,$sql);

?>
<!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>fetch data</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
<script src="https://maxcdn.bootstrapcdn.com.bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
</br>
<table>
<tr>
<th>email</th>
<th>email</th>
</tr>
<?php
if(mysqli_num_rows($result)>0)
{
    while($row = mysqli_fetch_array($result))
    {
        ?>
        <tr>
        <td><?php echo $row["email"];?></td>
        <td><?php echo $row["email"];?></td>
        </tr>

        <?php
    }
}
?>
</br>
</table>

</body>
</html>

包含外键的表的代码(在我添加外键之前,此窗体工作正常)。我添加外键后,它不想工作。):

<?php
session_start();
$mysqli=new MySQLi('127.0.0.1','root','','accounts');
 if(isset($_POST["login"])) {
        $name = $mysqli->real_escape_string($_POST['name']);
        $email = $mysqli->real_escape_string($_POST['email']);
        $address = $mysqli->real_escape_string($_POST['address']);
        $password = md5($_POST['password']);

        $sql ="INSERT INTO try(name,email,password,address)"
                ."VALUES ('$name','$email','$password','$address')";        

                if($mysqli->query($sql)=== true) {

                           $_SESSION['message'] =  "Registration successful!
                          Added $username to the database!";
                          header("location:login.php");
                      }
                      else {
                          $_SESSION['message'] = "User could not be added to the database!";        }
}

?> 

<!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>Tutor Register</title>
</head>

<body><?=$_SESSION['message']?>
<form class="form2" action="" method="POST" >
      <div class="alert alert-error"></div>
      <input type="text" placeholder="User Name" name="name" required />
      <input type="email" placeholder="Email" name="email" required />
       <input type="text" placeholder="address" name="address" required />
      <input type="password" placeholder="Password" name="password" autocomplete="new-password" required />
      <input type="password" placeholder="Confirm Password" name="confirmpassword" autocomplete="new-password" required />
      <input type="submit" value="REGISTER" name="login" class="btn btn-block btn-primary"/>
    </form>
</body>
</html>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题