Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear
-- Создание таблицы специальности врачей CREATE TABLE DoctorSpecialties ( SpecialtyId INT AUTO_INCREMENT PRIMARY KEY, Specialty VARCHAR(255) NOT NULL ); -- Создание таблицы врачей CREATE TABLE Doctors ( DoctorId INT AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(100) NOT NULL, SpecialtyId INT NOT NULL, FOREIGN KEY (SpecialtyId) REFERENCES DoctorSpecialties(SpecialtyId) ); -- Создание таблицы пациентов CREATE TABLE Patients ( PatientId INT AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(100) NOT NULL, Age INT NOT NULL, Gender VARCHAR(10) NOT NULL ); -- Создание таблицы пожеланий пациентов CREATE TABLE Patient_wishes ( WishId INT AUTO_INCREMENT PRIMARY KEY, PatientId INT NOT NULL, SpecialtyId INT NOT NULL, WishText TEXT, FOREIGN KEY (PatientId) REFERENCES Patients(PatientId), FOREIGN KEY (SpecialtyId) REFERENCES DoctorSpecialties(SpecialtyId) ); -- Создание таблицы записей пациентов к врачам CREATE TABLE PatientAppointments ( AppointmentId INT AUTO_INCREMENT PRIMARY KEY, PatientId INT NOT NULL, DoctorId INT NOT NULL, AppointmentDate DATE NOT NULL, FOREIGN KEY (PatientId) REFERENCES Patients(PatientId), FOREIGN KEY (DoctorId) REFERENCES Doctors(DoctorId) );

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php function insertIntoTable($mysqli, $tableName, $columnNames, $data) { $columns = implode(", ", $columnNames); $values = rtrim(str_repeat("?, ", count($columnNames)), ", "); $sql = "INSERT INTO `$tableName` ($columns) VALUES ($values)"; $q = mysqli_prepare($mysqli, $sql); if ($q) { $bindTypes = str_repeat("s", count($columnNames)); $bindParams = array_merge(array($bindTypes), $data); $params = []; foreach ($bindParams as $key => $value) { $params[$key] = &$bindParams[$key]; } call_user_func_array(array($q, "bind_param"), $params); if ($q->execute() === false) die(print_r($q->error, true)); $q->close(); } else die("Failed to prepare the SQL query."); } $patients = [ ['Name' => 'Иванова', 'Age' => 33, 'Gender' => 'Ж'], ['Name' => 'Воробьева', 'Age' => 45, 'Gender' => 'Ж'], ['Name' => 'Суркин', 'Age' => 12, 'Gender' => 'М'] // ... другие пациенты ]; foreach ($patients as $row) { insertIntoTable($mysqli, 'Patients', ['Name', 'Age', 'Gender'], [$row['Name'], $row['Age'], $row['Gender']]); } //вынесена в функцию //foreach ($patients as $row) { // $q = mysqli_prepare( // $mysqli, "INSERT INTO `Patients` (`Name`, `Age`, `Gender`) VALUES (?, ?, ?)"); // $q->bind_param("sis", $row['Name'], $row['Age'], $row['Gender']); // $stmt = $q->execute(); // if ($stmt === false) { // die(print_r(sqlsrv_errors(), true)); // } //} $doctor_specialties = [ ['Specialty' => 'Терапевт'], ['Specialty' => 'Хирург'], ['Specialty' => 'Ортодонт'] // ... другие специальности ]; foreach ($doctor_specialties as $row) { insertIntoTable($mysqli, 'DoctorSpecialties', ['Specialty'], [$row['Specialty']]); } $doctors = [ ['Name' => 'Масалимов', 'SpecialtyId' => 1], ['Name' => 'Фатхуллин', 'SpecialtyId' => 2], ['Name' => 'Васнецов', 'SpecialtyId' => 3] // ... другие врачи ]; foreach ($doctors as $row) { insertIntoTable($mysqli, 'Doctors', ['Name', 'SpecialtyId'], [$row['Name'], $row['SpecialtyId']]); } // Заполнение таблицы записей пациентов к врачам $appointmentsData = [ ['DoctorId' => 1, 'PatientId' => 1, 'AppointmentDate' => '2022-03-09'], ['DoctorId' => 2, 'PatientId' => 2, 'AppointmentDate' => '2021-10-29'], ['DoctorId' => 3, 'PatientId' => 3, 'AppointmentDate' => '2023-12-09'] ]; foreach ($appointmentsData as $row) { insertIntoTable($mysqli, 'PatientAppointments', ['PatientId', 'DoctorId', 'AppointmentDate'], [$row['PatientId'], $row['DoctorId'], $row['AppointmentDate']]); } $result = $mysqli->query('SELECT * FROM `PatientAppointments`'); // запрос на выборку while($row = $result->fetch_assoc()) { echo 'Запись id='.$row['AppointmentDate']."\n"; // выводим данные }
Copy Clear