English 中文(简体)
使用 php 插入两个表格
原标题:Insert in to two tables using php
  • 时间:2012-05-22 21:24:15
  •  标签:
  • php
  • sql

我环顾四周,仍然不确定如何这样做。我已经干尽了几种不同的方式,而且显然仍然做错了。请帮帮忙。

我有一个包含账户用户名和密码的账务表和一个单独的联系人表,用户名的账务表喜欢这个单独的联系人表。我需要将这两个表格都插入其中。这是我到目前为止拥有的。

谢谢 谢谢

//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! :-) ; 
        } 

    } 
}
最佳回答

搜索堆叠流时, 您将会找到以下的链接 :

< a href=" "https://stackoverflow.com/ questions/6128680/ update - two

< a href=" "https://stackoverflow.com/ questions/8766490/mysql- update- two-tats-at-once" > 堆叠 2

问题回答

更改 :

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

致:

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

我认为你的问题会消失

值得您重读 "http://php.net/manual/en/control-structures.if.php" rel = "nofollow" > this this

请注意,您应该使用 < a href=" http://en.wikipedia.org/wiki/Post/Redirect/Get" rel=“nofollow”>POST/Redent/GET 的设计模式来处理这类性质提交的表格。

这个代码块不会做你认为它做的那些事

这将发送查询 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";
     }




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签