PHP未将数据发送到phpmyadmin

时间:2019-06-26 20:35:04

标签: php mysql

我正在创建一个网站,希望将数据发送到MySQL,但没有发送。

如有需要,我可以提供更多详细信息。

完整代码https://pastebin.com/JW5UUQbt

if(isset($_POST['submit2']))
{
$pid=intval($_GET['pkgid']);
$useremail=$_SESSION['login'];
$date=$_POST['date'];
$time=$_POST['time'];
$comment=$_POST['comment'];
$status=0;
$sql="INSERT INTO tblbooking (PackageId, UserEmail, BDate, BTime, Comment, status) VALUES (:pid, :useremail , :bdate , :btime , :comment , :status);";
$query = $dbh->prepare($sql);
$query->bindParam(':pid',$pid,PDO::PARAM_STR);
$query->bindParam(':useremail',$useremail,PDO::PARAM_STR);
$query->bindParam(':bdate',$date,PDO::PARAM_STR);
$query->bindParam(':btime',$time,PDO::PARAM_STR);
$query->bindParam(':comment',$comment,PDO::PARAM_STR);
$query->bindParam(':status',$status,PDO::PARAM_STR);
$query->execute();
?>
<form name="book" method="post">
                <label class="inputLabel">Date</label>
                <input type="text" name="date" required="">
            <div class="bnr-right">
                <label class="inputLabel">Time</label>
                <input type="text" name="time" required="">
            </div>
                        <label class="inputLabel">Comment</label>
                        <input class="special" type="text" name="comment">
                    </li>
                    <?php if($_SESSION['login'])
                    {?>
                        <li class="spe" align="center">
                    <button type="submit" name="submit2" class="btn-primary btn">Book</button>
                        </li>
                        <?php } else {?>
                            <li class="sigi" align="center" style="margin-top: 1%">
                            <a href="#" data-toggle="modal" data-target="#myModal4" class="btn-primary btn" > Book</a></li>
                            <?php } ?>
            </div>

        </div>
        </form>
<?php }} ?>

1 个答案:

答案 0 :(得分:1)

我稍微简化了代码(包括对$ useremail和$ pid的值进行硬编码)以识别可能导致此问题的原因,并且它在以下情况下首次起作用:

<?php 
  // DB credentials.
  define('DB_HOST','localhost');
  define('DB_USER','root');
  define('DB_PASS','');
  define('DB_NAME','test');
  // Establish database connection.
 try
  {
    $dbh = new PDO("mysql:host=".DB_HOST.";port=3307;dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
  }
 catch (PDOException $e)
  {
      exit("Error: " . $e->getMessage());  
   }
?>

<?php

 if(isset($_POST['submit2']))
  {
    $pid=6;
    $useremail= "hello@domain.com";
    $date=$_POST['date'];
    $time=$_POST['time'];
    $comment=$_POST['comment'];
    $status=0;
    $sql="INSERT INTO tblbooking (PackageId, UserEmail, BDate, BTime, Comment, status) VALUES (:pid, :useremail , :bdate , :btime , :comment , :status);";
    $query = $dbh->prepare($sql);
    $query->bindParam(':pid',$pid,PDO::PARAM_STR);
    $query->bindParam(':useremail',$useremail,PDO::PARAM_STR);
    $query->bindParam(':bdate',$date,PDO::PARAM_STR);
    $query->bindParam(':btime',$time,PDO::PARAM_STR);
    $query->bindParam(':comment',$comment,PDO::PARAM_STR);
    $query->bindParam(':status',$status,PDO::PARAM_STR);
    $query->execute();
  }
?>

<form name="book" method="post">
     <label class="inputLabel">Date</label>
     <input type="text" name="date" required="">
     <div class="bnr-right">
        <label class="inputLabel">Time</label>
        <input type="text" name="time" required="">
     </div>
     <label class="inputLabel">Comment</label>
     <input class="special" type="text" name="comment">
      <button type="submit" name="submit2" class="btn-primary btn">Book</button>          
 </form>