简体   繁体   中英

PDO && mysql insert query

I am building a commenting system. It works in the same way with MySQL, MySQLi and PDO. I have a lot of customers therefore I will have multiple Ids.

In my insert query I am defining a FOREIGN KEY which is the user_id from the customer's table into the notes table.

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.

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 ?

<?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. Instead this, send the current post id and populate this variable dynamically. Also set the Id from your user table to auto incrementing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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