这段代码的问题是什么,我使用一个表单将一些值插入到数据库中,我有一个类似的控制器设置。当我提交表单时,该值没有发布在数据库中,但是如果我删除了所有其他字段,只在表单中保留了2个字段并发布它,则该值会起作用,因此我错过了一些内容,试图解决该问题已超过6小时。请提供帮助:
//database insertion
if (isset($_POST['VideoTITLE']))
if (isset($_POST['ByArtist']))
if (isset($_POST['GroupName']))
if (isset($_POST['URL']))
if (isset($_POST['VideoDate']))
{
try
{
$sql = 'INSERT INTO videoclip SET
VideoTITLE = :VideoTITLE,
ByArtist = :ByArtist,
GroupName = :GroupName,
URL = :URL,
VideoDate = CURDATE()
';
$s = $pdo -> prepare($sql);
$s -> bindValue(':VideoTITLE',$_POST['VideoTITLE']);
$s -> bindValue(':ByArtist',$_POST['ByArtist']);
$s -> bindValue(':GroupName',$_POST['GroupName']);
$s -> bindValue(':URL',$_POST['URL']);
$s -> execute();
}
catch(PDOException $e)
{
$error = 'error adding submitted data' . $e-> getMessage();
include 'error.html.php';
exit();
}
header('Location:.');
exit();
}
以下是我的html表单设置:
<form action="?" method="post" class="form-horizontal">
<legend>Song Info</legend>
<fieldset>
<label>Song Title </label>
<input type="text" id="VideoTITLE" name="VideoTITLE" placeholder="song name…">
<label>Artist </label>
<input type="text" id="ByArtist" name="ByArtist" placeholder="artist name…">
<label>Musical Group</label>
<input type="text" id="GroupName" name="GroupName" placeholder="Type something…">
<label>Poster link</label>
<input type="text" id="URL" name="URL" placeholder="Type something…">
</fieldset><br>
<input type="submit" class="btn btn-success" value="Post video">
</form>
最佳答案
有几个问题,也许更多:
您的if条件中有isset($_POST['VideoDate'])
,由于VideoDate
不在您的表单中,因此该条件将始终为false。您应该去掉这个,因为您似乎想在插入脚本中使用CURDATE()
来设置它。
插入语句不正确。mysql插入通常看起来像INSERT INTO TABLE_NAME (COL1, COL2) values('VALUE1', 'VALUE2');
,因此您应该将插入代码更改为$sql = 'INSERT INTO videoclip (VideoTITLE, ByArtist, GroupName, URL, VideoDate) values (:VideoTITLE, :ByArtist, :GroupName, :URL, CURDATE())';
关于php - 将值插入数据库的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13979506/