0

I know next to nothing about PHP and most of this code is how my instructor taught me. I'm using vertrigo server, which is running, however when I submit the data it says 'not inserted', I'm lost, what should I change in my code? or have I set up the database incorrectly? Please help.

<html>
<head>

<title></title>
</head>

<body>

<form name="employees" action="" method="POST">
<table border="1" align="center">
<tr>
<td> Employee ID </td>
<td> <input type="text" name="id">
</td>
</tr>
<tr>
<td> Status </td>
<td> Senior Manager <input type="radio" name="stat" value="Senior Manager">Manager<input type="radio" name="stat" value="Manager"> Senior Executive<input type="radio" name="stat" value="Senior Executive" >
</td>
</tr>
<tr>
<td> Salary </td>
<td> <select name="salary"> <option value="below 20,000">below 20,000</option> <option value="20,000-40,000">20,000-40,000</option> <option value="above 40,000">above 40,000</option> </select>
</td>
</tr>
<tr>
<td> Gender </td>
<td> Male <input type="radio" name="gender" value="Male"> Female <input type="radio" name="gender" value="Female">
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Enter Record">
</td>
</tr>
</table>
</form>
</body>
</html>

<?php
$con = mysqli_connect("localhost", "root", "vertrigo", "employeedb");

if (isset($_POST['submit']))
    {
    $id = $_POST['id'];
    $stat = $_POST['stat'];
    $salary = $_POST['salary'];
    $gender = $_POST['gender'];
    $sql = "INSERT INTO employeetb(EmployeeID,Status,Salary,Gender)VALUES('$id','$stat','$salary','$gender')";
    $query = mysqli_query($con, $sql);
    if (!$query)
        {
        echo 'not inserted';
        }
      else
        {
        echo 'inserted';
        }
    }

?>
Hedam
  • 2,209
  • 27
  • 53
  • 1
    At a first glance, your code looks good. To output the reason for the error, insert the following line after `echo 'not inserted`; `printf("Errormessage: %s\n", mysqli_error($con));` – Mario A Mar 13 '17 at 20:12
  • Thanks a lot, this code helped helped me figure out an error in the database. Problem solved! – Waleed Ibrahim Mar 14 '17 at 21:22

2 Answers2

0

Maybe you can try use mysqli_real_escape_string in the $_POST value before inserting into the database. Something like:-

    $salary= mysqli_real_escape_string($con, $_POST['salary']);

Check out this docx

Hafiz K
  • 100
  • 1
  • 9
-1

put some spaces in your sql line

you have this

$sql="INSERT INTO employeetb(EmployeeID,Status,Salary,Gender)VALUES('$id','$stat','$salary','$gender')";

try this

$sql="INSERT INTO employeetb (EmployeeID,Status,Salary,Gender) VALUES ('$id','$stat','$salary','$gender')";
ATechGuy
  • 1,240
  • 8
  • 13