N
N
N!crO2018-02-20 22:39:11
PHP
N!crO, 2018-02-20 22:39:11

How to correctly create a MySQL query?

There are two forms, the first form is required, the second is not. If you just write two queries, then the data from the forms is written to different lines. I need to be able to write the data from the first and second form into one line. But a situation may arise when the user does not fill out the second form, and if he does, you need to insert data from the second form into the line with the data of the first.

<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$birthdate = $_POST['birthdate'];
$repsubject = $_POST['subject'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$company = $_POST['company'];
$position = $_POST['position'];
$about = $_POST['about'];

$connect = new PDO('mysql:host=192.168.1.4;dbname=invitation;charset=utf8', 'n1cro', 'XRT6IPLkeK5DyYzc');

$sql = 'INSERT INTO users '. '(firstname, lastname, birthdate, repsubject, country, phone,'. ' email)'. 'VALUES '. '(:fname, :lname, :bdate, :rsubject, :country, :phone,'. ':email)';

$result = $connect->prepare($sql);

$result -> bindParam(':fname', $firstname, PDO::PARAM_STR);
$result -> bindParam(':lname', $lastname, PDO::PARAM_STR);
$result -> bindParam(':bdate', $birthdate, PDO::PARAM_STR);
$result -> bindParam(':rsubject', $repsubject, PDO::PARAM_STR);
$result -> bindParam(':country', $country, PDO::PARAM_STR);
$result -> bindParam(':phone', $phone, PDO::PARAM_STR);
$result -> bindParam(':email', $email, PDO::PARAM_STR);
$result -> execute();

$result = null;
$connect = null;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rodion, 2018-02-21
@n1croo

After inserting, you need to get the id of the added record.
Further, when displaying the second form, include this id in the form
When processing the second form, you will get this id and it will be clear which record needs to be updated.
It should be borne in mind that the user can change the id and then other records will be updated. To avoid this, you can use sessions or somehow encrypt the value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question