将数据添加到2个表中

时间:2017-02-01 04:05:01

标签: php mysqli

我正在尝试将数据添加到2个表中。数据到达我的桌子,称为公司,没有任何问题。但是数据没有到达表用户。我没有收到错误消息,然后页面加载了管理页面。

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <title>Create a Company</title>
    <link rel="stylesheet" type="text/css" href="/css.css"/>

</head>
<body>
    <h2 class="header"> Create a Company </h2>
    <form action="processcompany.php" method="post">
        <input class="entry" placeholder="Account No" name="accountno" type="text"><br>
        <input class="entry" placeholder="Company Name" name="companyname" type="text" required="required"><br>
        <input class="entry" placeholder="GST/VAT/ABN/TAX No" name="taxno" type="text" required="required"><br>
        <input class="entry" placeholder="Address Line 1" name="address1" type="text" value=""><br>
        <input class="entry" placeholder="Address Line 2" name="address2" type="text" value=""><br>
        <input class="entry" placeholder="Suburb/County" name="suburb" type="text" value=""><br>
        <input class="entry" placeholder="State" name="state" type="text" value=""><br>
        <input class="entry" placeholder="Post/Zip Code" name="postcode" type="text" value=""><br>
        <input class="entry" placeholder="Country" name="country" type="text" value=""><br>
        <input class="entry" placeholder="Primary Contact" name="primarycontact" type="text" value=""><br>
        <input class="entry" placeholder="Primary Email" name="primaryemail" type="text" value=""><br>
        <input class="entry" placeholder="Subscription Type" name="subscriptiontype" type="text" value=""><br>
        <input class="entry" placeholder="Subscription Status" name="subscriptionstatus" type="hidden" value="Active"><br>
        <input class="entry" placeholder="Subscription End Date" name="subscriptionenddate" type="text" value=""><br><br><br>



        <input class="entry" placeholder="login Email Address" name="loginname" type="text" value=""><br>
        <input class="entry" placeholder="First and Last Name" name="counttypename" type="text" value=""><br>
        <input class="entry" placeholder="User Type" name="usertype" type="hidden" value="Company Administrator"><br>
        <input class="entry" placeholder="User Status" name="status" type="hidden" value="Active"><br>
        <input class="entry" placeholder="Password" name="password" type="text" value=""><br>
        <input class="button" type="submit">

    </form>
</body>
</html>

这是我的脚本文件

<?php
include 'db.php';

$sql = "INSERT INTO `companies` 
            ( `accountno`, `companyname` , 
            `taxno` , `address1`, `address2`, `suburb` , 
            `state` , `postcode`, `country`, `primarycontact` , 
            `primaryemail`, `subscriptiontype` , 
            `subscriptionstatus`, `subscriptionenddate`,    
            `datecreated` ) 
    VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())";

$stmt = $conn->prepare($sql);
if ( ! $stmt ) {
echo $stmt->error;
exit;
}

$stmt->bind_param('isssssssssssss',
                $_POST['accountno'],
                $_POST['companyname'],
                $_POST['taxno'],
                $_POST['address1'],
                $_POST['address2'],
                $_POST['suburb'],
                $_POST['state'], 
                $_POST['postcode'], 
                $_POST['country'], 
                $_POST['primarycontact'], 
                $_POST['primaryemail'], 
                $_POST['subscriptiontype'], 
                $_POST['subscriptionstatus'], 
                $_POST['subscriptionenddate']
            );

$stmt->execute();
if ( ! $stmt ) {
echo $stmt->error;
exit;
}    

$sqla = "INSERT INTO `users` 
            ( `accountno`, `loginname` , 
            `password` , `countteamname`, `status`, `usertype` , 
            `datecreated` ) 
    VALUES (?,?,?,?,?,?,NOW())";

$stmta = $conn->prepare($sqla);
if ( ! $stmta ) {
echo $stmta->error;
exit;
}

$stmta->bind_param('isssss',
                $_POST['accountno'],
                $_POST['loginname'],
                $_POST['password'],
                $_POST['countteamname'],
                $_POST['status'],
                $_POST['usertype']
            );

$stmta->execute();
if ( ! $stmta ) {
echo $stmta->error;
exit;
}

mysqli_close($conn);

header('location: admin.php');

?>

我的数据库值是

userid, accountno, loginname, password, countteamname, status, counttype, piid, datecreated
目前没有将counttype和piid添加到表中。 userid是mysql的自动增量号。

一旦我上传了此内容,我将使用哈希来保护密码。

我一直在试图自己解决这个问题几个小时。我希望你能提供帮助。

1 个答案:

答案 0 :(得分:0)

通过它的外观你只能用查询添加到一个表

连接两个查询怎么样

$ sql =“QUERY;”;

$ sql。=“QUERY;”;

使用。=来连接两个查询。