Я учусь программировать с Php.У меня есть HTML-страница индекса с динамической формой, связанной со страницей connection.php, на которой я установил соединение с базой данных, а также где я создаю запрос для вставки данных в базу данных (правильно ли я поступаю?).
Я хочу показать вам код:
index.html
<body>
<form action="connection.php" class="register" method="POST">
<h1>Conferenza</h1>
<fieldset class="row1">
<legend>Informazioni Richiedente Prenotazione</legend>
<p>
<label>Nome
</label>
<input name="nome" type="text" required="required"/>
<label>Cognome
</label>
<input name="cognome" type="text" required="required"/>
<label>Data Videoconferenza
</label>
<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>
<br>
<label>Email
</label>
<input name="email" type="text" required="required"/>
<label>Stanza
</label>
<select name="stanza">
<option value="1">Stanza 1
</option>
<option value="2">Stanza 2
</option>
</select>
</p>
<p>
<label>Oggetto Prenotazione
</label>
<textarea rows="4" cols="50" name="oggetto" form="usrform" placeholder="Inserire Oggetto Conferenza"></textarea>
<br>
</p>
<div class="clear"></div>
</fieldset>
<fieldset class="row2">
<legend>Personale </legend>
<p>
<input type="button" value="Add Passenger" onClick="addRow('dataTable')" />
<input type="button" value="Remove Passenger" onClick="deleteRow('dataTable')" />
<p>(All acions apply only to entries with check marked check boxes only.)</p>
</p>
<table id="dataTable" class="form" border="1">
<tbody>
<tr>
<p>
<td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
<td>
<label>Nome</label>
<input type="text" required="required" name="nome_p[]">
</td>
<td>
<label for="Cognome">Cognome</label>
<input type="text" required="required" name="cognome_p[]">
</td>
<td>
<label for="Email">Email</label>
<input type="text" required="required" name="email_p[]">
</td>
<td>
<label for="Tipologia">Tipologia</label>
<select id="BX_birth" name="tipologia[]" required="required">
<option>Interno</option>
<option>esterno</option>
</select>
</td>
</p>
</tr>
</tbody>
</table>
<div class="clear"></div>
</fieldset>
<input class="submit" type="submit" value="Confirm »" />
<div class="clear"></div>
</form>
</body>
process.php
<body>
<form action="connection.php" class="register">
<h1>YouAreBUS Ticket Reservation</h1>
<?php if(isset($_POST)==true && empty($_POST)==false):
$chkbox = $_POST['chk'];
$nome = $_POST['nome'];
$day = $_POST['day'];
$month = $_POST['month'];
$mob = $_POST['mob'];
$type = $_POST['type'];
$oggetto = $_POST['oggetto'];
$to=$_POST['to'];
$stanza=$_POST['stanza'];
$nome_int=$_POST['nome_int'];
$cognome_int=$_POST['cognome_int'];
$email_int=$_POST['email_int'];
$tipologia=$_POST['tipologia'];
?>
<fieldset class="row1">
<legend>Informazioni Richiedente Prenotazione</legend>
<p>
<label>Nome
</label>
<input name="nome" type="text" readonly="readonly" value="<?php echo $nome ?>"/>
<label>Data Videoconferenza
</label>
<input type="text" readonly="readonly" class="small" value="<?php echo $day ?>"/>
<input type="text" readonly="readonly" class="small" value="<?php echo $month ?>"/>
<input type="text" readonly="readonly" class="small" value="2013"/>
<label>Email
</label>
<input type="text" readonly="readonly" value="<?php echo $email ?>"/>
</p>
<p>
<label>Oggetto Prenotazione
</label>
<input name="oggetto" type="text" readonly="readonly" value="<?php echo $oggetto ?>"/>
<label>Stanza
</label>
<input type="text" readonly="readonly" value="<?php echo $stanza ?>"/>
</p>
<div class="clear"></div>
</fieldset>
<fieldset class="row2">
<legend>Personale
</legend>
<table id="dataTable" class="form" border="1">
<tbody>
<?php foreach($nome_p as $a => $b){ ?>
<tr>
<p>
<td >
<?php echo $a+1; ?>
</td>
<td>
<label>Nome</label>
<input type="text" readonly="readonly" name="nome_p[$a]" value="<?php echo $nome_p[$a]; ?>">
</td>
<td>
<label for="Cognome"></label>
<input type="text" readonly="readonly" class="small" name="cognome_p[]" value="<?php echo $cognome_p[$a]; ?>">
</td>
<td>
<label for="Email">Email</label>
<input type="text" readonly="readonly" name="email_p[]" value="<?php echo $BX_gender[$a]; ?>">
</td>
<td>
<label for="BX_birth">Berth Pre</label>
<input type="text" readonly="readonly" name="tipologia[]" value="<?php echo $tipologia[$a]; ?>">
</td>
</p>
</tr>
<?php } ?>
</tbody>
</table>
<div class="clear"></div>
</fieldset>
<fieldset class="row5">
<legend>Terms and Mailing</legend>
<p>
<input class="submit" type="button" value="Make Payment »" />
<a class="submit" href="index.html" type="button"> Back To Demo <a/>
</p>
<div class="clear"></div>
</fieldset>
<?php else: ?>
<fieldset class="row1">
<legend>Sorry</legend>
<p>Some things went wrong please try again.</p>
</fieldset>
<?php endif; ?>
<div class="clear"></div>
</form>
</body>
А также
connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "123456789";
$dbname = "Project";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(isset($_POST['submit'])) {
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$day = $_POST['day'];
$month = $_POST['month'];
$email = $_POST['email'];
$stanza = $_POST['stanza'];
$oggetto = $_POST['oggetto'];
$nome_p = $_POST['nome_p[]'];
$cognome_p = $_POST['cognome_p[]'];
$email_p = $_POST['email_p[]'];
$tipologia = $_POST['tipologia[]'];
$query = "INSERT into Prenotazione (nome,cognome,day,month,email,stanza,oggetto) values('$nome','$cognome','$day','$month','$email','$stanza','$oggetto')";
$query1 = "INSERT into Personale (nome_p,cognome_p,email_p,tipologia,email,oggetto) values('$nome_p','$cognome_p','$email_p','$tipologia','$email','$oggetto')";
}
if (mysqli_query($conn, $query)) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . mysqli_error($conn);
}
if (mysqli_query($conn, $query1)) {
echo "New record created successfully";
} else {
echo "Error: " . $query1 . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
и scripts.js
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount < 5){ // limit the user from creating fields more than your limits
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
}else{
alert("Maximum Passenger per ticket is 5.");
}
}
function deleteRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) { // limit the user from removing all the fields
alert("Cannot Remove all the Passenger.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}
Моя проблема заключается в вставке в базу данных, посколькузапрос, который я написал, не должен быть правильным.В моей базе данных есть 2 таблицы:
Prenotazione: nome cognome date e-mail stanza oggetto
Personale: nome_p cognome_p email_p tipologia email_richiedente oggetto_richiedente
По вашему мнению, кто может написать запрос ввставить данные из динамической формы?
большое спасибо за вашу помощь!
ОБНОВЛЕНИЕ.Это моя страница, когда я нажимаю кнопку Подтвердить. Изображение