PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create table users ( id int primary key auto_increment, name varchar(64) ); insert into users values (1, 'User1'); create table addresses ( id int primary key auto_increment, user_id int, city varchar(64), address varchar(255), index (user_id), foreign key (user_id) references users(id) on delete cascade );
Copy Clear
Copy Format Clear
<?php session_start(); // require_once "connect.php"; $connect = &$mysqli; $address = 'My own address'; // $_POST["address"]; $address_id = (int)$_POST["address_id"]; $_SESSION["user"]["address"] = $address; $id = 1; //$_SESSION["user"]["uid"]; if ($address_id == 0) { // Insert new address $stmt = mysqli_prepare( $connect, "INSERT INTO addresses (address, user_id) VALUES (?, ?)"); mysqli_stmt_bind_param($stmt, "si", $address, $id); } else { // Update exists address $stmt = mysqli_prepare($connect, "UPDATE users SET address = ? WHERE id = ?;"); mysqli_stmt_bind_param($stmt, "si", $address, $address_id); } $stmtStatus = mysqli_stmt_execute($stmt); mysqli_stmt_close($stmt); //header("Location: ../purchase"); $result = mysqli_query( $connect, "SELECT * FROM users LEFT JOIN addresses ON users.id = user_id" ); $addresses = mysqli_fetch_object($result); print_r($addresses); ?>
Show:  
Copy Clear