-1

I can't figure out why the form can't submit data to the MySQL database. I don't even get any errors, that would help me understand the problem. I tried to change the action method to GET, to see if the form accepts the data, and it does, the information gets displayed in the URL, but it doesn't insert anything in the database. Thanks.

This is the insert function (located in info_file.php):

function insert_records($employee_id_fk,$license_num, $expiry_date, 
                        $aquired_date, $penalty_point)
{
   global $connection_Var;
   mysqli_query($connection_Var,
        "INSERT INTO bus_driver  
                (`employee_id_fk`,`license_num`,`expiry_date`,
                 `aquired_date`,`penalty_point`)
        VALUES ('".$employee_id_fk."', '".$license_num."','".
                $expiry_date."','".$aquired_date."','".$penalty_point."')");
}

This is the code that will be executed by the form (lcoated in new_instance.php):

<?php 
    include 'info_file.php';
    Open_Connection(); 
    insert_records($_POST["employee_id_fk"],$_POST["license_num"],
                    $_POST["expiry_date"],$_POST["aquired_date"],
                    $_POST["penalty_point"]);
    header( 'Location:view.php');
    Close_Connection();
?>

This is the form:

<form action="new_instance.php" method="post">
             <p>Employee ID: <input type="text" name="employee_id_fk"></p>
            <p>License Number: <input type="text" name="license_num"></p>
            <p>Expiry date: <input type="text" name="expiry_date"></p>
            <p>Aquired data: <input type="text" name="aquired_date"></p>
            <p>Penalty point: <input type="text" name="penalty_point"></p>
            <input type="submit" value="Submit" />
           </form>

First time using stack overflow, so if sorry if there are mistakes in the formatting. Also, I don't if there is a way to attach the files (I guess there isn't for security purposes), as it is a project with multiple files, I had to take only the code that I think is the issue.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Rakib
  • 3
  • 4
  • Your code is very insecure. Use prepared statements, or at least escape the user input string... – Matthias Bö May 31 '18 at 17:39
  • 1
    You're not checking the return value of `mysqli_query`. If it fails, it will return `false`. And then you'd use `mysqli_error($connection_Var)` to see what the error was from the database. Note also that this code is wide open to SQL injection, which makes errors much more likely. – David May 31 '18 at 17:39
  • Please try to debug first - https://stackoverflow.com/q/22662488/296555 – waterloomatt May 31 '18 at 18:04
  • Could you add these two lines at the top of your script and see if it throws an error? `error_reporting(E_ALL); ini_set('display_errors', 1);` – rpm192 May 31 '18 at 18:57

1 Answers1

-2

Try printing an error message with mysqli_error($connection_Var); http://php.net/manual/en/mysqli.error.php

In my experience, usually the table doesn't exist, the syntax is wrong, or the user doesn't have the appropriate permissions. I'd also recommend you use PHP PDO for database connections, it's more robust and MySQLi is a bit archaic. http://php.net/manual/en/book.pdo.php

Anch0rman
  • 85
  • 4
  • Hi, I found the problem. Apparently, it was Wampserver, as after a clean installation the data from the form have been accepted. I know MySQLi is old, this is just a school project, so I don't need anything professional. Also, we didn't learn that yet. Anyway, thanks for the help, I will keep in mid how to display the error message. – Rakib Jun 01 '18 at 09:22