Combine 2 $_POST into One column using INSERT statement

David Morin :

I have a form that has a hidden field (the date). I want this data to be inserted with the note that is submitted as well. I only have 3 columns in my table. ID,Note_id and Note.

So if a user submits a note the insert statement should add the date infront of the note.

My Current SQL Statement:

$pdoOptions = array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => false
);

$pdo = new PDO("mysql:host=" . MYSQL_HOST . ";dbname=" . MYSQL_DATABASE, MYSQL_USER,MYSQL_PASSWORD,$pdoOptions);

$data = [
   'notes' => $_POST['notes'],
   'id' => $_POST['rowID'],
   'date2' => $_POST['date'],
];
$sql = "INSERT INTO notes (note_id, note) Values(:id, :notes)";
$stmt= $pdo->prepare($sql);
$stmt->execute($data);

My thought was I could add the :date2 to this but it does not seem to work

$pdoOptions = array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => false
);

$pdo = new PDO("mysql:host=" . MYSQL_HOST . ";dbname=" . MYSQL_DATABASE, MYSQL_USER,MYSQL_PASSWORD,$pdoOptions);

$data = [
   'notes' => $_POST['notes'],
   'id' => $_POST['rowID'],
   'date2' => $_POST['date'],
];
$sql = "INSERT INTO notes (note_id, note) Values(:id, :date '-' :notes)";
$stmt= $pdo->prepare($sql);
$stmt->execute($data);

Whats the best method of doing this? I know I can have another column but I dont want that structure. The way it is displayed on the users side needs to be like this.

Thank you

Ronak Dhoot :

Use CONCAT()

$sql = "INSERT INTO notes (note_id, note) Values(:id, CONCAT(:date,'-',:notes))";

This will work

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=297954&siteId=1