dqcwl02022 2018-06-20 03:41
浏览 40

外键不起作用

I am creating one table that includes a primary key and another table that includes a foreign key.

One table is for registration and the other one also for registration.

Both table work fine before I add a foreign key.

After I add a foreign key, the table related to the foreign key registration doesn't work.

It can't do any updating.

I want to fetch data from both tables, but I cannot.

I would really appreciate it if anyone could help me.

Below, I included my table containing the foreign key and users containing the primary key.

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

Here is my code for fetching data:

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

The code for the table including the foreign key (This form works fine until I add the foreign key. After I add the foreign key, it doesn't want to work.):

<?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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 数学建模,尽量用matlab回答,论文格式
    • ¥15 昨天挂载了一下u盘,然后拔了
    • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
    • ¥20 易康econgnition精度验证
    • ¥15 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能