即时通讯试图抄袭我的添加视频提交表单以更新另一个mysql表,以跟踪提交者发布了哪些视频链接。我试图通过使用MAX()获取插入到提交表中的最后一个videoId来插入视频ID,而用户名将仅来自:submitter,并且postdOn使用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);
以下为我工作:
我必须指出,您不能将其$postedOn = now();用作发布当前时间/日期的变量。需要将其输入为VALUES
$postedOn = now();
VALUES
IE: VALUES(:videoId,:username,NOW())";
VALUES(:videoId,:username,NOW())";
请注意,我用作$pdo连接变量。
$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(); }