向数据库提交数据没有收到错误但没有数据

时间:2013-12-16 17:52:07

标签: php mysql database forms

我正在使用x10hosting.com,我已经为数据库设置了数据库和用户,我还编写了注册用户页面但是当我输入所有数据并单击提交时我没有收到任何错误消息所以我我猜它是连接到数据库就好了,但没有数据保存到数据库中的表,这是我的代码。任何建议都会有所帮助,因为我现在多次修改我的代码,但仍然没有修复它

<?php
require_once("config.php");
if (!isset($_POST['submit'])) {
?>   <!-- The HTML registration form -->
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
       Please fill the following form to sign up:<br /><br />

        Username*: <input type="text" name="username" /><br />
        Password*: <input type="password" name="password" /><br />
        Password Verify*: <input type="password" name="passwordVeri" /><br />
        First name: <input type="text" name="firstName" /><br />
        Last name: <input type="text" name="lastName" /><br />
        Email*: <input type="type" name="emailAddress" /><br />
        Relationship Status*: <input type="type" select name="relationshipStatus" /><br />
        <option value="Single">Single</option>
        <option value="Taken">Taken</option>            
        </select> 
        Country*: <input type="type" name="country" /><br />
        City*: <input type="type" name="city" /><br />
        Postcode*: <input type="type" name="postCode" /><br />
        Mobile number*: <input type="type" name="mobileNumber" /><br />
        Gender*: <input type="type" select name="gender" /><br />
        <option value="Single">Single</option>
        <option value="Taken">Taken</option>            
        </select>
        Date of Birth*: <input type="type" name="dateOfBirth" /> (Format: DD-MM-YYYY)<br />
        <input type="submit" name="submit" value="Register" />
    </form>
<?php


} else {
## connect mysql server
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    # check connection
    if ($mysqli->connect_errno) {
        echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();


    # protect data for insertion
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $emailAddress = mysql_real_escape_string($_POST['emailAddress']);
    $firstName = mysql_real_escape_string($_POST['firstName']);           
    $lastName = mysql_real_escape_string($_POST['lastName']);                             
    $country = mysql_real_escape_string($_POST['country']);                          
    $city = mysql_real_escape_string($_POST['city']);                            
    $relationshipStatus = mysql_real_escape_string($_POST['relationshipStatus']);                           
    $postCode = mysql_real_escape_string($_POST['postCode']);                            
    $mobileNumber = mysql_real_escape_string($_POST['mobileNumber']);                             
    $dateOfBirth = mysql_real_escape_string($_POST['dateOfBirth']);                           
    $gender = mysql_real_escape_string($_POST['gender']);                             

         //set every user to 0
        $_POST['accountType'] = 0;
        $accountType = mysql_real_escape_string($_POST['accountType']);                       

    //check if the two passwords are identical
      if($_POST['password']==$_POST['passwordVeri'])
      {
      }
       else
      {
    //Otherwise,  passwords are not identical
         echo "<p>Passwords do not match!</p>";;
      }

      //We check if the password has 6 or more characters
      if(strlen($_POST['password'])>=6)
      {
      }
       else
      {
    //Otherwise,  passwords are not identical
         echo "<p>Password must be 6 or more characters!</p>";;
      }

    //We check if the email form is valid
              if(preg_match('#^(([a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+\.?)*[a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+)@(([a-z0-9-_]+\.?)*[a-z0-9-_]+)\.[a-z]{2,}$#i',
              $_POST['emailAddress']))
           {
           }
           else
           {
               //Otherwise,  email not valid
         echo "<p>The email you entered is not valid.!</p>";;
      }


    # check if username and email exist else insert
    $exists = 0;
    $result = $mysqli->query("SELECT username from User WHERE username = '{$username}' LIMIT 1");
    if ($result->num_rows == 1) {
        $exists = 1;
        $result = $mysqli->query("SELECT emailAddress from Member WHERE emailAddress = '{$emailAddress}' LIMIT 1");
        if ($result->num_rows == 1) $exists = 2; 
    } else {
        $result = $mysqli->query("SELECT emailAddress from Member WHERE emailAddress = '{$emailAddress}' LIMIT 1");
        if ($result->num_rows == 1) $exists = 3;
    }

    if ($exists == 1) echo "<p>Username already exists!</p>";
    else if ($exists == 2) echo "<p>Username and Email already exists!</p>";
    else if ($exists == 3) echo "<p>Email already exists!</p>";
    else {
        # insert data into mysql database
        $sql = "INSERT  INTO `user` (`Userid`, `username`, `password`, `accountType`) 
                VALUES (NULL, '{$username}', '{$password}', '{$accountType}')";
                "INSERT  INTO `Member` (`firstName`, `lastName`, `gender`, `emailAddress`, `city`, `country`, `postCode`, `relationshipStatus`, `mobileNumber`) 
                VALUES (NULL, '{$firstName}', '{$lastName}', '{$gender}', '{$emailAddress}', '{$city}', '{$country}', '{$postCode}', '{$relationshipStatus}',               '{$mobileNumber}')";

        if ($mysqli->query($sql)) {
            //echo "New Record has id ".$mysqli->insert_id;
            echo "<p>Registred successfully!</p>";
        } else {
            echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>";
            exit();
        }
    }
    }
}

 ?> 

