PHP Insert to MYSql after checking existed value -


i trying register users in app , tried check if age existed stop registration process, wrote code register users , worked when tried validate registration using check_age function doesn't work , still allow registration if age existed can tell me missed code : here code:

<?php if($_server["request_method"]=="post") {        require "init.php";         creat_student(); } function creat_student() {     global $con;     $firstname=$_post["firstname"];     $lastname=$_post["lastname"];     $age=$_post["age"];      if(strcmp(check_age(), '0') == 0)     {     $query="insert student(firstname,lastname,age) values ('$firstname','$lastname','$age');";     mysqli_query($con,$query);     mysqli_close($con);     }     else      echo "not true";  } function check_age() {     global $con;     $age=$_post["age"];     echo " $age";     $temp_arr=array();      $query="select *  student age ='{$age}'; ";     $result=mysqli_query($con,$query);     $num_of_rows=mysqli_num_rows($result);      if($num_of_rows==0)     return '0';     else      return '1';  } 

well mention comments above, there sanitizing needed.

but here suggest..

  1. change check_age() function , pass parameter of age in below , return $num_of_rows.

    function check_age($age) {    global $con;     $query="select *  student age =".$age;    $result=mysqli_query($con,$query);    return mysqli_num_rows($result); } 
  2. then in creat_student() function if condition change like...

    function creat_student() {   global $con;   $firstname=$_post["firstname"];   $lastname=$_post["lastname"];   $age=$_post["age"];    if(!check_age($age))   {     $query="insert student(firstname,lastname,age) values ('$firstname','$lastname','$age');";     mysqli_query($con,$query);     mysqli_close($con);  }  else     echo "not true"; } 

thanks, jay.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -