简体   繁体   English

PDO && mysql插入查询

[英]PDO && mysql insert query

I am building a commenting system. 我正在建立一个评论系统。 It works in the same way with MySQL, MySQLi and PDO. 它与MySQL,MySQLi和PDO的工作方式相同。 I have a lot of customers therefore I will have multiple Ids. 我有很多客户,因此我将有多个ID。

In my insert query I am defining a FOREIGN KEY which is the user_id from the customer's table into the notes table. 在我的插入查询中,我定义了一个FOREIGN KEY,它是从客户表到notes表的user_id

My question is, after the VALUES attribute, what should be the first value to identify the specific id for the particular customer I am working on. 我的问题是,在VALUES属性之后,应该是第一个值来标识我正在处理的特定客户的特定ID。

If I leave the value as VALUES (1,...) in my database, each comment will always be pointed at user_id (1) , what can I work on user_id 2 and 3 ? 如果我在数据库中将值保留为VALUES (1,...) ,则每个注释将始终指向user_id (1) ,该如何处理user_id 23

<?php
            // DEFINE QUERY HERE
            $query = $dbc->prepare("INSERT INTO `notes`(`user_id`,`entry`, registration_date)
                                    VALUES (1,:entry, NOW())");

            //EXCECUTE QUERY
            $query->bindValue(':entry', $_POST['entry']);
            $query->execute();
            echo '<span style="color:red;">Note Added </span>';
            $dbc = null;
        } else {
            echo '<span style="color:red;">Note was not added, insert Admin notes</span>';
        }
    }
    catch(PDOExeption $e){
        echo $e->getMessage();
    }
?>

Please, remove the value 1 from your statement. 请从您的语句中删除值1。 Instead this, send the current post id and populate this variable dynamically. 相反,发送当前帖子ID并动态填充此变量。 Also set the Id from your user table to auto incrementing. 还将用户表中的ID设置为自动递增。

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

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