doushaqing7080 2014-05-26 13:19
浏览 65
已采纳

让我的sql插入查询在函数内工作

I have a form that passes input data to a processing page. This processing form then checks whether the email and user name already exists in the database. If they do, an error is reported, the function I am having difficulty with is, if the error reports nothing then go and execute the sql insert query otherwise echo the error. I can get most of it to work except the insert data to database. Can anyone help me see the error in my code please ?

Processing page :

<?php

session_start();
list($username,$email,$clubname, $hash) = $_SESSION['data'];
unset($_SESSION['data']);

include_once 'db_connect.php';

$usernameErr = $emailErr = "";

$password = $hash;
$databaseErr = 'cannot connect to database';


$query1 = mysqli_query($mysqli, "SELECT * FROM members WHERE email='".$email."'");

if(mysqli_num_rows($query1) > 0){
//  echo 'email already exists';

    $usernameErr = "username already exists";

}else{
    // do something
    if (!mysqli_query($mysqli,$query1))
    {
        die('Error: ' . mysqli_error($mysqli));
    }
}

$query2 = mysqli_query($mysqli, "SELECT * FROM members WHERE username='".$username."'");

if(mysqli_num_rows($query2) > 0){
//  echo 'username already exists';

    $emailErr = "email already exists";

}else{
    // do something
    if (!mysqli_query($mysqli,$query2))
    {
        die('Error: ' . mysqli_error($mysqli));
    }
}

if ($usernameErr == "" && $emailErr == "" ) {

    $sql = mysqli_query($mysqli, "INSERT INTO members (username, email, password, clubname) VALUES ('$username', '$email', '$password', '$clubname')");

    if (!mysqli_query($mysqli,$sql)) {
        die('Error: ' . mysqli_error($mysqli));
    }
    echo "1 record added";
} 
else {
    echo $usernameErr.'<br/><br/>';
    echo $emailErr.'<br/><br/>';
}
  • 写回答

1条回答 默认 最新

  • dou91736 2014-05-26 13:37
    关注

    I came up with this, a simple skeleton, just edit to suit you. Don't forget to hash the password parameter.

    function emailEmailExists($emall) {
       if(mysqli_num_rows($email) >= 1) {
         return 1;
       } else {
         return 0;
       }
     }
    
    function usernameExists($username) {
      if(mysqli_num_rows($username) >= 1) {
        return 1;
      } else {
        return 0;
      }
    }
    
    
    function insertUser($username, $password, $email, $otherField, $otherField, ) {
      if(checkEmailExists($email) == 1) {
        echo 'Email in use!';
      } else if(usernameExists($username) == 1){
        echo 'Username in use!';
      } else {
        $insertUser = mysqli_query($yourQuery);
    
        if($insertUser) {
          echo 'User created!';
        } else {
          // Give error.
        }
      }
    } 
    

    Edit

    How do you create you DB connection, remove your credentials and please show.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据