【问题标题】:PHP not sending data to phpmyadmin [closed]PHP没有向phpmyadmin发送数据[关闭]
【发布时间】:2019-06-26 20:35:04
【问题描述】:

我正在创建一个网站,并且想将数据发送到 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 }} ?>

【问题讨论】:

  • 日志是你的朋友。他们通常会告诉您什么不起作用以及确切的位置。否则我们只是在猜测浪费时间。什么不具体?
  • phpMyAdmin 是用 PHP 编写的 MySQL 管理工具,它本身不是数据库。您可能正在使用 MySQL 或 MariaDB 作为数据库。
  • 提交数据后未到达数据库
  • 你可以试试一个叫做橡皮鸭调试的电脑软件调试系统en.wikipedia.org/wiki/Rubber_duck_debugging

标签: php mysql


【解决方案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>

【讨论】:

  • Fatal error: Uncaught Error: Call to a member function prepare() on null in X:\OSPanel\domains\kajaki2.com\package-details.php:12 Stack trace: #0 {main} thrown in X:\OSPanel\domains\kajaki2.com\package-details.php on line 12 试过这个stackoverflow.com/questions/28592400/… 但不起作用
  • 我已经编辑了代码以包含我使用的数据库连接详细信息,与 pastebin 中代码的唯一区别是数据库名称 - 测试,并且我在连接中包含了一个端口号多个 MySQL 在本地运行。
  • 仍然存在问题
  • 你有什么问题?
  • 值未出现在数据库中
猜你喜欢
  • 2018-10-23
  • 2013-12-24
  • 2016-03-23
  • 1970-01-01
  • 2013-04-13
  • 2014-10-19
  • 2016-03-08
  • 1970-01-01
  • 2016-04-07
相关资源
最近更新 更多