外键不起作用

时间:2018-06-20 03:42:00

标签: php mysql mysqli

我正在创建一个包含主键的表和另一个包含外键的表。

一个表用于注册,另一个表也用于注册。

在添加外键之前,两个表都可以正常工作。

添加外键后,与外键注册相关的表不起作用。

它无法进行任何更新。

我想从两个表中获取数据,但是我不能。

如果有人能帮助我,我将非常感激。

下面,我包含了包含外键的表和包含主键的用户。

below i include my table try contain foreign key and users contain primary key

这是我的数据提取代码:

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

0 个答案:

没有答案