将注册表单从mysql转换为mysqli

时间:2016-04-29 06:38:38

标签: php mysql mysqli

这是我在mysql中的注册表单,我正在尝试将其转换为mysqli,以便它可以与数据库一起使用

<?php
    mysql_connect("localhost","root","");  

    mysql_select_db("Project"); 

    if(isset($_POST['submit'])){ 
      $FirstName =$_POST['FirstName'];  
      $LastName = $_POST['LastName'];  
      $DOB = $_POST['DOB'];  
      $UserName = $_POST['UserName'];  
      $PassWord = $_POST['PassWord'];  
      $Email = $_POST['Email'];  
      $CourseID = $_POST['CourseID']; 

    if($FirstName==''){ 
        echo "<script>alert('please enter your  
        FirstName!')</script>"; 
        exit(); 
    } 
    if($LastName==''){ 
        echo "<script>alert('please enter your  
        LastName!')</script>"; 
        exit(); 
    } 
    if($DOB==''){ 
        echo "<script>alert('please enter your  
        DOB!')</script>"; 
        exit(); 
    } 
    if($UserName==''){ 
        echo "<script>alert('please enter your  
        UserName!')</script>"; 
        exit(); 
    } 
    if($PassWord==''){ 
        echo "<script>alert('please enter your  
        PassWord!')</script>"; 
        exit(); 
    } 

    if($CourseID==''){ 
        echo "<script>alert('please enter your  
        CourseID!')</script>"; 

        exit(); 
    } 
    if($Email==''){ 
        echo "<script>alert('please enter your  
        Email!')</script>"; 
        exit(); 
    } 
    $check_email = "select * FROM Student WHERE Email='$Email'"; 

    $run = mysql_query($check_email); 

    if(mysql_num_rows($run)>0){ 
       echo "<script> alert('email $Email already exist in our database, 
       please try another one')</script>"; 
       exit();            
    } 
    $query = "insert into Student (FirstName,LastName,DOB,UserName, 
    PassWord,Email,CourseID) VALUES('$FirstName','$LastName','$DOB', 
    '$UserName','$PassWord','$Email','$CourseID')"; 
    if(mysql_query($query)){ 

        echo "<script> window.open('Welcome.php','_self')</script>"; 
    } 
}
?>

1 个答案:

答案 0 :(得分:3)

在必要时进行更改。这是OOP方法。也不要存储普通密码。

$db = new mysqli("localhost", "root", "","db_name");
$check_email = $db->prepare("select * FROM Student WHERE Email=?"); 
$check_email->bind_param("s",$email);
$check_email->execute();
$result=$check_email->get_result();
$row=$result->fetch_assoc();

       if($row)
{
               echo "<script> alert('email $Email already exist in our database, 
               please try another one')</script>"; 
               exit();            
 } 
            $query = $db->prepare("insert into Student (FirstName,LastName,DOB,UserName, 
            PassWord,Email,CourseID) VALUES(?,?,?,?,?,?,?)"; 

            $query->bind_param("sssssss",$firstname,$lastname,$dob,$username,$password,$email,$courseid);          

if($query->execute())
{ 

                echo "<script> window.open('Welcome.php','_self')</script>"; 
            }