Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
create table student (
id int primary key auto_increment,
student_name varchar(255),
email varchar(255) unique key
);
create table bill (
id int primary key auto_increment,
student_id int,
total_balance int,
foreign key (student_id) references student(id)
);
<?php
$name = "Barry Block";
$email = "barry.b@gmail.com";
$balance = 500;
$student_sql = "
insert into student (student_name, email)
values ( :name, :emailAddress )";
$stmt = $pdo->prepare($student_sql);
$stmt->execute([':name'=>$name, ':emailAddress'=>$email]);
$bill_sql = "
insert into bill (student_id, total_balance)
select id, :balance from student where email = :emailAddress";
$stmt = $pdo->prepare($bill_sql);
$stmt->execute([':balance'=>$balance, ':emailAddress'=>$email]);
$query = "SELECT * from student join bill on bill.student_id = student.id;";
$stmt = $pdo->prepare($query);
$stmt->execute();
$students = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($students);