2 个答案:

答案 0 :(得分:1)

您的代码有括号问题:

if ($mysqli->connect_errno) {
    echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
    exit();
    ...blablah your entire code...
}

而不是:

if ($mysqli->connect_errno) {
    echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
    exit();
}
    ...blablah your entire code...

所以你的代码永远不会执行。这是正确的版本:

<?php
require_once("config.php");
if (!isset($_POST['submit'])) {
?>   <!-- The HTML registration form -->
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
       Please fill the following form to sign up:<br /><br />

        Username*: <input type="text" name="username" /><br />
        Password*: <input type="password" name="password" /><br />
        Password Verify*: <input type="password" name="passwordVeri" /><br />
        First name: <input type="text" name="firstName" /><br />
        Last name: <input type="text" name="lastName" /><br />
        Email*: <input type="type" name="emailAddress" /><br />
        Relationship Status*: <input type="type" select name="relationshipStatus" /><br />
        <option value="Single">Single</option>
        <option value="Taken">Taken</option>            
        </select> 
        Country*: <input type="type" name="country" /><br />
        City*: <input type="type" name="city" /><br />
        Postcode*: <input type="type" name="postCode" /><br />
        Mobile number*: <input type="type" name="mobileNumber" /><br />
        Gender*: <input type="type" select name="gender" /><br />
        <option value="Single">Single</option>
        <option value="Taken">Taken</option>            
        </select>
        Date of Birth*: <input type="type" name="dateOfBirth" /> (Format: DD-MM-YYYY)<br />
        <input type="submit" name="submit" value="Register" />
    </form>
<?php


} else {
## connect mysql server
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    # check connection
    if ($mysqli->connect_errno) {
        echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();
    }

    # protect data for insertion
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $emailAddress = mysql_real_escape_string($_POST['emailAddress']);
    $firstName = mysql_real_escape_string($_POST['firstName']);           
    $lastName = mysql_real_escape_string($_POST['lastName']);                             
    $country = mysql_real_escape_string($_POST['country']);                          
    $city = mysql_real_escape_string($_POST['city']);                            
    $relationshipStatus = mysql_real_escape_string($_POST['relationshipStatus']);                           
    $postCode = mysql_real_escape_string($_POST['postCode']);                            
    $mobileNumber = mysql_real_escape_string($_POST['mobileNumber']);                             
    $dateOfBirth = mysql_real_escape_string($_POST['dateOfBirth']);                           
    $gender = mysql_real_escape_string($_POST['gender']);                             

         //set every user to 0
    $_POST['accountType'] = 0;
    $accountType = mysql_real_escape_string($_POST['accountType']);                       

    //check if the two passwords are identical
    if($_POST['password']==$_POST['passwordVeri'])
    {
    }
     else
    {
    //Otherwise,  passwords are not identical
         echo "<p>Passwords do not match!</p>";;
    }

    //We check if the password has 6 or more characters
    if(strlen($_POST['password'])>=6)
    {
    }
     else
    {
    //Otherwise,  passwords are not identical
         echo "<p>Password must be 6 or more characters!</p>";;
    }

    //We check if the email form is valid
    if(preg_match('#^(([a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+\.?)*[a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+)@(([a-z0-9-_]+\.?)*[a-z0-9-_]+)\.[a-z]{2,}$#i',
              $_POST['emailAddress']))
           {
           }
           else
           {
               //Otherwise,  email not valid
         echo "<p>The email you entered is not valid.!</p>";;
      }


    # check if username and email exist else insert
    $exists = 0;
    $result = $mysqli->query("SELECT username from User WHERE username = '{$username}' LIMIT 1");
    if ($result->num_rows == 1) {
        $exists = 1;
        $result = $mysqli->query("SELECT emailAddress from Member WHERE emailAddress = '{$emailAddress}' LIMIT 1");
        if ($result->num_rows == 1) $exists = 2; 
    } else {
        $result = $mysqli->query("SELECT emailAddress from Member WHERE emailAddress = '{$emailAddress}' LIMIT 1");
        if ($result->num_rows == 1) $exists = 3;
    }

    if ($exists == 1) echo "<p>Username already exists!</p>";
    else if ($exists == 2) echo "<p>Username and Email already exists!</p>";
    else if ($exists == 3) echo "<p>Email already exists!</p>";
    else {
        # insert data into mysql database
        $sql = "INSERT  INTO `user` (`Userid`, `username`, `password`, `accountType`) 
                VALUES (NULL, '{$username}', '{$password}', '{$accountType}')";
                "INSERT  INTO `Member` (`firstName`, `lastName`, `gender`, `emailAddress`, `city`, `country`, `postCode`, `relationshipStatus`, `mobileNumber`) 
                VALUES (NULL, '{$firstName}', '{$lastName}', '{$gender}', '{$emailAddress}', '{$city}', '{$country}', '{$postCode}', '{$relationshipStatus}',               '{$mobileNumber}')";

        if ($mysqli->query($sql)) {
            //echo "New Record has id ".$mysqli->insert_id;
            echo "<p>Registred successfully!</p>";
        } else {
            echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>";
            exit();
        }
    }
}

