R
R
Rinat Ilyasov2021-12-03 10:20:38
PHP
Rinat Ilyasov, 2021-12-03 10:20:38

It is not possible to enter data from the form into the database, helpnet?

<form action="connect.php" method="post">
                    <label for="fname">Имя</label>
                    <input type="text" id="fname" name="first_name" placeholder="Имя..">
                    <label for="lname">Фамилия</label>
                    <input type="text" id="last_name" name="last_name" placeholder="Фамилия..">
                    <label for="lname">Почта</label>
                    <input type="text" id="email" name="email" placeholder="Почта..">
                    <label for="lname">Книги</label>
                    <input type="text" id="books" name="books" placeholder="Книги..">
                    <label for="lname">Срок книги</label>
                    <input type="text" id="term" name="term" placeholder="Напишите срок..">
                    <label for="country">Проживание</label>
                    <select id="country" name="country">
                        <option value="australia">Россия</option>
                        <option value="canada">Алматы</option>
                        <option value="usa">Казахстан</option>
                    </select>
                    <label for="subject">Описание</label>
                    <textarea id="subject" name="subject" placeholder="Напишите что-то.."
                        style="height:170px"></textarea>
                    <input type="submit" value="Отправить" name='sendFrom'>
                </form>


if(isset($_GET['sendForm'])){

    $first_name =  $_POST['first_name'];
    $last_name =  $_POST['last_name'];
    $email =  $_POST['email'];
    $books =  $_POST['books'];
    $term =  $_POST['term'];
    $country =  $_POST['country'];
    $subject =  $_POST['subject'];

    $host = 'localhost';
    $username = 'root';
    $password = '';
    $db_name = 'library_db';

    $connection = mysqli_connect($host, $username, $password, $db_name);
     if (!$connection) {
          echo 'Не могу соединиться с БД. Код ошибки: ' . mysqli_connect_errno() . ', ошибка: ' . mysqli_connect_error();
          exit;
        }

    $nameSql = '"'.$mysqli->real_escape_string($first_name).'"';
    $surnameSql = '"'.$mysqli->real_escape_string($last_name).'"';
    $emailSql = '"'.$mysqli->real_escape_string($emailSql).'"';
    $booksSql = '"'.$mysqli->real_escape_string($booksSql).'"';
    $termSql = '"'.$mysqli->real_escape_string($termSql).'"';
    $countrySql = '"'.$mysqli->real_escape_string($country).'"';
    $subjectSql = '"'.$mysqli->real_escape_string($subject).'"';

    $query = "INSERT INTO `users` (`first_name`, `last_name`, `email`, `books`, `term`, `country`, `subject`) VALUES (`{$nameSql}`, `{$surnameSql}`, `{$emailSql}`, `{$booksSql}`, `{$termSql}`, `{$countrySql}`, `{$subjectSql}`)";
    $result = $connection->prepare($query);
    $result->bind_param($nameSql, $surnameSql, $emailSql, $booksSql, $termSql, $countrySql, $subjectSql);
    $result->execute();

    if(mysqli_query($connection, $result)){
        echo "Запись добавлена!";
    }

    mysqli_close($connection);


Just don't swear :D

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2021-12-03
@FanatPHP

https://qna.habr.com/q/918033#answer_1847841

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question