SQL错误未捕获mysqli_sql_exception

时间:2017-06-27 22:52:56

标签: php sql database

请帮帮我! :( 我想在2个表中添加数据,但我有一个问题。 Datas没有添加,sql也不想工作:(

  

未捕获mysqli_sql_exception:您的SQL语法出错;   查看与您的MariaDB服务器版本对应的手册   正确的语法使用附近'和mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); require_once 'functions.php'; $db = get_connect(); if(empty($_POST)){ //Пользователь перешел по ссылке $task = []; $patient = []; if (isset($_GET['id'])) { // получаем инфу по вызову и пациенту с конкретным id $id = (int)$_GET['id']; $task_q = mysqli_query($db, " SELECT * FROM `task` WHERE `id` = {$id} ;"); $task = mysqli_fetch_assoc($task_q); $patient_q = mysqli_query($db, " SELECT * FROM `patient` WHERE `id` = {$id} ;"); $patient = mysqli_fetch_assoc($patient_q); } else { // делаем заглушку для book // $book_q = mysqli_query($db, " // SHOW COLUMNS FROM `book` // ;"); // $book = mysqli_fetch_assoc($book_q); } $status_q = mysqli_query($db, " SELECT * FROM `status` ORDER BY `name` ;"); $statuses = mysqli_fetch_all($status_q, MYSQLI_ASSOC); $user_q = mysqli_query($db, " SELECT CONCAT (`user`.name, ' ', `user`.surname , ' ', `user`.m_name) as u_fullname FROM `user` WHERE `id_role` = 3 ORDER BY `u_fullname` ;"); $users = mysqli_fetch_all($user_q, MYSQLI_ASSOC); view_init('header'); view_init('sidebar'); view_init('top_nav'); view_init('task_add', [ 'task' => $task, 'patient' => $patient, 'users' => $users, 'statuses' => $statuses ]); view_init('footer'); } else { // пользователь отправил форму по пациенту $name = escape($_POST['name']); $surname = escape($_POST['surname']); $m_name = escape($_POST['m_name']); $gender = $_POST['gender']; $dob = (int)($_POST['dob']); $address = escape($_POST['address']); $tel= (int)($_POST['tel']); $id = (int)$_POST['id']; // по задаче $diagnosis= (int)($_POST['diagnosis']); $id_user = (int)($_POST['diagnosis']); $id_status = (int)($_POST['status']); $id = (int)$_POST['id']; if ($id) { // редактирование книги mysqli_query($db, " UPDATE `patient` SET `name` = '{$name}', `surname` = '{$surname}', `m_name` = '{$m_name}', `gender` = '{$gender}', `dob` = {$dob}, `address` = '{$address}', `tel` = {$tel} WHERE `id` = {$id} ;"); mysqli_query($db, " UPDATE `task` SET `diagnosis` = '{$diagnosis}', `id_user` = {$id_user}, `id_status` = {$id_status}, `id_patient` = {$id_patient} WHERE `id` = {$id} ;"); } else { // добавление новой задачи $query_t = mysqli_query($db, " SELECT `id` FROM `task` WHERE `diagnosis` = '{$diagnosis}' AND `id_user` = {$id_user}, AND `id_status` = {$id_status} ;"); $query_p = mysqli_query($db, " SELECT `id` FROM `patient` WHERE `name` = '{$name}' AND `surname` = '{$surname}', AND `m_name` = '{$m_name}', AND `gender` = '{$gender}', AND `dob` = {$dob}, AND `address` = '{$address}', AND `tel` = {$tel} ;"); if (!mysqli_num_rows($query_t)) { mysqli_query($db, " INSERT INTO `task` SET `diagnosis` = '{$diagnosis}', `id_status` = '{$id_status}', `id_user` = '{$id_user}', WHERE `id` = {$id} ;"); } elseif (!mysqli_num_rows($query_p)) { mysqli_query($db, " INSERT INTO `patient` SET `name` = '{$name}', `surname` = '{$surname}', `m_name` = '{$m_name}', `gender` = '{$gender}', `dob` = {$dob}, `address` = '{$address}', `tel` = {$tel}, WHERE `id` = {$id} ;"); } } if (mysqli_error($db)) { echo 'Вызов не был добавлен'; //header('location:tasks.php'); } else { echo 'Вызов добавлен'; } } = 1'在第4行   D:\ xampp \ htdocs \ medhelper \ www \ task_add.php:243堆栈跟踪:#0   d:\ XAMPP \ htdocs中\ medhelper \ WWW \ task_add.php(243):   mysqli_query(对象(mysqli),' \ r \ n S ...')#1 {main}引入   第243行的D:\ xampp \ htdocs \ medhelper \ www \ task_add.php

PHP

 <form action="task_add.php" method="post" id="add">
<div class="modal-body">
    <div class="personal">
        <div class="personal_info">
            <h4>Личные данные пациента</h4>
            <div class="clearfix"></div>
            <div class="row">
                <div class="col-md-6 col-sm-12 col-xs-12 form-group">
                    <input type="hidden" name="id"
                           value="<?= isset($patient['id']) ? $patient['id'] : '' ?>">

                    <label>Имя</label>
                    <input type="text" name="name"
                           class="form-control"
                           id="field"
                           placeholder="Имя">
                </div>
                <div class="col-md-6 col-sm-12 col-xs-12 form-group">
                    <label>Фамилия</label>
                    <input type="text" name="surname"
                           class="form-control"
                           id="field" placeholder="Фамилия">
                </div>
                <div class="col-md-6 col-sm-12 col-xs-12 form-group">
                    <label>Отчество</label>
                    <input type="text" name="m_name"
                           class="form-control" id="field"
                           placeholder="Отчество">
                </div>
                <div class="col-md-6 col-sm-12 col-xs-12 form-group">
                    <label>Дата рождения</label>
                    <input type="date" class="form-control"
                           id="field" name="dob">
                </div>
                <div class="col-md-12 form-group">
                    <label>Пол</label>
                    <div class="radio">
                        <label class="radio-inline">
                            <input type="radio" name="gender" id="optionsRadios1" value="1" checked>
                            Женский
                        </label>
                    </div>
                    <div class="radio">
                        <label class="radio-inline">
                            <input type="radio" name="gender" id="optionsRadios2" value="2">
                            Мужской
                        </label>
                    </div>
                </div>
            </div>
        </div>
        <div class="personal_address">
            <h4>Адрес и телефон</h4>
            <div class="clearfix"></div>
            <div class="row">
                <div class="col-md-6 col-sm-12 col-xs-12 form-group">
                    <label>Адрес</label>
                    <input type="text" name="address"
                           class="form-control" id="field"
                           placeholder="Адрес">
                </div>
                <div class="col-md-6 col-sm-12 col-xs-12 form-group">
                    <label>Телефон</label>
                    <input type="number" name="tel"
                           class="form-control"
                           id="field" placeholder="Телефон">
                </div>
            </div>
        </div>
        <div class="personal_doctor">
            <input type="hidden" name="id"
                   value="<?= isset($task['id']) ? $task['id'] : '' ?>">

            <h4>Жалобы</h4>
            <div class="cleaffix"></div>
            <div class="row">
                <div class="col-md-6 col-sm-12 col-xs-12 form-group">
                    <label>Жалобы</label>
                    <input type="text" class="form-control"
                           id="field" name="diagnosis">
                </div>
                <div class="col-md-6 col-sm-12 col-xs-12 form-group">
                    <label>Врач</label>
                    <select name="user" class="form-control">
                        <option id="user_blank">Выберите врача</option>
                        <? foreach ($users as $user): ?>
                        <option value="<?= $user['id'] ?>"
                        <?= isset($task['u_fullname']) && ($user['id'] == $task['u_fullname']) ? 'selected' : ''?>
                        >
                        <?= $user['u_fullname'] ?>
                        </option>
                    </select>
                    <? endforeach ?>
                </div>

                <div class="col-md-6 col-sm-12 col-xs-12 form-group">
                    <label>Статус вызова</label>
                    <select name="status" class="form-control">
                        <option id="status_blank">Выберите статус</option>
                        <? foreach ($statuses as $status): ?>
                        <option value="<?= $status['id'] ?>" <?= isset($task['id_status']) && ($status['id'] == $task['id_status']) ? 'selected' : ''?> >
                        <?= $status['name'] ?>
                        </option>
                        <? endforeach ?>
                    </select>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Отмена</button>
    <!--<button type="button" class="btn btn-primary">Добавить</button>-->
    <input type="submit" value="Добавить" class="btn btn-primary">
</div>

Html模板 - 表单

WMIC UserAccount Where "Name='%Name%'" Set PasswordExpires=FALSE

表 患者 patient 任务 enter image description here

1 个答案:

答案 0 :(得分:1)

AND条件之间不应该有逗号,所以这个

AND `id_user` = {$id_user},
AND `id_status` = {$id_status}

应该写成

AND `id_user` = {$id_user} 
AND `id_status` = {$id_status}
  

您已在多个查询中完成此操作,因此您需要全部检查

此处的INSERT命令中也存在错误

INSERT INTO `patient` SET
            `name` = '{$name}',
            `surname` = '{$surname}',
            `m_name` = '{$m_name}',
            `gender` = '{$gender}',
            `dob` = {$dob},
            `address` = '{$address}',
            `tel` = {$tel},
 WHERE `id` = {$id}"

INSERT查询中没有WHERE子句,因为您正在创建一个新行,而不是尝试查找要更改的行。所以语法是

INSERT INTO `patient` SET
            `name` = '{$name}',
            `surname` = '{$surname}',
            `m_name` = '{$m_name}',
            `gender` = '{$gender}',
            `dob` = {$dob},
            `address` = '{$address}',
            `tel` = {$tel}"