无法通过PHP和MySQL添加数据

时间:2016-03-05 08:17:35

标签: php mysql

验证功能

function validate(add_app_form){
    var valid = true;
    var userTxt = document.getElementById("patient_name").value;
    var dateTxt = document.getElementById("app_date").value;
    var timeTxt = document.getElementById("app_time").value;
    var oldName = document.getElementById("select_old").value;
    if(userTxt == "" && dateTxt == "" && timeTxt == "" && oldName == "choose")
    {
      //$("#lblTxt").text("Username and Password are required!");
      $('#patient_name').css('border-color', 'red');
      $('#app_date').css('border-color', 'red');
      $('#app_time').css('border-color', 'red');
      $('#select_old').css('border-color', 'red');
      $("#add_app_lbl").text("Please Fill all the form");
      valid = false;
    }
    if(userTxt == "" && oldName == "choose")
    {
      $('#patient_name').css('border-color', 'red');
      $("#add_app_lbl").text("Please Add Patient Name Or select an old patient");
      valid = false;
    }
    if(dateTxt == "")
    {
      $('#app_date').css('border-color', 'red');
      $("#add_app_lbl").text("Please Add a Date");
      valid = false;
    }
    return valid;
}

已编辑的代码

<?php
    //Set error reporting on
    error_reporting(E_ALL);
    ini_set("display_errors", 1);

    //Include connection file
    require_once('../include/global.php');


    $user = $_SESSION['username'];
    $id_logged = $_SESSION['login_id'];

    if(isset($_POST['add_app_btn'])){
    //Values From AJAX
        $patient_name = $_POST['patient_name'];
        $date_app = $_POST['app_date'];
        $time_app = $_POST['app_time'];
        $reason = $_POST['app_reason'];
        $old_patient_id = $_POST['select_old'];

        //If new patient
        if($patient_name == "" && $old_patient_id != "choose")
        {
            try{    
        //See if date and time exist
                $appExist = "SELECT * FROM appointment WHERE id_logged = :id_logged AND date_app = :date_app and time_app = : time_app";
                $appExistStmt = $conn->prepare($appExist);
                $appExistStmt->bindValue(":id_logged", $id_logged);
                $appExistStmt->bindValue(":date_app", $date_app);
                $appExistStmt->bindValue(":time_app", $time_app);
                $appExistStmt->execute();
                $appExistStmtCount = $appExistStmt->rowCount();

                if($appExistStmtCount == 0)
                    {
                        //Add to appointment table
                        $appAdd = "INSERT INTO appointment(id_logged, patient_id, date_app, time_app, reason)
                                   VALUES(:id_logged, :patient_id, :date_app, :time_app, :reason)";
                        $appAddStmt = $conn->prepare($appAdd);
                        $appAddStmt->bindValue(":id_logged", $id_logged);
                        $appAddStmt->bindValue(":patient_id", $old_patient_id);
                        $appAddStmt->bindValue(":date_app", $date_app);
                        $appAddStmt->bindValue(":time_app", $time_app);
                        $appAddStmt->bindValue(":reason", $reason);

                        $appAddStmt->execute();

                        echo "added";
                    }
                    else
                    {
                        echo "not added";
                        header("Location: add_appoint.php");
                    }
                }

            catch(PDOException $m)
            {
                $m->getMessage();
                echo "error";
                header("Location: add_app_btnoint.php");
            }
    }
    }
    ?>

已编辑的代码2

<form class="form-horizontal" id="add_app_form" method="post" action="add_appoint.php" onSubmit="return validate(this);">
                  <div class="box-body">
                    <div class="form-group">
                      <label for="patient_name" class="col-sm-3 control-label">Old Patient</label>
                      <div class="col-sm-4">
                        <select id="select_old" name="select_old">
                          <option value="choose">Choose Name</option>
                          <?php foreach($name_array as $na) { ?>
                          <option value="<?php echo $na['id'] ?>"><?php echo $na['patient_name'] ?></option>
                          <?php } ?>
                        </select>
                      </div>
                      <label for="patient_name" class="col-sm-1 control-label">New</label>
                      <div class="col-sm-4">
                        <input type="text" class="form-control" id="patient_name" name="patient_name" placeholder="New Patient Name">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="app_date" class="col-sm-2 control-label">Date</label>
                      <div class="col-sm-4">
                        <input type="date" class="form-control" id="app_date" name="app_date">
                      </div>
                      <label for="app_time" class="col-sm-2 control-label">Time</label>
                      <div class="col-sm-4">
                        <input type="time" class="form-control" id="app_time" name="app_time">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="app_reason" class="col-sm-2 control-label">Reason</label>
                      <div class="col-sm-10">
                        <textarea class="form-control" id="app_reason" name="app_reason" placeholder="Reason"></textarea>
                      </div>
                    </div>
                  </div><!-- /.box-body -->
                  <div class="box-footer">
                    <button type="submit" id="add_app_btn" name="add_app_btn" class="btn btn-success pull-right">Add Appointment</button>
                  </div><!-- /.box-footer -->
                </form>

