-1
<?php

session_start();
$host = "localhost";
$user = "root";
$password = "";
$database = "tuition";
$conn = mysqli_connect($host, $user, $password, $database);

?>

<div role="tabpanel" class="tab-pane fade in" id="sign_up">
  <form action="" method="post">
    <input type="text" class="form-control" name="register_name" placeholder="Name" required />
    <input type="text" class="form-control" name="register_ic" placeholder="IC" required />
    <input type="telephone" class="form-control" name="register_phone" placeholder="Telephone" required />
    <input type="email" class="form-control" name="register_email" placeholder="Email" required />
    <input type="password" class="form-control" name="register_password" placeholder="Password" required />
    <input type="submit" class="form-control" name="register_btn" value="Sign up" />
  </form>
</div>

<?php

if(isset($_POST['register_btn'])){
  $name = $_POST['register_name'];
  $ic = $_POST['register_ic'];
  $phone = $_POST['register_phone'];
  $email = $_POST['register_email'];
  $password = md5($_POST['register_password']);
  $result = mysqli_query($conn,"select parent.parent_ic from parent where parent_ic ='$ic'");

  if(mysqli_num_rows($result)!=1){
    mysqli_query($conn,"insert into parent(parent_name,parent_ic,parent_email,parent_contact_num,parent_password) values ('$name','$ic','$email','$phone','$password')");

?>
    <script type="text/javascript">
      alert("success");
    </script>

<?php

  }
  else{

?>
    <script type="text/javascript">
      alert("fail");
    </script>
<?php

  }
}

?>

It coming out with alert success, but it do not insert the data into the database.But when I hard code insert the data into the database it work! Why?

Here is the result after I click submit button:

here is the result after I click submit button

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
tRex379
  • 3
  • 6
  • Essential: [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Lawrence Cherone Jul 15 '18 at 04:43
  • Don't use `MD5` for encryption! Use [password_hash](http://php.net/manual/en/function.password-hash.php) to do the encryption! And use [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) to pevent SQL injection! – Ivan86 Jul 15 '18 at 04:53
  • I recommend using PDO over MySQLi, also the result will not be equal to 1 if it fails, instead use `If (mysqli_num_rows($result) > 0)` –  Jul 15 '18 at 20:49

1 Answers1

0

You should see by printing error after insert data query

echo("Error description: " . mysqli_error($conn));

I think your MySQL error will be there.

  • ok, I think I know the problem. I am adding the name as ''H'ng" because of ' the name cannot insert into the database xD – tRex379 Jul 18 '18 at 14:13