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); //$q->bind_param("sis", $row['Name'], $row['Age'], $row['Gender']); //$stmt = $q->execute(); //if ($stmt === false) { // die(print_r(sqlsrv_errors(), true)); //} //$q = $mysqli->prepare($sql); if ($q) { //$bindTypes = "sis"; $bindTypes = str_repeat("s", count($columnNames)); // Assuming all values are strings echo "bindTypes = " . $bindTypes . "\n"; $bindParams = array_merge([$bindTypes], $data); echo "bindParams = " . $bindParams . "\n"; $refs = []; foreach ($bindParams as $key => $value) { $refs[$key] = &$bindParams[$key]; } call_user_func_array([$q, 'bind_param'], $refs); if ($q->execute() === false) { die(print_r($q->error, true)); } $q->close(); } else { die("Failed to prepare the SQL query."); } } $patients = [ ['Name' => 'Иванов', 'Age' => 30, 'Gender' => 'М'], ['Name' => 'Воробьева', 'Age' => 25, '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)); // } //} $result = $mysqli->query('SELECT * FROM `Patients`'); // запрос на выборку while($row = $result->fetch_assoc()) { echo 'Запись id='.$row['Name']."\n"; // выводим данные } $doctor_specialties = [ ['Specialty' => 'Терапевт'], ['Specialty' => 'Хирург'], ['Specialty' => 'Ортодонт'] // ... другие специальности ]; foreach ($doctor_specialties as $row) { $q = mysqli_prepare( $mysqli, "INSERT INTO `DoctorSpecialties` (`Specialty`) VALUES (?)"); $q->bind_param("s", $row['Specialty']); $stmt = $q->execute(); if ($stmt === false) { die(print_r(sqlsrv_errors(), true)); } } //$result = $mysqli->query('SELECT * FROM `DoctorSpecialties`'); // запрос на выборку //while($row = $result->fetch_assoc()) { // echo 'Запись id='.$row['SpecialtyId'].' '.$row['Specialty']."\n"; // выводим данные //} $doctors = [ ['Name' => 'Масалимов', 'SpecialtyId' => 1], ['Name' => 'Фатхуллин', 'SpecialtyId' => 2], ['Name' => 'Васнецов', 'SpecialtyId' => 3] // ... другие врачи ]; foreach ($doctors as $row) { $q = mysqli_prepare( $mysqli, "INSERT INTO `Doctors` (`Name`, `SpecialtyId`) VALUES (?, ?)"); $q->bind_param("si", $row['Name'], $row['SpecialtyId']); $stmt = $q->execute(); if ($stmt === false) { die(print_r(sqlsrv_errors(), true)); } } // Заполнение таблицы записей пациентов к врачам $appointmentsData = [ ['DoctorId' => 1, 'PatientId' => 1, 'AppointmentDate' => '2022-03-08'], ['DoctorId' => 2, 'PatientId' => 2, 'AppointmentDate' => '2021-10-22'], ['DoctorId' => 3, 'PatientId' => 3, 'AppointmentDate' => '2023-12-03'] ]; // Заполнение таблицы записей пациентов к врачам foreach ($appointmentsData as $row) { // Вставляем запись в базу данных $q = mysqli_prepare( $mysqli, "INSERT INTO `PatientAppointments` (`PatientId`, `DoctorId`, `AppointmentDate`) VALUES (?, ?, ?)"); $q->bind_param("iis", $row['PatientId'], $row['DoctorId'], $row['AppointmentDate']); $stmt = $q->execute(); if ($stmt === false) { die(print_r(sqlsrv_errors(), true)); } }
Copy Clear