Php 使用下拉菜单插入数据库

Php 使用下拉菜单插入数据库,php,pdo,Php,Pdo,问题是在下拉列表中插入。我能够将数据库中的数据填充到下拉列表中。问题是从下拉列表中插入到表中 HTML(从数据库中生成下拉列表) 主题 团体 ---选择部--- PHP(插入数据库的代码) 我将您的第一个查询移至页面顶部。在我看来,这就是用组数据填充html的内容 我清理了一下你的html。格式良好的代码更容易阅读,并且在出现问题时更容易进行故障排除。我喜欢避免闯入和闯出php 您的insert查询很接近,但我为您提供了一个非常清晰的示例。这应该会告诉你前进的方向。记住:准备、绑定和执行 &

问题是在下拉列表中插入。我能够将数据库中的数据填充到下拉列表中。问题是从下拉列表中插入到表中

HTML(从数据库中生成下拉列表)


主题
团体
---选择部---
PHP(插入数据库的代码)


我将您的第一个查询移至页面顶部。在我看来,这就是用组数据填充html的内容

我清理了一下你的html。格式良好的代码更容易阅读,并且在出现问题时更容易进行故障排除。我喜欢避免闯入和闯出php

您的insert查询很接近,但我为您提供了一个非常清晰的示例。这应该会告诉你前进的方向。记住:准备、绑定和执行

<?php

//DB select statement  - This should probably go before your select html
$sql= "SELECT * FROM groups";
$stmt = $db->prepare($sql); //Prepare
//Nothing to bind
$stmt->execute(); //Execute
$groups = $stmt->fetchAll();

echo
'<div class="group">
  <label>Subject</label>
  <input type="text" name="subject">
</div> 

<div class="group">

  <label>Group</label>                    

    <select id="ministry" name="group">                        
      <option style="font-family: century gothic">---Select Ministry---</option>';

      foreach($groups as $group){
        echo
        '<option value="' . $group['group_id'] . '">' . $group['groupname'] . '</option>';
      }

   echo 
   '</select>

</div>';

if(isset($_POST['sendSMS'])){

  //insert into database 
  $query = "INSERT INTO `message` 
  (
     `date`,
     `subject`,
     `group`,
     `message`
   )

VALUES

  (
    :date,
    :subject,
    :group,
    :message

  )";

  //Remember these three steps.  1.)Prepare, 2.)Bind, 3.)Execute

  $stmt = $db->prepare($query); //Prepare

  //Bind
  $stmt->bindParam(":date",    $_POST['date']);
  $stmt->bindParam(":subject", $_POST['subject']);
  $stmt->bindParam(":group",   $_POST['group']);
  $stmt->bindParam(":message", $_POST['message']);

  //Execute 
  $stmt->execute();


  echo "SMS sent successfully";

}

?>


消息
发送短信

您是否收到任何错误?打印$groups的结果并检查其中是否有任何内容?共享您的所有代码?致命错误:未捕获的异常“PDOException”,消息为“SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,了解使用C:\xampp\htdocs\SACOMM\sendSMS.php:48堆栈跟踪:#0 C:\xampp\htdocs\SACOMM\sendSMS.php(48):PDOStatement->execute()中第1行的“组,消息”值('08/06/2018)、“测试”、“家庭作业俱乐部”、“测试”)的正确语法 #1@WayneWilliam Try更新了答案。我确保所有内容的间隔都正确。表格名称就在括号上。这可能是问题所在。请告诉我。我认为这不是问题所在。问题仍然存在。
<?php

$date = "";
$subject = "";
$group = "";
$message = "";  

$sql= "SELECT * FROM groups";
$stmt = $db->prepare($sql);
$stmt->execute();
$groups = $stmt->fetchAll();

if (isset($_POST['sendSMS'])) {
    $date = (isset($_POST['date']));
    $subject = $_POST['subject'];
    $group = $_POST['group'];
    $message = $_POST['message'];

    $sql = "INSERT INTO message (date, subject, group, message) 
            VALUES 
           (:date, :subject, :group, :message)";

    $stmt->execute(array(
        ':date' => $_POST['date'],
        ':subject' => $_POST['subject'],
        ':group' => $_POST['group'],
        ':message' => $_POST['message']));

    $result = $sql->execute();
    echo "SMS sent successfully";
}

?>
<?php

//DB select statement  - This should probably go before your select html
$sql= "SELECT * FROM groups";
$stmt = $db->prepare($sql); //Prepare
//Nothing to bind
$stmt->execute(); //Execute
$groups = $stmt->fetchAll();

echo
'<div class="group">
  <label>Subject</label>
  <input type="text" name="subject">
</div> 

<div class="group">

  <label>Group</label>                    

    <select id="ministry" name="group">                        
      <option style="font-family: century gothic">---Select Ministry---</option>';

      foreach($groups as $group){
        echo
        '<option value="' . $group['group_id'] . '">' . $group['groupname'] . '</option>';
      }

   echo 
   '</select>

</div>';

if(isset($_POST['sendSMS'])){

  //insert into database 
  $query = "INSERT INTO `message` 
  (
     `date`,
     `subject`,
     `group`,
     `message`
   )

VALUES

  (
    :date,
    :subject,
    :group,
    :message

  )";

  //Remember these three steps.  1.)Prepare, 2.)Bind, 3.)Execute

  $stmt = $db->prepare($query); //Prepare

  //Bind
  $stmt->bindParam(":date",    $_POST['date']);
  $stmt->bindParam(":subject", $_POST['subject']);
  $stmt->bindParam(":group",   $_POST['group']);
  $stmt->bindParam(":message", $_POST['message']);

  //Execute 
  $stmt->execute();


  echo "SMS sent successfully";

}

?>