简体   繁体   中英

Need help submitting my form data into database

OK, let me start off by saying im an amateur, so all your help is really appreciated.

OK, I have a form, called 'skills.php' and on this form, you enter the following fields 'Skills_ID, Employee_ID, First_name, Last_name and Skill'. I have used java so when you select employee_ID, the name fields change to what employee that is (linked to employee table).

HOWEVER, since i have added this function, i can not save my form data into my database. Maby i accidently deleted a line of code when implementing the java function. Can someone help me figure it out? below is my form 'Skill.php':

<html>

<?php
// Connecting to database
$pdo = new PDO("mysql:host=localhost;dbname=hrmwaitrose;charset=utf8", "root", "");
?>
<html>
<head>
        <link type="text/css" rel="stylesheet" href="style.css"/>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <!-- Online Jquery -->
        <title>Skill</title>
    </head>

    <body>
        <div id="content">
            <h1 align="center">Add Employee Skill</h1>

            <form action="insertskill.php" method="post">
                <div>
                    <p>
                        Skill ID:
                        <input type="text" name="Training_ID">
                    </p>
                    <p>
                        Employee ID:
                        <select id="Employee_ID">
                            <option value="">Select one</option>
                            <?php
                            $st = $pdo->prepare("SELECT Employee_ID FROM Employee");
                            $st->execute();
                            $rows = $st->fetchAll(PDO::FETCH_ASSOC);
                            foreach ($rows as $row) {
                                ?><option value="<?php echo $row ['Employee_ID']; ?>"><?php echo $row ['Employee_ID']; ?></option><?php
                            }
                        ?>
                        </select>
                    <p>
                        First name:
                        <input type="text" name="First_name" id="First_name">
                    </p>
                    <p>
                        Last name:
                        <input type="text" name="Last_name" id="Last_name">
                    </p>
                    <p>
<p>Skill: <select name="Skill">
  <option value="">Select...</option>
  <option value="Checkouts">Checkouts</option>
  <option value="Fresh goods">Fresh goods</option>
  <option value="Dry goods">Dry goods</option>
  <option value="Fruit & Veg">Fruit & Veg</option>
  <option value="Operations">Operations</option>
</select>
</p>
                    <input type="submit">
                    <INPUT Type="BUTTON" VALUE="Back" ONCLICK="window.location.href='index.html'">
            </form>
        </div>
    <script type="text/javascript">
        $(function() { // This code will be executed when DOM is ready
            $('#Employee_ID').change(function() { 
                var $self = $(this); // jQuery object with the select inside
                $.post("insertskill.php", { Employee_ID : $self.val()}, function(json) {
                    if (json && json.status) {
                        $('#First_name').val(json.name);
                        $('#Last_name').val(json.lastname);
                    }
                })
            });
        })
    </script>
    </body>
</html>

And here is the code used when the submit button is pressed 'insertskill.php':

<?php
$pdo = new PDO("mysql:host=localhost;dbname=hrmwaitrose;charset=utf8", "root", "");

header("Content-Type:application/json; Charset=utf-8");


$st = $pdo->prepare("SELECT First_name, Last_name FROM Employee WHERE Employee_ID = :employee_id");
$st->execute(array ('employee_id' => $_POST['Employee_ID']));
$data = $st->fetch(PDO::FETCH_ASSOC);

echo json_encode(array ('status' => true, 'name' => $data ['First_name'], 'lastname' => $data ['Last_name']));
?>

Just by looking at this code imn pretty sure i might of accidently deleted the coding to insert it into database, HOWEVER i dont know how to fix it :( can someone help? much appreciated! Thanks in advance

in "insertskill.php" line 6:

$st = $pdo->prepare("SELECT First_name, Last_name FROM Employee WHERE Employee_ID = :employee_id");

You're performing a SELECT query (read), sounds to me more like you wanted an INSERT query (write).

An INSERT statement in your case would be something similar to:

$st = $pdo->prepare("INSERT INTO Employee (Training_Id,Employee_Id,First_Name,Last_Name,Skill) VALUES (:training_id,:employee_id,:first_name,:last_name,:skill)")

$st->execute(array(':training_id' => $training_id, ':employee_id' => ...));

First, watch out. Java is not JavaScript !

And you're right, you removed the INSERT lines. There are several articles on the web about how to do that. Here is one . Just google PDO Insert for more results.

You should put these INSERT commands in your insertskill.php file and create another one, maybe fetchName.php or something like that, with the code that you presented to return first and last name for an employee based on its id, and use that in your JavaScript $.change() function. It doesn't make sense for a file called insertskill.php to fetch data.

And your forgot to specify that you're expecting a JSON in your $.post() command. Do that instead:

$.post("insertskill.php", { Employee_ID : $self.val()}, function(json) {
    if (json && json.status) {
        $('#First_name').val(json.name);
        $('#Last_name').val(json.lastname);
    }
}, 'json'); // Here!

First thing to say is that you are NOT using Java. You are using JavaScript (actually ECMAScript which is VERY VERY different from Java.

Next, you are correct, that there is now no INSERT statement there at all, you will need to write one to insert whatever data you want to insert.

HOWEVER, you also need to somehow tell the difference between when insertskill.php is called for an Ajax request (to get the names for the form) and when it is called to insert the data. So on your Javascript (jQuery I guess) request, you could change the url to:

$.post("insertskill.php?ajax"

and then in insertskill.php do:

if(isset($_GET['ajax'])) {
  header("Content-Type:application/json; Charset=utf-8");
  $st = $pdo->prepare("SELECT First_name, Last_name FROM Employee WHERE Employee_ID = :employee_id");
  $st->execute(array ('employee_id' => $_POST['Employee_ID']));
  $data = $st->fetch(PDO::FETCH_ASSOC);
  echo json_encode(array ('status' => true, 'name' => $data ['First_name'], 'lastname' => $data ['Last_name']));
  } 
else {
  // Do the insert statement stuff...
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM