0

私は周りを見回しましたが、Imはまだこれを行う方法がわかりません。私はいくつかの異なる方法を乾燥させました、そしてその明白なimはまだそれを間違っています。手伝ってもらえますか

アカウントのユーザー名とパスワードを含むアカウントテーブルと、ユーザー名によってアカウントテーブルに好まれる別の連絡先テーブルがあります。これらの両方に挿入する必要があります。これが私がこれまでに持っているものです。

ありがとう

//signup.php  
include 'connect.php';  

echo '<h3>Sign up</h3>';  

if($_SERVER['REQUEST_METHOD'] != 'POST')  
{  
    /*the form hasn't been posted yet, display it 
      note that the action="" will cause the form to post to the same page it is on */  
    echo '<form action="" method="post">
  <br>
  <table width="0" border="0">
    <tr>
    <th align="left" scope="col">Name:</th>
    <th scope="col"><input type="text" name="name"></th>
  </tr>
  <tr>
    <th align="left" scope="row">Phone:</th>
    <td><input type="text" name="phone"></td>
  </tr>
  <tr>
    <th align="left" scope="row">Address</th>
    <td><textarea name="address" rows="4"></textarea></td>
  </tr>
  <tr>
    <th align="left" scope="row"><p>Postcode</p></th>
    <th align="left" scope="row"><input type="text" name="postcode" id="postcode"></th>
  </tr>
  <tr>
    <th align="left" scope="row">Email</th>
    <td><input type="text" name="email"></td>
  </tr>
  <tr>
    <th align="left" scope="row">Username</th>
    <td><input type="type" name="username"></td>
  </tr>
  <tr>
    <th align="left" scope="row">Password</th>
    <td align="left"><input type="password" name="password"></td>
  </tr>
  <tr align="left">
    <th colspan="2" scope="row"><input type="Submit"></th>
  </tr>
  </table>
</form>'; 
} 
else 
{ 
    /* so, the form has been posted, we'll process the data in three steps:  
        1.  Check the data  
        2.  Let the user refill the wrong fields (if necessary)  
        3.  Save the data  
    */  
    $errors = array(); /* declare the array for later use */  

    if(isset($_POST['username']))  
    {  
        //the user name exists  
        if(!ctype_alnum($_POST['username']))  
        {  
            $errors[] = 'The username can only contain letters and digits.';  
        }  
        if(strlen($_POST['username']) > 30)  
        {  
            $errors[] = 'The username cannot be longer than 30 characters.';  
        }  
    }  
    else  
    {  
        $errors[] = 'The username field must not be empty.';  
    } 
    if(!empty($errors)) /*check for an empty array, if there are errors, they're in this array (note the ! operator)*/  
    {  
        echo 'Uh-oh.. a couple of fields are not filled in correctly..'; 
        echo '<ul>'; 
        foreach($errors as $key => $value) /* walk through the array so all the errors get displayed */ 
        { 
            echo '<li>' . $value . '</li>'; /* this generates a nice error list */ 
        } 
        echo '</ul>'; 
    } 
    else 
    { 
        //the form has been posted without, so save it 
        //notice the use of mysql_real_escape_string, keep everything safe! 
        //also notice the sha1 function which hashes the password 
        $sql = "INSERT INTO 
                    tbl_accounts(accounts_username, accounts_password, accounts_date) 
                VALUES('" . mysql_real_escape_string($_POST['username']) . "', 
                       '" . sha1($_POST['password']) . "', 
                        NOW())";  
        $sql2= "INSERT INTO 
                    tbl_contacts(contacts_username, contacts_name, contacts_email, contacts_phone, contacts_address, contacts_postcode, contacts_date) 
                VALUES('" . mysql_real_escape_string($_POST['username']) . "',
                '" . mysql_real_escape_string($_POST['name']) . "',
                '" . mysql_real_escape_string($_POST['email']) . "',
                '" . mysql_real_escape_string($_POST['phone']) . "',
                '" . mysql_real_escape_string($_POST['address']) . "',
                '" . mysql_real_escape_string($_POST['postcode']) . "',
                        NOW())";

        $result = mysql_query($sql);  
        if(!$result)  
        $result = mysql_query($sql2);  
        if(!$result)  
        {  
            //something went wrong, display the error  
            echo 'Something went wrong while registering. Please try again later.'; 
            //echo mysql_error(); //debugging purposes, uncomment when needed 
        } 
        else 
        { 
            echo 'Successfully registered. You can now <a href="signin.php">sign in</a> and start posting! :-)'; 
        } 

    } 
}
4

3 に答える 3

1

変化する:

    $result = mysql_query($sql);  
    if(!$result)  
    $result = mysql_query($sql2);  
    if(!$result)  
    {  

に:

    if(!mysql_query($sql) || !mysql_query($sql2))  
    {  

...そしてあなたの問題はなくなると思います。

これこれを読み直す価値があります。

この種のフォーム送信を処理するには、 POST / Redirect/GETデザインパターンを使用する必要があることにも注意してください。

于 2012-05-22T21:29:50.063 に答える
0

このコードブロックは、あなたが思っていることをしません

これは、クエリsql1を送信し、そのクエリが失敗した場合はsql2を送信します。

    $result = mysql_query($sql);  
    if(!$result)  
    $result = mysql_query($sql2);  
    if(!$result)  

あなたがそれらの両方に挿入したいなら、あなたはするべきです

    $result = mysql_query($sql);  
    if(!$result)  {
       echo "first query failed"; 
    }
    else {  $result = mysql_query($sql2); }  

    if(!$result) {
       echo "second query failed";
     }
于 2012-05-22T21:30:35.867 に答える
0

stackoverflowを検索すると、次のようなリンクが見つかります。

スタック1

スタック2

于 2012-05-22T21:42:41.160 に答える