简体   繁体   中英

Inserting only 1 data into database PDO

Why is this not working please help me! how can i insert 1 data into database and also i want where in it :(

$Recipient = $_GET['Add_Friend'];
$Sender = $_SESSION['IsLogged'];

$query = 'INSERT INTO '.$DB_Table.' ('.$DB_Column['Friend_Requests'].') VALUES (:Sender) WHERE '.$User['Username'].'=:Recipient';
$run = $Database->prepare($query);
$run->execute
    (
    array(
            ':Sender' => $Sender,
            ':Recipient' => $Recipients
        )
    );

INSERT does not use the WHERE clause, because it adds data to a table, it doesn't filter anything. Your statement would probably look like this:

INSERT INTO someTable (someColumn)
VALUES ('some value')
WHERE otherColumn='other value';

What you probably want is:

INSERT INTO someTable (someColumn, otherColumn)
VALUES ('some value', 'other value');

This ':Recipient' => $Recipients

should be ':Recipient' => $Recipient

as per $Recipient = $_GET['Add_Friend'];

you're using the wrong variable => $Recipients you've " pluralized " it.

Add $Database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after the connection is opened, including, and at the top of your file(s):

error_reporting(E_ALL);
ini_set('display_errors', 1);

in order to troubleshoot/debug your code.

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