PHP,MySQL将第一个表中的数据插入第二,第三和第四

时间:2017-11-27 12:52:34

标签: php html mysql sql database

我有一个名为users(id,username,password)的表,第二个表名为profiles(id,user_id,username,name,lastname,age,gender,country,company_name),第三个表名为companies(id,user_id, name,resources_id)和第四个名为resources的表(id,user_id,company_id,money)。当用户注册他们的帐户时,他们需要创建他们的个人资料。现在,当他们填写个人资料数据时,个人资料和公司表格填写得很好。

我的问题出在资源表中:

  

错误:整数值不正确:''对于第1行的'company_id'列

当我刷新该页面时出现错误,资源表填写正常,但现在我为配置文件和公司表格获得了双行。

我的代码:

<?php
    session_start();

    $dbserver                    = "localhost";
    $dbusername                  = "root";
    $dbpassword                  = "1234512345";
    $db                          = "game";

    $conn = new mysqli($dbserver, $dbusername, $dbpassword, $db);

    if ($conn->connect_error)
    {
        die("Connection failed: ".$conn->connect_error);
    }

    if(isset($_SESSION['loggedin']))

    $username = $_SESSION['loggedin'];

    $query = "SELECT id FROM users WHERE username = '$username'";
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_assoc($result);

    $userId = $row['id'];
    $_POST['id'] = $userId;

    $name = $_POST['name'];
    $lastname = $_POST['lastname'];
    $age = $_POST['age'];
    $gender = $_POST['gender'];
    $country = $_POST['country'];
    $company_name = $_POST['company_name'];
    $_POST['loggedin'] = $username;

    $query = "SELECT id FROM companies WHERE name = '$company_name'";
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_assoc($result);

    $companyId = $row['id'];
    $_POST['id'] = $companyId;

    //INSERT DATA INTO PROFILES
    $sql = "INSERT INTO profiles (user_id, username, name, lastname, age, gender, country, company_name)
    VALUES ('$userId', '$username', '$name', '$lastname', '$age', '$gender', '$country', '$company_name')";

    //INSERT DATA INTO COMPANIES
    $sql2 = "INSERT INTO companies (user_id, name)
    VALUES ('$userId', '$company_name')";

    //INSERT DATA INTO RESOURCES
    $sql3 = "INSERT INTO resources (user_id, company_id)
    VALUES ('$userId', '$companyId')";

    if($conn->query($sql) && $conn->query($sql2) && $conn->query($sql3) === TRUE)
    {
        header("Location: ../../index.php?page=profile");
        die();
    }
    else
    {
        echo "Error: ".$conn->error;
    }
?>

我应该做些什么?我还是初学者,很抱歉很长一段时间。

编辑: 一个人帮我解决了这个代码,它比上一个版本的错误证明更多。删除公司表,将其与配置文件合并,现在一切正常!

<?php
session_start();

$dbserver                    = "localhost";
$dbusername                  = "root";
$dbpassword                  = "1234512345";
$db                          = "game";

$conn = new mysqli($dbserver, $dbusername, $dbpassword, $db);

if ($conn->connect_error)
{
    die("Connection failed: ".$conn->connect_error);
}

if(isset($_SESSION['loggedin']))

$username = $_SESSION['loggedin'];

$query = "SELECT id FROM users WHERE username = '$username'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);

$userId = $row['id'];
$_POST['id'] = $userId;

$name = $_POST['name'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$country = $_POST['country'];
$company_name = $_POST['company_name'];
$_POST['loggedin'] = $username;

$query = "SELECT id FROM companies WHERE name = '$company_name'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result); 

$sql1 = "INSERT INTO profiles (user_id, username, name, lastname, age, gender, country, company_name) VALUES (?,?,?,?,?,?,?,?)";
$sql2 = "INSERT INTO resources (user_id) VALUES (?)";
try {
  $conn->autocommit(false);
  $statement = $conn->prepare($sql1);
  $statement->bind_param("dsssdsss", $userId, $username, $name, $lastname, $age, $gender, $country, $company_name);
  $statement->execute();


  $statement = $conn->prepare($sql2);
  $statement->bind_param("d", $userId);
  $statement->execute();

  // COMMIT THE CHANGES
  $conn->commit();
  header("Location: ../../index.php?page=profile");
}
catch(Exception $e){
    // undo everything that was done in the try block in the case of a failure.
    $conn->rollback();
    echo "Error: ".$conn->error;
}?>

0 个答案:

没有答案