我有一个PHP代码,它从表单中获取值并将它们添加到MySQL数据库中。 PHP代码的第一部分,查看管理员是否从下拉列表中选择已存在的患者,然后添加约会的日期和时间以及原因。

然后将值发布到PHP代码中,我们会看到我们是否已在这些日期和时间中预约。如果不是($appExistStmtCount == 0),那么去插入约会。

问题是没有任何内容添加到数据库中,并且看不到任何PHP错误。

这是PHP代码:

<?php
    //Set error reporting on
    error_reporting(E_ALL);
    ini_set("display_errors", 1);

    //Include connection file
    require_once('../include/global.php');


    $user = $_SESSION['username'];
    $id_logged = $_SESSION['login_id'];

    if(isset($_POST['add_app_btn'])){
    //Values From AJAX
        $patient_name = $_POST['patient_name'];
        $date_app = $_POST['app_date'];
        $time_app = $_POST['app_time'];
        $reason = $_POST['app_reason'];
        $old_patient_id = $_POST['select_old'];

        //If new patient
        if($patient_name == "" && $old_patient_id != "choose")
        {
            try{    
        //See if date and time exist
                $appExist = "SELECT * FROM appointment WHERE id_logged = :id_logged AND date_app = :date_app and time_app = : time_app";
                $appExistStmt = $conn->prepare($appExist);
                $appExistStmt->bindValue(":id_logged", $id_logged);
                $appExistStmt->bindValue(":date_app", $date_app);
                $appExistStmt->bindValue(":time_app", $time_app);
                $appExistStmt->execute();
                $appExistStmtCount = $appExistStmt->rowCount();

                if($appExistStmtCount == 0)
                    {
                        //Add to appointment table
                        $appAdd = "INSERT INTO appointment(id_logged, patient_id, date_app, time_app, reason)
                                   VALUES(:id_logged, :patient_id, :date_app, :time_app, :reason)";
                        $appAddStmt = $conn->prepare($appAdd);
                        $appAddStmt->bindValue(":id_logged", $id_logged);
                        $appAddStmt->bindValue(":patient_id", $old_patient_id);
                        $appAddStmt->bindValue(":date_app", $date_app);
                        $appAddStmt->bindValue(":time_app", $time_app);
                        $appAddStmt->bindValue(":reason", $reason);

                        $appAddStmt->execute();

                        echo "added";
                    }
                    else
                    {
                        echo "not added";
                        header("Location: add_appoint.php");
                    }
                }

            catch(PDOException $m)
            {
                $m->getMessage();
                echo "error";
                header("Location: add_app_btnoint.php");
            }
    }
    }
    ?>

这里是HTML表单:

<form class="form-horizontal" id="add_app_form" onSubmit="return validate(this);">
                  <div class="box-body">
                    <div class="form-group">
                      <label for="patient_name" class="col-sm-3 control-label">Old Patient</label>
                      <div class="col-sm-4">
                        <select id="select_old" name="select_old">
                          <option value="choose">Choose Name</option>
                          <?php foreach($name_array as $na) { ?>
                          <option value="<?php echo $na['id'] ?>"><?php echo $na['patient_name'] ?></option>
                          <?php } ?>
                        </select>
                      </div>
                      <label for="patient_name" class="col-sm-1 control-label">New</label>
                      <div class="col-sm-4">
                        <input type="text" class="form-control" id="patient_name" name="patient_name" placeholder="New Patient Name">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="app_date" class="col-sm-2 control-label">Date</label>
                      <div class="col-sm-4">
                        <input type="date" class="form-control" id="app_date" name="app_date">
                      </div>
                      <label for="app_time" class="col-sm-2 control-label">Time</label>
                      <div class="col-sm-4">
                        <input type="time" class="form-control" id="app_time" name="app_time">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="app_reason" class="col-sm-2 control-label">Reason</label>
                      <div class="col-sm-10">
                        <textarea class="form-control" id="app_reason" name="app_reason" placeholder="Reason"></textarea>
                      </div>
                    </div>
                  </div><!-- /.box-body -->
                  <div class="box-footer">
                    <button type="submi;" id="add_app_btn" class="btn btn-success pull-right">Add Appointment</button>
                  </div><!-- /.box-footer -->
                </form>

PS

可以在URL中看到值,但页面只是刷新而没有添加任何内容

1 个答案:

答案 0 :(得分:1)

您的表单没有方法,所以它通过get传递数据。您需要在表单中添加> baskets2incidence(x) 4 x 9 sparse Matrix of class "dgCMatrix" fruit semi-finished bread margarine ready soups yogurt coffee whole milk [1,] 1 1 1 1 . . . [2,] 1 . . . 1 1 . [3,] . . . . . . 1 [4,] 1 . . . 1 . . cream cheese meat spreads [1,] . . [2,] . . [3,] . . [4,] 1 1

编辑。正如@u_mulder所提到的,你需要在你的按钮中添加name属性,以便在点击按钮时检查你的php。