Mysql - conditional insert query with select and PDO

Django Anonymous

I am unable to understand on how to apply insert query with select statement:

I have gone through this question also:

MySQL INSERT from a SELECT with PDO

But where is the VALUES part??

Like I have this query to insert in Mysql and here I use Values also:

$db_conn->beginTransaction();
$query = $db_conn->prepare('INSERT INTO mytable (name, user_id) VALUES(:sname, :uid)');
foreach($UploadData AS $DataValue)
{
    $query->execute(array(':sname' => $DataValue['Name'],':uid' =>$_SESSION['uid']));
}
$db_conn->commit();

My motto is to check if the name exists with the same uid it shouldn't import the data otherwise it should. But Where are the values part :/ I am blind :P

EDIT1: From MySQL INSERT from a SELECT with PDO

How will this code block work if no VALUES is supplied?

$sql_enc = '
    INSERT INTO sessionid (enc_id, enc_pass, enc_date) 
        (SELECT AES_ENCRYPT(username, :aeskey), AES_ENCRYPT(pwd, :aeskey), DATE_ADD(NOW(), INTERVAL 15 SECOND) FROM users WHERE username = :username)
';
$res_enc = $pdo->prepare($sql_enc);
$res_enc->bindParam(':aeskey', $aeskey);
$res_enc->bindParam(':username', $username);
$res_enc->bindParam(':pwd', $username);
$res_enc->execute();
$res_enc = null;
Michel Feldheim

There are two valid INSERT syntax:

INSERT 
    INTO `table` [(field1, field2)] 
    VALUES ( 'val1', 'val2' )

Or

INSERT 
    INTO `table` [(field1, field2)] 
    SELECT 'val1', 'val2'

the selected columns are your value fields.

@comments: Replace: http://dev.mysql.com/doc/refman/5.5/en/replace.html

Procedures: http://dev.mysql.com/doc/refman/5.6/en/create-procedure.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL query to Select -> Insert and Delete selected rows

From Dev

INSERT INTO SELECT PDO query

From Dev

Converting MySql Insert To PDO

From Dev

Conditional SQL INSERT INTO query

From Dev

PDO effective SELECT and INSERT

From Dev

PHP; PDO; MySQL; INSERT query not taking input

From Dev

Mysql - INSERT from SELECT with conditional ON DUPLICATE KEY UPDATE

From Dev

PHP; PDO; MySQL INSERT query with drop down

From Dev

Conditional select queries with PDO

From Dev

conditional insert into mysql

From Dev

INSERT INTO array with MySQL and PDO

From Dev

MySQL Conditional Select Replace Query

From Dev

MySQL query to Select -> Insert and Delete selected rows

From Dev

INSERT INTO SELECT PDO query

From Dev

mysql query error insert joined select

From Dev

Mysql Insert query with values and select as subquery

From Dev

PDO effective SELECT and INSERT

From Dev

PDO insert into MySQL Database not working with another PDO query

From Dev

MYSQL INSERT INTO FROM SELECT QUERY

From Dev

MySQL: Insert with conditional

From Dev

mysql query INSERT INTO in a conditional statements (case if/else)

From Dev

conditional insert into mysql

From Dev

mysql - SELECT AND INSERT QUERY into multiple table with conditions

From Dev

PHP PDO can't run query INSERT INTO with SELECT

From Dev

Issue running query based on SELECT statement with MySQL and PHP PDO

From Dev

Mysql crashed on conditional insert

From Dev

mysql insert with select query to insert multiple rows

From Dev

PDO insert BindParam mysql

From Dev

Mysql query insert into only if select count > n