0

Sorry. There's another malay language in my code..Hope u understand by looking at the code only..The problem is, if i enter any error input, the error is not display. And if there is no any error,it won't inserted in mysql. Thanks...

<?php
include ("connection.php");

$namaErr = $noicErr = $katalaluanErr = "";

if (isset($_POST['btnSubmit'])) {
    include ("connection.php");

    if (empty($_POST[$nama])) {
        $namaErr = "Anda wajib mengisi nama anda di ruangan ini!";
    }else if(strlen($nama) < 3) {
        $error = true;
        $namaErr= "Nama hendaklah tidak kurang daripada 3 karakter";
    } else if (!preg_match("/^[a-zA-Z ]+$/",$nama)) {
        $error = true;
        $namaErr= "Nama mestilah mengandungi huruf dan ruang sahaja";
    }

    //ic
    if (empty($_POST[$noic])) {
        $noicErr = "Anda wajib mengisi nombor IC anda di ruangan ini!";
    }else if (strlen($noic) != 12) {
        $error = true;
        $noicErr = "Nombor IC hendaklah tidak kurang daripada 12 nombor    
        digit";
    } else if (!is_numeric($noic)) {
        $error = true;
        $noicErr = "Nombor IC mestilah mengandungi nombor sahaja";
    } 

    //katalaluan
    if (empty($_POST[$katalaluan]))  {
        $katalaluanErr = "Anda wajib mengisi kata laluan anda di    
        ruangan ini!";
    } else if(strlen($katalaluan) < 8) {
        $error = true;
        $katalaluanErr = "Kata laluan hendaklah tidak kurang daripada 8";
    } 
}
if (!$error){ 
    $EncryptPassword = md5($katalaluan);

    $query="INSERT INTO daftar_pengguna(nama, noic, katalaluan)  
            VALUES('$nama','$noic', '$EncrpytPassword')";
    $res = mysql_query($query);
}
?>

And below is the form.

<form name="form1" method="post" action="borangdaftar.php">

    <table width="544" border="1">
    <tr>
         <th scope="row"><span class="style32">Nama</span></th>
         <td><input name="nama" type="text" size="30" maxlength="50" value=" 
<?php echo htmlspecialchars($nama); ?>">*<span class="isa_error"><? 
         php echo $nama; ?></span></td>
</tr>
<tr>
    <th width="208" scope="col"><span class="style32">No IC </span> </th>
    <td width="320"><input name="noic" type="text" size="12"       
         maxlength="50" value="<?php echo htmlspecialchars($noic); ?>">* 
         <span class="isa_error"><?php echo $noic; ?></span>
    </td>
</tr>
<tr>
    <th scope="row"><span class="style32">Kata Laluan</span></th>
    <td><input name="katalaluan" type="password" size="30"  
         maxlength="50" value="<?php echo htmlspecialchars($katalaluan); ?  
         >">*<span class="isa_error"><?php echo $katalaluan; ?></span>
    </td>
</tr>
</table>
<p align="center">
    <input name="btnSubmit" type="submit" value="Hantar">
    <input name="btnReset" type="reset" Value="reset"> </p>

</form>
</th></tr></table>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
ila dila
  • 3
  • 1
  • 5
  • 1
    Don't include same file twice and use include_once instead of include. – shubham715 Jan 21 '17 at 12:40
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Jan 21 '17 at 12:45
  • i've changed it. it is same also. :( – ila dila Jan 21 '17 at 12:47
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 21 '17 at 12:51
  • Please dont __roll your own__ password hashing. PHP provides [`password_hash()`](http://php.net/manual/en/function.password-hash.php) and [`password_verify()`](http://php.net/manual/en/function.password-verify.php) please use them. And here are some [good ideas about passwords](https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet) If you are using a PHP version prior to 5.5 [there is a compatibility pack available here](https://github.com/ircmaxell/password_compat) – RiggsFolly Jan 21 '17 at 12:52
  • 1) Initialize `$error = false;` at the top of your script. 2) Set `$error = true` EVERYWHERE you see an error – RiggsFolly Jan 21 '17 at 12:54
  • I've followed all your given code.. but, everytime i've put any input, the input will display behind the text field. not the error – ila dila Jan 21 '17 at 13:09

1 Answers1

0

my problem solved.1. i deleted the 'if .... ' above the $encrypted. 2. change to '$namaErr and so on...' in the beside the span error and declare the variable as '$nama= $_POST['nama']; and so on....'. Thanks for your support... :) time to add more features in my system. hope goes well...

ila dila
  • 3
  • 1
  • 5