0
<form name="applyform" action="applyform.php" method="post">
    <fieldset>
        <legend>Application Details</legend>
        <p>Name :<?php echo $row ["Emp_Fname"]; ?></p>
        <p>ID number :<?php echo $row['Emp_ID']; ?></p>
        <p>Email :<?php echo $row['Emp_Email']; ?></p>
        <p>Address :<?php echo $row['Emp_Address']; ?></p>
        <p>Handphone Number :<?php echo $row['ContactNo_HP']; ?></p>
        <p>Phone Number :<?php echo $row['ContactNo_Home']; ?></p>
        <p>Date of application :<?php echo $row['Leave_RequestDate']; ?></p>
        <p>Type of leave:
           <select name="leave type">
              <option selected>Annual leave</option>
              <option>Sick leave</option>
              <option>Emergency leave</option>
              <option>Maternity leave</option>
            </select>
        </p>
        <p>Leave duration:<input type="date" name="leave_start">to<input type="date" name="leave_end"></p>
        <p>Reason:<textarea rows="4" cols="50" name="reason"></textarea></p>
        <p><input type="submit" name="submitbtn" value="Submit"/>

This is the code of my form. Is there anything wrong?

<?php

if(isset($_POST['submitbtn'])) {

  if(!$con)  {
    die("cannot connect : " .mysql_error());
  }
  $sql = ("INSERT INTO leave(Leave_Start,Leave_End,Leave_Reason)    VALUES('$_POST[leave_start]','$_POST[leave_end]','$_POST[reason]')");
  mysql_query($sql,$con);

  mysql_close($con);
}
?>  

The above is my PHP code. When I try submitting the form the database just won't update, can anyone help me?

Angel M.
  • 2,692
  • 2
  • 32
  • 43

5 Answers5

0

Did you forget to include a mysql_connect around line 5 of your PHP code?

$con = mysql_connect('mysql_host', 'mysql_user', 'mysql_password');
Brian Showalter
  • 4,321
  • 2
  • 26
  • 29
0

Instead of

$sql = ("INSERT INTO leave(Leave_Start,Leave_End,Leave_Reason) VALUES('$_POST[leave_start]','$_POST[leave_end]','$_POST[reason]')");

try

$sql = ("INSERT INTO leave(Leave_Start,Leave_End,Leave_Reason) VALUES('".$_POST["leave_start"]."','".$_POST["leave_end"]."','".$_POST["reason"]."')");

notice : doing a query like that is not secure as the $_POST is not check before

something like this should be better

$sql = ("INSERT INTO leave(Leave_Start,Leave_End,Leave_Reason) VALUES('".mysqli_real_escape_string($con,$_POST["leave_start"])."','".mysqli_real_escape_string($con,$_POST["leave_end"])."','".mysqli_real_escape_string($con,$_POST["reason"])."')");
0

You need to understand little bit about PHP variable passing and SQL injection. You final query must look like

 $sql = ("INSERT INTO leave(Leave_Start,Leave_End,Leave_Reason) VALUES('{".mysqli_real_escape_string($_POST['leave_start'])."}','{".mysqli_real_escape_string($_POST['leave_end'])."}','{".mysqli_real_escape_string($_POST['reason'])."}')");
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
0

You can try MySQLi instead of predicated MySQL.

<?php

if(isset($_POST['submitbtn']))
{

$connection=mysqli_connect("Host","Username","Password","Database");

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

mysqli_query($connection,"INSERT INTO leave(Leave_Start, Leave_End, Leave_Reason)    VALUES('$_POST[leave_start]','$_POST[leave_end]','$_POST[reason]')");

}
?>  
-1

Your insert is incorrect:

$sql = ("INSERT INTO leave(Leave_Start,Leave_End,Leave_Reason)   VALUES('$_POST[leave_start]','$_POST[leave_end]','$_POST[reason]')");

should be this:

$sql = ("INSERT INTO leave(Leave_Start,Leave_End,Leave_Reason)    VALUES('{$_POST["leave_start"]}','{$_POST["leave_end"]}','{$_POST["reason"]}')");

You should always escape special characters in a string. Otherwise they get misinterpreted.

http://php.net/manual/en/language.types.string.php

by the way, I am assuming that $con is a valid connection object.

ferd tomale
  • 845
  • 7
  • 20