简体   繁体   English

插入表VALUES-mysql_query到PDO

[英]INSERT INTO table VALUES - mysql_query to PDO

trying to insert values from an old mysql_query using the new PDO and can't seem to get it. 试图使用新的PDO从旧的mysql_query插入值,但似乎无法获取它。 Here's the old code that works with the old method: 这是与旧方法兼容的旧代码:

$query = mysql_query("INSERT INTO videos VALUES ('','$title',time(),'0','$length','','$name','$cat','$reciter','$genre')");

I've tried variations of the following code taken from another question on stack, but nothing that works for me. 我已经尝试了以下代码的变体,这些代码取自另一个堆栈上的问题,但对我没有任何帮助。

$query = "UPDATE people 
         SET price=?, 
             contact=?, 
             fname=?, 
             lname=? 
          WHERE id=? AND 
                username=?";
$stmt = $dbh->prepare($query);
$stmt->bindParam(1, $price);
$stmt->bindParam(2, $contact);
$stmt->bindParam(3, $fname);
$stmt->bindParam(4, $lname);
$stmt->bindParam(5, $id);
$stmt->bindParam(6, $username);    
$stmt->execute();

the first value to be inserted is an auto increment value in the db. 要插入的第一个值是数据库中的自动增量值。 I am at a loss as to how to write that with the new PDO. 我不知道如何用新的PDO编写它。 Then the third is an attempt at a timestamp. 然后,第三次尝试进行时间戳记。 All others are values that exist in the script already. 所有其他值都是脚本中已经存在的值。

So this is more along the lines of what I'm looking for.. Its what I have now, but doesn't work. 因此,这更符合我的要求。.它现在拥有的东西,但是不起作用。

$sql = "INSERT INTO videos (id, title, timestamp, views, length, image, vid_url, cetegory, reciter, genre)
                    VALUES (:id, :title, :timestamp, :views, :length, :image, :vid_url, :category, :reciter, :genre)";

$query = $DBH->prepare($sql); 
$results = $query->execute(array(
    ":id" => '',
    ":title" => $title,
    ":timestamp" => time(),
    ":views" => '0',
    ":length" => $length,
    ":image" => '',
    ":vid_url" => $name,
    ":category" => $cat,
    ":reciter" => $reciter,
    ":genre" => $genre
));

If id is an autoincrement, don't pass it to your query. 如果id是自动递增,请不要将其传递给查询。 If you specify a value to be inserted to an autoincrement table, sql will attempt to insert that value. 如果您指定要插入到自动增量表中的值,则sql将尝试插入该值。 so don't include it in the query, let SQL do that. 因此不要在查询中包含它,而让SQL做到这一点。

Secondly, if the third field is a timestamp, set the default to 其次,如果第三个字段是时间戳记,则将默认值设置为

CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

SQL will update the timestamp automatically on update or insert (if you only want it on insert just put CURRENT_TIMESTAMP . then you also can drop that row. SQL会在更新或插入时自动更新时间戳(如果只希望在插入时添加CURRENT_TIMESTAMP ,那么您也可以删除该行。

You can also drop the images row if you aren't inserting anything there either, no point to put something in the query if you're not using it! 如果您也未在其中插入任何内容,也可以删除图像行,如果不使用该行,则无需在查询中放入任何内容!

Also, the key PDO looks for doesn't have the colon ( : ) so if your item is :title , the array key will be just 'title' . 此外,关键PDO查找没有冒号( : ),所以如果您的项目:title ,数组键将只是'title' So, your code should look something like: 因此,您的代码应类似于:

$sql = "INSERT INTO videos ( title, views, length,, vid_url, cetegory, reciter, genre)
                    VALUES (:title, :views, :length, :vid_url, :category, :reciter, :genre)";

$query = $DBH->prepare($sql); 
$results = $query->execute(array(
    "title" => $title,
    "views" => '0',
    "length" => $length,
    "vid_url" => $name,
    "category" => $cat,
    "reciter" => $reciter,
    "genre" => $genre
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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