-1

Can someone help, I cant seem to get the data colleted from the form to be sent to the mysql database.

I am very new to coding and I cant seem to figure out why the form data is not being sent to the mysql database table.

Please any help would be muchly appricated. once I press submit the page closes than refreshs without any errors, but the data has not been sent to the database table.

Please see code below.

<?php include'inc/header.php'; ?>
<div class="container">

    <center>
        <h2 style="color: #odc16f">Shipped</h2>
        <hr>
    </center>

    <center>
        <!-- Button trigger modal -->
        <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#addEmpModal">
            Add Order
        </button>
    </center>

    <!-- Modal -->
    <div class="modal fade" id="addEmpModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form action="" method="post">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria- hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">Add New Order</h4>
                    </div>
                    <div class="modal-body">

                        <div class="form-group">
                            <label>Enter Name</label>
                            <input class="form-control" type="text" name="customer" id="customer" placeholder="Enter Name">
                            <label id="lbcustomer" style="color:red"></label>
                        </div>

                        <div class="form-group">
                            <label>Enter Date</label>
                            <input class="form-control" type="date" name="date" id="date" placeholder="Enter Date">
                            <label id="lbdate" style="color:red"></label>
                        </div>

                        <div class="form-group">
                            <label>Enter Invoice</label>
                            <input class="form-control" type="number" name="invoice" id="invoice" placeholder="Enter Invoice">
                            <label id="lbinvoice" style="color:red"></label>
                        </div>


                        <div class="form-group">
                            <label>Enter eBay</label>
                            <input class="form-control" type="number" name="ebay" id="ebay" placeholder="Enter eBay">
                            <label id="lbebay" style="color:red"></label>
                        </div>

                        <div class="form-group">
                            <label>Enter Shipped</label>
                            <input class="form-control" type="text" name="shipper" id="shipper" placeholder="Enter Shipped">
                            <label id="lbshipper" style="color:red"></label>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary" id="save">Save changes</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

</div><!-- /.container ends here -->

<?php
    include'inc/footer.php';
?>
<script>
    $(document).ready(function() {
        $(document).on('click', '#save', function() {

            var customer = $("#customer").val();
            var date = $("#date").val();
            var invoice = $("#invoice").val();
            var ebay = $("#ebay").val();
            var shipper = $("#shipper").val();

            if (customer == "") {
                $("#lbcustomer").html("Enter Name");
            } else if (date == "") {
                $("#lbdate").html("Enter Date");
            } else if (invoice == "") {
                $("#lbinvoice").html("Enter Invoice");
            } else if (ebay == "") {
                $("#lbebay").html("Enter eBay");
            } else if (shipper == "") {
                $("#lbshipper").html("Enter Shipper");
            } else {
                $.ajax({
                    url: "save_data.php",
                    type: "post",
                    data: {
                        customer: customer,
                        date: date,
                        invoice: invoice,
                        ebay: ebay,
                        shipper: shipper
                    },
                    success: function(data) {
                        alert("Order Has Been Successful");
                        $("#addEmpModal").modal('hide');
                        location.reload();
                    }
                });
            }

        });
    });

</script>

Please see below the save_data.php code

<$php
include 'config/config.php';
global $con;

   $customer = $_POST['customer'];
   $date = $_POST['date'];
   $invoice = $_POST['invoice'];
   $ebay = $_POST['ebay'];
   $shipper = $_POST['shipper'];

$save_data = "INSERT INTO orders(customer, date, invoice, ebay, shipper)VALUES('$customer','$date','$invoice','$ebay','$shipper')";
$result = mysqli_query($con, $save_data);

and below is the config.php code.

<?php

$con = mysqli_connect("localhost","root","Password","shippedorders");
if (!$con) {
  echo "Failed to connect to MySQL: ".mysqli_connect_error($con);
  } 
MrPureleb
  • 11
  • 3
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 22 '19 at 08:57
  • 1
    Possible duplicate of [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Sep 22 '19 at 08:58
  • single quotes prevent `$` parsing `VALUES("$customer","$date"...` – suspectus Sep 22 '19 at 09:01
  • What is `<$php`? Is this a typo when posting here or is this what you have in your file? – Dharman Sep 22 '19 at 09:06
  • lol... rookie mistake, it was actually in my code and after I fixed it the problem was solved. thank you so much for pointing that out. – MrPureleb Sep 22 '19 at 09:54

1 Answers1

0

you are missing

action in :

<form action="save_data.php" method="post">

or are you running your php in same page as html ? or you