答案 1 :(得分:1)

首先,您将mysql_mysqli_函数混合在一起。

此外,您的INSERT列和VALUES列不匹配。您的INSERT有9x列,Member表(VALUES)有10x值。

$sql = "INSERT  INTO `user` (`Userid`, `username`, `password`, `accountType`) 
VALUES (NULL, '{$username}', '{$password}', '{$accountType}')";
"INSERT  INTO `Member` (`firstName`, `lastName`, `gender`, `emailAddress`, `city`, `country`, `postCode`, `relationshipStatus`, `mobileNumber`) 
VALUES (NULL, '{$firstName}', '{$lastName}', '{$gender}', '{$emailAddress}', '{$city}', '{$country}', '{$postCode}', '{$relationshipStatus}', '{$mobileNumber}')";

在firstName之前删除成员查询的NULL或为其添加适当的USER_ID字段。仅这一点就会阻止你的查询执行。

要使用mysqli_real_escape_string,您需要将目前的内容更改为:

# protect data for insertion
$username = mysqli_real_escape_string($mysqli,$_POST['username']);
$password = mysqli_real_escape_string($mysqli,$_POST['password']);
$emailAddress = mysqli_real_escape_string($mysqli,$_POST['emailAddress']);
$firstName = mysqli_real_escape_string($mysqli,$_POST['firstName']);           
$lastName = mysqli_real_escape_string($mysqli,$_POST['lastName']);                             
$country = mysqli_real_escape_string($mysqli,$_POST['country']);                          
$city = mysqli_real_escape_string($mysqli,$_POST['city']);                            
$relationshipStatus = mysqli_real_escape_string($mysqli,$_POST['relationshipStatus']);                           
$postCode = mysqli_real_escape_string($mysqli,$_POST['postCode']);                            
$mobileNumber = mysqli_real_escape_string($mysqli,$_POST['mobileNumber']);                             
$dateOfBirth = mysqli_real_escape_string($mysqli,$_POST['dateOfBirth']);                           
$gender = mysqli_real_escape_string($mysqli,$_POST['gender']);                             

 //set every user to 0
$_POST['accountType'] = 0;
$accountType = mysqli_real_escape_string($mysqli,$_POST['accountType']);

此外,您可以通过执行以下操作来执行第一次查询所做的操作:

$sql2 =  "INSERT  INTO `Member` (`firstName`, `lastName`, `gender`, `emailAddress`, `city`, `country`, `postCode`, `relationshipStatus`, `mobileNumber`) 
        VALUES ('{$firstName}', '{$lastName}', '{$gender}', '{$emailAddress}', '{$city}', '{$country}', '{$postCode}', '{$relationshipStatus}', '{$mobileNumber}')";

if ($mysqli->query($sql2)) {
//echo "New Record has id ".$mysqli->insert_id;
echo "<p>Member table updated successfully!</p>";
} else {
    echo "<p>MySQL Member table error no {$mysqli->errno} : {$mysqli->error}</p>";
    exit();
}