PHP MYSQL INSERT帮助没有错误

时间:2014-05-02 04:23:49

标签: php mysql sql

我试图捎带我的添加视频提交表单来更新另一个mysql表,以跟踪提交者发布什么视频链接。我试图通过使用MAX()将最后一个videoId插入到提交表中来插入视频ID,并且用户名将仅来自:submitter和postingOn正在使用now()来获取时间戳。使用提交表单后,没有数据输入adminposts表,我没有收到任何错误。谢谢你的帮助。

<?php


    require_once '../connection.php';

    $sql = "INSERT INTO videoinfo
            (submitter,videoTitle,channelName,videoLink,videoLength)
            VALUES(:submitter,:videoTitle,:channelName,:videoLink,:videoLength)";

    $stmt = $dataconn -> prepare($sql); 
    $stmt -> execute(array(":submitter"=> $_POST['submitter'],
    ":videoTitle"=> $_POST['videoTitle'],
    ":channelName"=> $_POST['channelName'],
    ":videoLink"=> $_POST['videoLink'],
    ":videoLength"=> $_POST['videoLength']));

    $sql = "SELECT MAX(videoId) FROM videoinfo";
    $stmt = $dataconn->prepare($sql);   
    $stmt -> execute(array());
    $record = $stmt->fetch();
    $videoID = $record['videoId'];

    $username = $_POST['submitter'];
    $postedOn = now();

    $sql = "INSERT INTO adminposts
            (videoId,username,postedOn)
            VALUES(:videoId,:username,postedOn = now())";
    $stmt = $dataconn -> prepare($sql);
    $stmt -> execute(array(":videoId"=> $videoID,
                            ":username"=> $username));      





    echo "<h3>Video has been successfully added!";

    include 'adminmain.php'
    ?>

更新了代码

    $sql = "SELECT MAX(videoId) AS videoId FROM videoinfo";
    $stmt = $dataconn->prepare($sql);   
    $stmt -> execute(array());
    $record = $stmt->fetch();
    $videoID = $record['videoId'];
    var_dump($videoID);
    $username = $_POST['submitter'];
    $postedOn = now();

    $sql = "INSERT INTO adminposts
            (videoId,username,postedOn)
            VALUES(:videoId,:username,$postedOn)";
    $stmt = $dataconn -> prepare($sql);
    $stmt -> execute(array(":videoId"=> $videoID,
                            ":username"=> $username));
$stmt->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

2 个答案:

答案 0 :(得分:1)

由于您的问题的评论已突出显示,因此您不会执行任何错误检查,如果您希望使用该路线,则需要为MAX(videoId)创建别名。

但是,对于您的特定用例,我建议您查看PDO :: lastInsertId()以获取刚刚插入的videoId。 http://www.php.net/manual/en/pdo.lastinsertid.php

这将消除显式写出第二个查询的需要,并为MAX()列添加别名。

答案 1 :(得分:1)

以下对我有用:

我必须指出,您无法使用$postedOn = now();作为变量来发布当前时间/日期。它需要作为VALUES

的一部分输入

即:VALUES(:videoId,:username,NOW())";

请注意我使用$pdo作为连接变量。

<?php

$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysql_dbname = 'xxx';

try {

$pdo= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password); 
     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
     exit( $e->getMessage() );
}

$sql = "SELECT MAX(videoId) AS videoId FROM videoinfo";

$stmt = $pdo->prepare($sql);   
$stmt -> execute(array());
$record = $stmt->fetch();
$videoID = $record['videoId'];

// var_dump($videoID);

$username = $_POST['submitter'];

try {
$sql = "INSERT INTO adminposts
        (videoId,username,postedOn)
        VALUES(:videoId,:username,NOW())";
$stmt = $pdo -> prepare($sql);
$stmt -> execute(array(":videoId"=> $videoID,":username"=> $username));
}

catch(PDOException $e){
// $result = "Sorry, an error occurred while editing the database.";

// will print a message of the actual error should there be one
print $e->getMessage();

    }