检查mysqli Insert查询是否执行成功,并显示消息和发送邮件


Check mysqli Insert Query excuted ok and display message and send email

我正在创建一个项目的注册表,没有任何安全或先进的,我仍然是相当新的php等

我将所需的数据插入到登录表和客户表中,数据插入良好。但是我不能让代码检查它是否工作,并发出一封电子邮件并向用户显示一条消息。

我已经尝试使用从数据库中检索的值,这只会是用户注册成功。

if($userID != null)
            {
                $msg1 = "Thank You! you are now registered, please check your email for a verification link to verify your new account! ";
                $col1 = "green";
                //require_once "Mail.php";
                require_once "inc/email.php";
            }

我也试过这个

if($query)
            {
                $msg1 = "Thank You! you are now registered, please check your email for a verification link to verify your new account! ";
                $col1 = "green";
                //require_once "Mail.php";
                require_once "inc/email.php";
        }

谢谢,

编辑-这里是所有的代码,

    <?php
    include ("inc/mysql.php");  
    error_reporting(0);
    $msg = "";
    $col = 'green';
    function test_input($data){
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
        return $data;
    }
    // define variables and set to empty values
    $name = $email = $chkemail = $password = $chkpassword =$address = $towncity = $postcode = "";
//Required field validation
if ($_SERVER["REQUEST_METHOD"] == "POST") {
           if (empty($_POST["name"])) {
                $msg = "Name is required";
                $col = 'red';
          } else {
                $name = test_input($_POST["name"]);
          }
          if (empty($_POST["email"])) {
                $msg = "Email is required";
                $col = 'red';
          } else {
                $email = test_input($_POST["email"]);
          }
          if (empty($_POST["chkemail"])) {
                $msg = "Please confirm your email address";
                $col = 'red';
          } else {
                $chkemail = test_input($_POST["chkemail"]);
          }
          if (empty($_POST["password"])){
                $msg = "Please enter a password";
                $col = 'red';
          } 
          if (empty($_POST["chkpassword"])){
                $msg = "Please confirm your password ";
                $col = 'red';
          } else{
                $chkpassword = test_input($_POST["chkpassword"]);
                if(($_POST["password"]) != $chkpassword) {
                    $msg = "Please check your password is correct";
                    $col = 'red';
                } else{
                    $password = test_input($_POST["password"]);
                }
          }
          if (empty($_POST["address"])) {
                $msg = "Please enter the first line of your address";
                $col = 'red';
          } else {
            $address = test_input($_POST["address"]);
          }
          if (empty($_POST["towncity"])) {
                $msg = "Please enter the first line of your Town or City";
                $col = 'red';
          } else {
            $towncity= test_input($_POST["towncity"]);
          }
          if (empty($_POST["postcode"])) {
                $msg = "Please enter your postcode";
                $col = 'red';
          } else {
            $postcode = test_input($_POST["postcode"]);
            $customerVeri = "N";
            if($customerVeri == "N"){
            $name = mysqli_real_escape_string($db, $name);
            $email = mysqli_real_escape_string($db, $email);
            $password = mysqli_real_escape_string($db, $password);
            $password = md5($password.substr($email,0,3));
            $chkpassword = md5($password.substr($email,0,3));
            $verifyLink = md5(substr($name,0,3).substr($email,0,3));

            $sql="SELECT customerEmail FROM customer_tbl WHERE customerEmail='$email'";
            $result=mysqli_query($db,$sql);
            $row=mysqli_fetch_array($result,MYSQLI_ASSOC);
            if(mysqli_num_rows($result) == 1)
            {   
                $msg1 = "Sorry...This email already exists, please enter another or login...";
                $col1 = "red";
            }
            else
            {
            $query = mysqli_query($db, "INSERT INTO login_tbl (customerEmail, customerPassword)VALUES ('$email', '$password')");
            $sql="SELECT userID FROM login_tbl WHERE customerEmail='$email'";
            $result=mysqli_query($db,$sql);
            $row=mysqli_fetch_array($result,MYSQLI_ASSOC);
            $userID = $row['userID'];
            $query2 = mysqli_query($db, "INSERT INTO customer_tbl (customerName, userID, customerEmail, customerPassword, customerAddress, customerTowncity, customerPostcode, customerVerified, customerVerifiedlink)VALUES ('$name', '$userID', '$email', '$password','$address','$towncity','$postcode','$customerVeri','$verifyLink')");
            echo("Error description: " . mysqli_error($db));
        }
          }
          }
}
    if($userID != null)
            {
                $msg1 = "Thank You! you are now registered, please check your email for a verification link to verify your new account! ";
                $col1 = "green";
                //require_once "Mail.php";
                require_once "inc/email.php";
            }
echo '<div style="color:'.$col.'">';
echo $msg;
echo '</div>';
echo '<div style="color:'.$col1.'">';
echo $msg1;
echo '</div>';
?>

似乎没有问题,但是email.php的问题阻止了语句的其余部分被执行。现在把它拆成碎片。有时,它只需要离开屏幕几个小时!

你不应该检查每条语句是否成功

现代编程不是这样工作的。如果发生错误,任何语句都可以报告错误。如果没有错误,则一切正常。

所以,去掉所有的条件,发送你的电子邮件。