简体   繁体   English

外键不会在级联上更新

[英]foreign key does not update on cascade

I am using phpmyadmin 4.7.3 version. 我正在使用phpmyadmin 4.7.3版本。 I made 2 tables with innoDB storage engine in a same database and i want to update foreign key automatically by using cascade but it does not work at all. 我在同一数据库中使用innoDB存储引擎制作了2个表,我想通过使用层叠自动更新外键,但它根本不起作用。 In my dineOwnerUser table i have id field which is foreign key in webpromo table with the name of ownerid. 在我的dineOwnerUser表中,我有id字段,这是webpromo表中具有ownerid名称的外键。 Here are all step which i did to make it foreign key. 这是我为使其成为外键所做的所有步骤。

  1. went to webpromo table 去了网络促销表
  2. clicked on relation view button 单击关系视图按钮
  3. after that i setup all option which is visible in image 之后,我设置了所有在图像中可见的选项

在webpromo表中,我这样做是为了做一个外键

So far i have explained which i have done the problem is that my webpromo table is totally empty even though foreign key is also not updating automatically. 到目前为止,我已经解释了我所做的问题是,即使外键也没有自动更新,我的webpromo表还是完全空的。 If i am wrong here kindly please guide me also I am posting my code just in case i am doing some thing wrong in coding here is my php code 如果我在这里不对,请也指导我,我也发布我的代码,以防万一我在编码中做错了这是我的PHP代码

    <?php
session_start();
    if(isset($_POST['recaptcha'])){
        $secret = "************";
        $response = $_POST['recaptcha'];
        $remoteip = $_SERVER['REMOTE_ADDR'];
        $url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip");
        $content = json_decode($url, TRUE);
        if($content['success'] ==1){
            function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            $data = strtolower($data);
            return $data;
            }
            $discount = test_input($_POST["discount"]);
            $discountitem = test_input($_POST["discountitem"]);
            $website = test_input($_POST["website"]);
            $expirydate = test_input($_POST["expirydate"]);
            $desc = test_input($_POST["desc"]);
            $filename;
            if(isset($_FILES['logouploader']['name'])){
                $filename = basename($_FILES['logouploader']['name']);
                $filename = test_input($filename);
            }
            $dir = "img/uploads/";
            $ext = strtolower(pathinfo($_FILES['logouploader']['name'], PATHINFO_EXTENSION));
            $allowed =  array('jpeg','png' ,'jpg');
            if(!in_array($ext,$allowed) ) {
                echo "wrongext";
                $uploadOk = 0;
                exit;
            }
            if ($_FILES["logouploader"]["size"] > 600000) {
                echo "large";
                $uploadOk = 0;
                exit;
            }
            $uploadOk = 1;
            if ($uploadOk == 0) {
                echo "Sorry";
                exit;
            }
            if ($uploadOk == 1) {
                move_uploaded_file($_FILES["logouploader"]["tmp_name"], $dir.$filename);
                $servername = "localhost";
                $username = "*****";
                $password = "*****";
                try {
                    $conn = new PDO("mysql:host=$servername;dbname=*********", $username, $password);
                    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    $query = "INSERT INTO webpromo (discount, dealitem, website, expirydate, description, logouploader) VALUES (?, ?, ?, ?, ?, ?)";
                    $statement = $conn->prepare($query);
                    $statement->execute(
                        array(
                        $discount,
                        $discountitem,
                        $website,
                        $expirydate,
                        $desc,
                        $filename
                        ) );
                        $conn = null;
                        exit;
                        echo "done";
                }
                catch(PDOException $e)
                {
                    echo "Connection failed: " . $e->getMessage();
                }
            }
        }
        if($content['success'] !=1){
            echo "notok";
            $conn = null;
            exit;
        }
        $conn = null;
        exit;
    }
?>

Note: i am getting image in my folder that's mean upto move_uploaded_file($_FILES["logouploader"]["tmp_name"], $dir.$filename); 注意:我正在我的文件夹中获取图像,该图像的意思是move_uploaded_file($ _ FILES [“ logouploader”] [“ tmp_name”],$ dir。$ filename); my code is working fine 我的代码工作正常

but after that when i try to populate my webpromo table with the form input values as well as uploaded image name it does not populate at all. 但是之后,当我尝试使用表单输入值以及上载的图像名称填充我的webpromo表时,它根本不会填充。 Thanks 谢谢

You have got the wrong idea my friend it does not mean your data will be automatically inserted if you are using cascade . 我的朋友,您有个错误的主意,这并不意味着您在使用层叠时将自动插入您的数据。

What is used for actually is if the primary key value is updated then it updates the foreign key value in all respective tables. 实际使用的是,如果主键值被更新,那么它将更新所有相应表中的外键值。 You have to write a seperate insert query. 您必须编写一个单独的插入查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM