简体   繁体   English

将值插入数据库的问题

[英]Issue with inserting value into a database

what is the issue with this code , I'm using a form to insert some values into a database , i have a controller setup like that. 这段代码有什么问题,我正在使用一种形式将一些值插入数据库中,所以我有一个类似的控制器设置。 when i submit the form , the value was not posted in the database, but if i remove all others fields and left only 2 fields in the form and post it ,it works so there's something that i miss,been trying to resolve for more than 6 hours .please some help : 当我提交表单时,该值未发布到数据库中,但是如果我删除所有其他字段并在表单中仅保留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();
}

here's my html form setup: 这是我的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>

Its a couple of problems, maybe more: 它有几个问题,也许还有更多:

  1. You have isset($_POST['VideoDate']) in your if condition which will always be false since VideoDate is not in your form. 您的if条件中包含isset($_POST['VideoDate']) ,由于VideoDate不在您的表单中,因此它始终为false。 You should take this out since you seem to want to set it using CURDATE() in your insert script. 您应该删除此内容,因为您似乎想在插入脚本中使用CURDATE()对其进行设置。
  2. your insert statement is incorrect. 您的插入语句不正确。 mysql inserts typically look like INSERT INTO TABLE_NAME (COL1, COL2) values('VALUE1', 'VALUE2'); mysql插入通常看起来像是INSERT INTO TABLE_NAME (COL1, COL2) values('VALUE1', 'VALUE2'); so you should change your insert code to look like 所以您应该将插入代码更改为

    $sql = 'INSERT INTO videoclip (VideoTITLE, ByArtist, GroupName, URL, VideoDate) values (:VideoTITLE, :ByArtist, :GroupName, :URL, CURDATE())';

Your syntax is incorrect for INSERT . 您的语法对于INSERT不正确。 It should be something like: 应该是这样的:

$sql = 'INSERT INTO videoclip (VideoTITLE, ByArtist, GroupName, URL, VideoDate) 
    VALUES (:VideoTITLE, :ByArtist, :GroupName, :URL, CURDATE())';

In addition, $_POST['VideoDate'] is not valid as you do not have it in your form. 此外, $_POST['VideoDate']无效,因为您的表单中没有此名称。

You're doing the if statements wrong. 您正在执行if语句错误。

if (isset($_POST['VideoTITLE']) && isset($_POST['ByArtist']) && isset($_POST['GroupName'])
    && isset($_POST['URL']) && isset($_POST['VideoDate'])) {
....
}

This is basic programming stuff, so you might want to get a good introductory book to programming or PHP. 这是基本的编程知识,因此您可能想获得一本很好的编程或PHP入门书。

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

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