Dynamic insertion in the database with php

Dynamic insertion in the database with php



I'm learning to program in php and I came across a problem that I can not solve.
Basically I have to insert a form into the database, with a dynamic part (in the sense that the user can decide how many fields to insert).
This form will go to populate two Tables: Prenotazione, Personale.
As for the first table, it is populated correctly. The second one is not, that is, only the name is inserted and not the rest. How can I proceed according to you? Where was I wrong? Thank you so much for your help and for your time.



index.php


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<title>Prenotazione Videoconferenza</title>
<script src="utils.js"></script>
</head>

<body>

Inserire i dati richiesti:<br><br>
<form method="post" action="input.php">
<b> Richiedente Conferenza:</b><br><br>
Nome:<input type="text" name="nome" size="20"><br>
Cognome:<input type="text" name="cognome" size="20"><br>
Email: <input type="email" name="email" size="20"><br>
Oggetto Conferenza:<br><textarea name="oggetto" rows="5" cols="40" placeholder="Specificare oggetto Videoconferenza"></textarea><br>
<select class="date" name="day">
<option value="1">01
</option>
<option value="2">02
</option>
<option value="3">03
</option>
<option value="4">04
</option>
<option value="5">05
</option>
<option value="6">06
</option>
<option value="7">07
</option>
<option value="8">08
</option>
<option value="9">09
</option>
<option value="10">10
</option>
<option value="11">11
</option>
<option value="12">12
</option>
<option value="13">13
</option>
<option value="14">14
</option>
<option value="15">15
</option>
<option value="16">16
</option>
<option value="17">17
</option>
<option value="18">18
</option>
<option value="19">19
</option>
<option value="20">20
</option>
<option value="21">21
</option>
<option value="22">22
</option>
<option value="23">23
</option>
<option value="24">24
</option>
<option value="25">25
</option>
<option value="26">26
</option>
<option value="27">27
</option>
<option value="28">28
</option>
<option value="29">29
</option>
<option value="30">30
</option>
<option value="31">31
</option>
</select>
<select name="month">
<option value="1">January
</option>
<option value="2">February
</option>
<option value="3">March
</option>
<option value="4">April
</option>
<option value="5">May
</option>
<option value="6">June
</option>
<option value="7">July
</option>
<option value="8">August
</option>
<option value="9">September
</option>
<option value="10">October
</option>
<option value="11">November
</option>
<option value="12">December
</option>
</select>
<br>
<b>Partecipanti </b>
<br>
<br>
<div id="start">
<div id="first">
Nome:<input type="text" name="nomep" size="20"><br>
Cognome: <input type="text" name="cognomep" size="20"><br>
Email: <input type="email" name="emailp" size="20"><br>
Personale:
<select name="tipologia" required="required">
<option>Interno</option>
<option>Esterno</option>
</select>
<br>
<br>
</div>
</div>
<br>
Numero partecipanti interni:
<input type="text" id="n1" size="1" value="1"><br>

<button><a href="#" id="add">Aggiungi partecipante</a></button>


<input type="submit" value="Invia" >
</form>
</body>
</html>



input.php


<?php
//Connecting to db here

$servername = "localhost";
$username = "root";
$password = "123456789";
$dbname = "Project1";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error)
die("Connection failed: " . $conn->connect_error);

echo "Connected successfully";

// Richiedente
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$email = $_POST['email'];
$oggetto = $_POST['oggetto'];
$day = $_POST['day'];
$month = $_POST['month'];

// Personale
$nomep = $_POST['nomep'];
$cognomep = $_POST['cognomep'];
$emailp = $_POST['emailp'];
$tipologia = $_POST['tipologia'];

//inserting data order
$query1 = "INSERT INTO Prenotazione (nome,cognome,email,oggetto,day,month) VALUES ('$nome','$cognome','$email','$oggetto', $day, $month)";
//execute the query here
$result = mysqli_query($conn, $query1 ); //if you are using pg_query and $conn is the connection resource
// Interni
$query = "";
if( !empty( $_POST['nomep'] ) )

foreach( $_POST['nomep'] as $key => $nomep )

$cognomep = empty( $_POST[$key]['cognomep'] ) ? NULL : $_POST[$key]['cognomep'];
$emailp = empty( $_POST[$key]['emailp'] ) ? NULL : $_POST[$key]['emailp'];
$tipologia = empty( $_POST[$key]['tipologia'] ) ? NULL : $_POST[$key]['tipologia'];
$query .= " ( '$nomep', '$cognomep', '$emailp','$tipologia', '$email', '$oggetto' ) ";


if( !empty( $query ) )

$query2 = "INSERT INTO Personale (nomep, cognomep, emailp, tipologia, email_rich,oggetto_rich) VALUES ".$query;
$result = mysqli_query($conn, $query2 ); //


?>



utils.js


$(document).ready(function()
$("#add").click(function()

var val1 =$("#n1").val();
for(var i=0;i<val1;i++)
$("#start").append($("#first").clone());

);
);



Image






Step through your code line by line, think about what each line does. For example, what is $_POST[$key] going to be?

– miken32
Sep 12 '18 at 21:38


$_POST[$key]






You need a comma after every values grouping e.g. VALUES (...) (...) is invalid SQL. This also is insecure, you are open to SQL injections. The queries should be parameterized. I also would just execute one insert at a time in the for loop.

– user3783243
Sep 12 '18 at 21:38



values


VALUES (...) (...)


insert


for






Warning: You are wide open to SQL Injections and should really use parameterized Prepared Statements instead of manually building your queries like that. Specially since you're not escaping the user inputs at all!

– Magnus Eriksson
Sep 12 '18 at 21:44






The second set of data you fetch (where the names end with ) will be an array in the format ['cognomep' => ['first', 'second', ...], 'emailp' => ['first', 'second', ...], etc] so $_POST[$key]['cognomep'] should be $_POST['cognomep'][$key]. That goes for all those values.

– Magnus Eriksson
Sep 12 '18 at 21:50




['cognomep' => ['first', 'second', ...], 'emailp' => ['first', 'second', ...], etc]


$_POST[$key]['cognomep']


$_POST['cognomep'][$key]






@MagnusEriksson Thank you for the reply! I have tried what you suggest to me and it works! But if add new fields, ( so if i click on " aggiungi Partecipante " ) with a js file so it creates new fields with nomep,cognomep,emailp i can't insert these in the database.

– Kepz23
Sep 12 '18 at 21:58




0



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)