【问题标题】:Data not being inserted mysql数据没有被插入mysql
【发布时间】:2020-11-11 19:59:42
【问题描述】:

我正在为自己构建一个自定义 CMS,它运行良好,但由于某种原因,系统没有插入某个称为类别的字段。我有一个名为类别的表,我可以毫无问题地插入这些类别。 在我的 addnewpost.php 页面上,我有这个表单字段,可以让我选择一个添加的类别..

<select name="Category" id="categorytitle" class="form-control">
 <?php 
   $sql = "SELECT id,title FROM category";
   $stmt = $conn->query($sql);
   while ($DataRows = $stmt->fetch()){
     $id = $DataRows["id"];
     $CategoryName = $DataRows["title"];
 ?>
   <option value=""><?php echo $CategoryName; ?></option>
 <?php } ?>
</select>

最重要的是,我将数据插入数据库中的一个名为帖子的表中......

<?php
if(isset($_POST["Submit"])){
  $posttitle  = $_POST["posttitle"];
  $Category   = $_POST["Category"];
  $image      = $_FILES["image"]["name"];
  $target     = "../uploads/".basename($_FILES["image"]["name"]);
  $posttext   = $_POST["postdescription"];
  $admin      = "phillip";

  date_default_timezone_set("Europe/London");
  $CurrentTime=time();
  $DateTime=strftime("%B-%d-%Y %H:%M:%S",$CurrentTime);

  if(empty($posttitle)){
    $_SESSION["ErrorMessage"] = "Post title cannot be empty";
    redirect_to("addnewpost.php");
  } elseif (strlen($posttitle)<10){
    $_SESSION["ErrorMessage"] = "Post title should be greater than 10 characters";
    redirect_to("addnewpost.php");
  } else {
    //All is good insert into the database
    $sql = "INSERT INTO posts(datetime,title,category,author,image,post)";
    $sql .= "VALUES(:dateTime,:postTitle,:categoryName,:adminName,:imageName,:postDescription)";
    $stmt = $conn->prepare($sql);
    $stmt->bindValue(':dateTime',$DateTime);
    $stmt->bindValue(':postTitle',$posttitle);
    $stmt->bindValue(':categoryName',$Category);
    $stmt->bindValue(':adminName',$admin);
    $stmt->bindValue(':imageName',$image);
    $stmt->bindValue(':postDescription',$posttext);

    $Execute=$stmt->execute();
    move_uploaded_file($_FILES["image"]["tmp_name"],$target);

    if($Execute){
      $_SESSION["SuccessMessage"]="Post with id : ".$conn->lastInsertId()." Added Successfully";
      redirect_to("addnewpost.php");
    } else {
      $_SESSION["ErrorMessage"]="Something went wrong. Try again";
      redirect_to("addnewpost.php");
    }
  }
}
?>

这是一个显示类别字段为空的屏幕截图。我确实启用了显示错误,但没有出现。任何想法表示赞赏...

【问题讨论】:

  • &lt;option value=""&gt; 不应该是&lt;option value="$id"&gt;
  • 更有可能&lt;option value="&lt;?php echo $id; ?&gt;"&gt;

标签: php mysql content-management-system


【解决方案1】:

谢谢奈杰尔和费利佩。我刚刚取出了选项值,所以它只是这个......

<option><?php echo $CategoryName; ?></option>

这让一切都完美无缺。也一直在看这个几个小时。 谢谢两位。

【讨论】:

    【解决方案2】:
       <select name="Category" id="categorytitle" class="form-control">
         <?php 
           $sql = "SELECT id,title FROM category";
           $stmt = $conn->query($sql);
           while ($DataRows = $stmt->fetch()){
             $id = $DataRows["id"];
             $CategoryName = $DataRows["title"];
         ?>
           <option value="<?php echo $CategoryName; ?>"><?php echo $CategoryName; ?></option>
         <?php } ?>
        </select>
    

    这个比较像

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多