Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

Can't add data through PHP and MySQL

Validate function

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;
}

EDITED CODE

<?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");
            }
    }
    }
    ?>

EDITED CODE 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>

I have a php code that take values from a form and add them into MySQL database. First part of the PHP code, see if the admin choose an already exist patient from drop list, then add a date and time of an appointment with a reason.

Then values are posted into PHP code where we see if we have already an appointment in those date and time. If not ($appExistStmtCount == 0) then go and insert an appointment.

The problem is that nothing added to database and can't see any PHP errors echoed.

Here is the PHP code:

<?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");
            }
    }
    }
    ?>

And here the HTML form:

<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="f

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your form has no method, so it's passing data through get. You need to add method="post" to your form.

Edit. As @u_mulder mentioned, you need to add name attribute to your button for the check in your php if the button is clicked.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...