我试图通过表单写两个表。
我有一个包含以下内容的客户表:
-customer_id int(11) (PK) (AI)
-customer_uname varchar(20)
-customer_password varchar(60)
customer_id是主要和自动递增。当写入此内容时,我希望该值也可以写入address_book表中的customer_id
address_book table
-address_book_id int(11) (PK)
-customer_id int(11) (FK)
-customer_firstname varchar(25)
-customer_surname varchar(25)
-customer_addr1 varchar(25)
-customer_addr2 varchar(25)
-customer_town varchar(25)
-customer_city varchar(20)
-customer_county varchar(20)
-customer_postcode varchar(12)
-customer_email varchar(40)
-customer_phone varchar(12)
以下是我用来将用户输入传递给脚本的表单:
<form action="newmemberscript.php" method="POST">
<p>First Name:
<input type="text" name="f_name" required/>
</p>
<p>Surname Name:
<input type="text" name="s_name" required/>
</p>
<p>Choose your username Name:
<input type="text" name="u_name" required/>
</p>
<p>Choose your password:
<input type="password" name="password" required/>
</p>
<p>Address Line 1:
<input type="text" name="addr_1" required/>
</p>
<p>Address Line 2:
<input type="text" name="addr_2" />
</p>
<p>Town:
<input type="text" name="town" required/>
</p>
<p>City:
<input type="text" name="city" />
</p>
<p>County:
<input type="text" name="county" />
</p>
<p>Postcode:
<input type="text" name="postcode" required/>
</p>
<p>Phone:
<input type="text" name="phone" />
</p>
<p>E-mail address:
<input type="text" name="e_mail" required/>
</p>
<p>
<input type="submit">
</p>
</form>
这是脚本。写入客户似乎不是问题。
$custpass = $_POST['password'];
$custpass = md5($custpass);
//write the variables into the database
$sql="INSERT INTO customers (customer_id, customer_uname, customer_password) VALUES ('', '$_POST[u_name]', '$custpass')";
if($result1 = mysqli_query($con, $sql))
{
$customer_id = mysqli_insert_id();
$sql2="INSERT INTO address_book (customer_id, customer_firstname, customer_surname, customer_addr1, customer_addr2, customer_town, customer_city, customer_county, customer_postcode, customer_email, customer_phone) VALUES ('".$customer_id."', '$_POST[f_name]', '$_POST[s_name]', '$_POST[addr_1]', '$_POST[addr_2]', '$_POST[town]', '$_POST[city]', '$_POST[county]', '$_POST[postcode]', '$_POST[email]', '$_POST[phone]')";
if($result2 = mysqli_query($con, $sql2))
{
echo "New member written to database";
}
}
目前没有任何内容写入address_book表,我无法弄清楚原因。我希望有人能看到我不能做的事情。提前谢谢。