Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
698 views
in Technique[技术] by (71.8m points)

php - INSERT with LEFT JOIN

<?php               
include 'connection.php';
session_start();
$noteinfo = array();
$noteinfo['note'] = $_POST['note'];
$_SESSION['noteinfo'] = $noteinfo;

if (isset($_POST['submit'])) {
    if (empty($_POST['note'])) {
        echo "Dobavete Komentar";
    }
    if (!empty($_SESSION['noteinfo'])) {
        $check = mysqli_escape_string($conn,$_SESSION['userinfo']['fname']);
        print_r($_SESSION['userinfo']);     
        $sql = "INSERT INTO users (user_fname,user_mname,user_lname,user_login,user_email,user_phone) 
                VALUES ('{$_SESSION['userinfo']['fname']}', '{$_SESSION['userinfo']['mname']}', '{$_SESSION['userinfo']['lname']}', '{$_SESSION['userinfo']['login']}', '{$_SESSION['userinfo']['email']}', '{$_SESSION['userinfo']['phone']}')";
        $sql1 = "INSERT INTO addresses (address_line_1,address_line_2,address_zip,address_city,address_province,address_country) 
                VALUES ('{$_SESSION['addressinfo']['adr1']}', '{$_SESSION['addressinfo']['adr2']}', '{$_SESSION['addressinfo']['zip']}', '{$_SESSION['addressinfo']['city']}', '{$_SESSION['addressinfo']['provinciq']}', '{$_SESSION['addressinfo']['durjava']}')"; 
        $sql2 = "INSERT INTO notes (note_text) 
                VALUES ('{$_SESSION['noteinfo']['note']}')";
        $sql3 = "INSERT INTO users_addresses (ua_user_id,ua_address_id) 
                SELECT users.user_id,addresses.address_id 
                FROM ( VALUES () )";

        if (mysqli_query($conn,$sql)) {
            echo "Added";
            if (mysqli_query($conn,$sql1)) {
                echo "Added";
                if (mysqli_query($conn,$sql2)) {
                    echo "Added";
                    if (mysqli_query($conn,$sql3)) {
                        echo "Mai stana toq put";
                        header("refresh:3 ; url=profile.php");
                    }
                }
            } 
        }
        else{
            echo "Error";
        }
    }
    else{
        header("refresh:1 ; url=zapiski.php");
    }
}
?>

My question is for $sql3: I want when the forms are completed to fill the users_addresses with the id of the user and the id from the address tables.But for now the SQL doesn't fill nothing in the table users_addresses. These are my tables:

These are my tables

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When a user is filling in values in an application and you need to look up ids for them, then a typical way to write the query looks like this:

INSERT INTO users_addresses (ua_user_id, ua_address_id)
    VALUES ( (SELECT u.user_id FROM users u WHERE u.username = ?),
             (SELECT a.address_id FROM address a WHERE a.address = ?)
           );

That is, you are passing parameters into the query, using the parameters to look up ids, and then using those ids to insert into the junction table.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...