1

This is my table html code. I tried sending the data using the normal insert but it only sends the last row data. I don't know how to send the full data . Can someone please help me with this.

<form action="admin_schedule_employee.php" id="schedule_employee" method="post" >
           <input type="date" class="input-sm" name="scheduledate" style="margin:10px;">
           <table class="table-responsive table table striped table-bordered">
                <thead>
                    <tr>
                        <th  style="width:20%">Employee First Name</th>
                        <th  style="width:20%">Employee ID</th>
                        <th  style="width:20%">Start Time</th>
                        <th  style="width:20%">End Time</th>                            
                    </tr>
                    <?php while($row = mysqli_fetch_array($search_result)): ?>
                    <tr>
                       <td><input disabled name="employeename" type="text" value="<?php echo $row['fname']; ?>"></input></td>
                       <td><input disabled name="employeeid"   type="number" value="<?php echo $row['employee_id']; ?>"></input></td>
                       <td><input name="starttime" type="time"></td>
                        <td><input name="endtime" type="time"></td>
                    </tr>
                    <?php endwhile; ?>
                </thead>
                <tbody>
               </tbody>
            </table>    
            <input type="submit" name="Schedule" value="Schedule">              
         </form>[This is how my table look like i want to send the whole data to sql database using php][1]
2
  • The thing is that you create inputs with same name -> that is the reason why you are getting last row. You need to make names of the inputs different. For example, add some variable which is incremented in every loop run. Commented Feb 15, 2017 at 14:41
  • You should probably share the SQL query you are using. The problem is likely there. Also, you aren't inserting html into a database, you're retrieving database data and constructing a html table. Commented Feb 15, 2017 at 15:07

1 Answer 1

1

To start with, you will need to create multiple pages:

form.php
process.php
done.php

Creating your user form is simple, place the table in form tags like you have done above, here is an example. Save this page as form.php

<form id="new record" action="process.php" method="POST">
    <table width="500px">
        <tr>
            <td width="50%">
                <input type="text" name="fname" id="fname">
            </td>
            <td width="50%">
                <input type="text" name="lname" id="lname">
            </td>
        </tr>
        <tr>
            <td width="50%">
            </td>
            <td width="50%">
                <input type="submit" value="Add Record">
            </td>
        </tr>
    </table>
</form>

Next, you will need to create a page which can process this data, and add it to your mysql database. For the following example, I have omitted my database details and substituted them, but you should add your own.

For this example, imagine my database has a table with only an fname and an lname column.

<meta http-equiv="refresh" content="0; url=/done.php" />
<?php
    $servername = "your_server_name";
    $username = "mysql_username";
    $password = 'mysql_password';
    $dbname = "database_name";
    $fname = $_GET['fname'];
    $lname = $_GET['lname'];


    try {
        $conn = new PDO("mysql:host=$servername; dbname=$dbname", $username,    $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "INSERT INTO online (fname, lname)
        VALUES ('$fname', '$lname')";
        // use exec() because no results are returned
        $conn->exec($sql);
        echo "New record inserted";
    }
    catch(PDOException $e)
    {
        echo $sql . "<br>" . $e->getMessage();
    }

    $conn = null;

?>

Hopefully, that will work to insert the record. Now we need a table on the done.php page which can display all the records in the database. Use the following code:

<html lang="en">
<head>
    <meta http-equiv="refresh" content="5; url=/done.php" />
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <?php
        $servername = "your_server_name";
        $username = "mysql_username";
        $password = 'mysql_password';
        $dbname = "database_name";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "SELECT * from table_name";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            echo $row["fname"]. ": &nbsp;";
            echo $row["lname"]. "<br /><br />";
        }
     } else {
        echo "No messages";
    }

    mysqli_close($conn);
?>
</body>
</html>

Hopefully this will work for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.