doulingzhuang3079 2014-01-25 12:14
浏览 39
已采纳

Mysql - 带select和PDO的条件插入查询

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;
  • 写回答

2条回答 默认 最新

  • duanlachu7344 2014-01-25 12:29
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