Мне нужно сделать форму регистрации и форму для ввода данных. Имеется кривое апи, которое умеет делать только один пост запрос. При попытке добавить второй в этот же запрос после ввода данных выдаёт ошибку, суть которой в том, что данные из другой таблицы не могут быть равны нулю
Php api
<?php
$user = file_get_contents('php://input');
$method = $_SERVER['REQUEST_METHOD'];
switch($method) {
// case "POST":
// $sign_in = json_decode(file_get_contents('php://input'));
// $sql = "INSERT INTO users(id, email, password) VALUES(null, :email, :password)";
// $stmt = $conn->prepare($sql);
// $stmt->bindParam(':email', $sign_in->Email);
// $stmt->bindParam(':password', $sign_in->password);
case "GET":
$sql = "SELECT * FROM diplom";
$stmt = $conn->prepare($sql);
$stmt->execute();
$diplom = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($diplom);
break;
case "POST":
$user = json_decode( file_get_contents('php://input') );
$sql = "INSERT INTO diplom(id, SecondName, Name, Surname, Email, Phone, Birthday, About) VALUES(null, :SecondName, :Name, :Surname, :Email, :Phone, :Birthday, :About)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':SecondName', $user->SecondName);
$stmt->bindParam(':Name', $user->Name);
$stmt->bindParam(':Surname', $user->Surname);
$stmt->bindParam(':Email', $user->Email);
$stmt->bindParam(':Phone', $user->Phone);
$stmt->bindParam(':Birthday', $user->Birthday);
$stmt->bindParam(':About', $user->About);
if($stmt->execute()) {
$response = ['status' => 1, "message" => "record created successfully"];
} else {
$response = ['status' => 0, "message" => "failed to create record"];
}
break;
$user = json_decode( file_get_contents('php://input') );
$sql_1 = "INSERT INTO users(id, email, password) VALUES(null, :sign_email, :sign_pass)";
$stmt_1 = $conn->prepare($sql_1);
$stmt_1->bindParam(':email', $user->sign_email);
$stmt_1->bindParam(':password', $user->sign_pass);
if($stmt_1->execute()) {
$response = ['status' => 1, "message" => "record created successfully"];
} else {
$response = ['status' => 0, "message" => "failed to create record"];
}
}
?>
Первая форма react
const navigate = useNavigate();
const [inputs, setInputs] = useState({})
const handleChange = (event) => {
const name = event.target.name;
const value = event.target.value;
setInputs(values => ({...values, [name]: value}));
}
const handleSubmit = (event) => {
event.preventDefault();
axios.post('https://mana.tw1.su/api/user/save', inputs).then(function(response){
console.log(response.data);
navigate('/success');
});
}
<Form onSubmit={handleSubmit}>
<fieldset>
<Form.Group className="mb-3">
<Form.Label htmlFor="SecondNameInput">Введите Вашу фамилию</Form.Label>
<Form.Control id="SecondNameInput" placeholder="Фамилия" name="SecondName" onChange={handleChange}/>
</Form.Group>
<Form.Group className="mb-3">
<Form.Label htmlFor="NameInput">Введите Ваше имя</Form.Label>
<Form.Control id="NameInput" placeholder="Имя" name="Name" onChange={handleChange}/>
</Form.Group>
<Form.Group className="mb-3">
<Form.Label htmlFor="SurnameInput">Введите Ваше отчество (при наличии)</Form.Label>
<Form.Control id="SurnameInput" placeholder="Отчество" name="Surname" onChange={handleChange}/>
</Form.Group>
<Form.Group className="mb-3">
<Form.Label htmlFor="EmailInput">Email</Form.Label>
<Form.Control id="EmailInput" type="email" placeholder="name@example.com" name="Email" onChange={handleChange}/>
</Form.Group>
<Form.Group className="mb-3">
<Form.Label htmlFor="NumberInput">Номер телефона</Form.Label>
<Form.Control id="NumberInput" type="tel" name="Phone" placeholder="+79881236790" onChange={handleChange}/>
</Form.Group>
<Form.Group className="mb-3">
<Form.Label htmlFor="DateInput">Дата рождения</Form.Label>
<Form.Control id="DateInput" type="date" name="Birthday" onChange={handleChange}/>
</Form.Group>
<Form.Group className="mb-3">
<Form.Label htmlFor="TextInput">Опишите Вашу трудовую деятельность</Form.Label>
<Form.Control id="TextInput" as="textarea" rews={5} name="About" onChange={handleChange}/>
</Form.Group>
<Form.Group className="mb-3">
<Checked/>
</Form.Group>
</fieldset>
</Form>
Вторая форма react
const navigate = useNavigate();
const [inputs, setInputs] = useState({})
const handleChange = (event) => {
const name = event.target.name;
const value = event.target.value;
setInputs(values => ({...values, [name]: value}));
}
const handleSubmit = (event) => {
event.preventDefault();
axios.post('https://mana.tw1.su/api/user/sign_in', inputs).then(function(response){
console.log(response.data);
// navigate('/');
});
<Form onSubmit={handleSubmit} className="sign_in">
<Form.Group className="mb-3">
<Form.Label htmlFor="login">Email:</Form.Label>
<Form.Control id="sign_email" type="email" placeholder="name@example.com" name="sign_email" onChange={handleChange} autoComplete="username"/>
</Form.Group>
<Form.Group className="mb-3">
<Form.Label htmlFor="pass">Пароль:</Form.Label>
<Form.Control id="sign_pass" type="password" placeholder="1234" name="sign_pass" onChange={handleChange} autoComplete="current-password"/>
</Form.Group>
<Form.Group>
<Button className="Checkedbtn" type='submit'>Отправить заявку</Button>
</Form.Group>
</Form>