PHP MYSQL INSERT帮助没有错误 [英] PHP MYSQL INSERT help no errors

查看:66
本文介绍了PHP MYSQL INSERT帮助没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重新添加我的添加视频提交表单"以更新另一个mysql表,以跟踪提交者发布了哪些视频链接.我试图通过使用MAX()获取插入到提交表中的最后一个videoId来插入视频ID,而用户名将仅来自:submitter,并且postdOn使用now()来获取时间戳.使用提交表单后,没有数据输入到adminposts表中,并且没有收到任何错误.感谢您的帮助.

Im attempting to piggy back on my Add a video submission form to update another mysql table to keep track of what submitter posts what video links. Im trying to insert the video ID by taking the last videoId inserted into the submission table using MAX() and the username will just be taken from :submitter and the postedOn is using now() to get a timestamp. After using the submission form no data is entered into the adminposts table and im not getting any errors. Thanks for the help.

<?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);

推荐答案

以下内容对我有用:

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

I have to point out that you can't use $postedOn = now(); as a variable to post the current time/date. It needs to be entered as part of the VALUES

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

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

Do note that I used $pdo as the connection variable.

<?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();

    }

这篇关于PHP MYSQL INSERT帮助没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