我不明白为什么我不能插入数据库。 我已经使用此表创建了一个数据库:
prenotazione(id,nome_rich,cognome_rich,email_rich,oggetto_rich)
interni(id,nome_int,cognome_int,email_int)
esterni(id,nome_est,cognome_est,email_est)
这是我的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="name" size="20"><br>
Cognome:<input type="text" name="surname" size="20"><br>
Email: <input type="email" name="email" size="20"><br>
Oggetto Conferenza:<br><textarea name="testo" rows="5" cols="40" placeholder="Specificare oggetto Videoconferenza"></textarea><br>
<br>
<b>Partecipanti Interni</b>
<br>
<br>
<div id="start">
<div id="first">
Nome:<input type="text" name="iname[]" size="20"><br>
Cognome: <input type="text" name="isurname[]" size="20"><br>
Email: <input type="email" name="iemail[]" size="20"><br>
<br>
</div>
</div>
<br>
Numero partecipanti interni:
<input type="text" id="n1" value="1"><br>
<button><a href="#" id="add">Aggiungi partecipante</a></button>
<br>
<b>Partecipanti Esterni</b>
<br>
<br>
Numero partecipanti Esterni:
<input type="text" id="n2" value="1"><br>
<button><a href="#" id="add2">Aggiungi partecipante</a></button>
<div id="start2">
<div id="first2">
Nome:<input type="text" name="ename[]" size="20"><br>
Cognome: <input type="text" name="esurname[]" size="20"><br>
Email: <input type="email" name="eemail[]" size="20"><br>
<br>
</div>
</div>
<input type="submit" value="Invia" >
</form>
</body>
</html>
这是我用来插入数据(以及连接到DB)的input.php
<?php
$conn = @pg_connect("dbname=postgres user=postgres password=123456789");
if(!$conn) {
die('Connessione fallita !<br />');
} else {
echo 'Connessione riuscita !<br />';
}
// Richiedente
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$testo = $_POST['testo'];
// Interni
if($_POST['iname']) foreach($_POST['iname'] as $iname)
if($_POST['isurname']) foreach($_POST['isurname'] as $isurname)
if($_POST['iemail']) foreach($_POST['iemail'] as $iemail)
// Esterni
if($_POST['ename']) foreach($_POST['ename'] as $ename)
if($_POST['esurname']) foreach($_POST['esurname'] as $esurname)
if($_POST['eemail']) foreach($_POST['eemail'] as $eemail)
//inserting data order
INSERT INTO prenotazioni (id,nome_rich, cognome_rich, email_rich,oggetto_rich)
VALUES (1,'$name','$surname', '$email','$testo');
INSERT INTO interni (nome_int, cognome_int, email_int)
VALUES ('$iname','$isurname','$iemail');
INSERT INTO esterni (nome_est, cognome_est, email_est)
VALUES ('$ename','$esurname','$eemail');
?>
utils.js
$(document).ready(function() {
$("#add").click(function(){
var val1 =$("#n1").val();
for(var i=0;i<val1;i++){
$("#start").append($("#first").clone());
}
});
});
$(document).ready(function() {
$("#add2").click(function(){
var val2 =$("#n2").val();
for(var i=0;i<val2;i++){
$("#start2").append($("#first2").clone());
}
});
});
我已经测试了与数据库的连接,并且我收到了肯定的消息(在echo中)。但是如果我控制这些表,它们是空的。
答案 0 :(得分:1)
尝试以下代码
// Richiedente
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$testo = $_POST['testo'];
//Connecting to db here
$conn_string = "host=localhost port=5432 dbname=test user=lamb password=bar"; // change the db credentials here
$conn = pg_connect($conn_string);
//inserting data order
$query1 = "INSERT INTO prenotazioni (id,nome_rich, cognome_rich, email_rich,oggetto_rich) VALUES (1,'$name','$surname', '$email','$testo')";
//execute the query here
$result = pg_query($conn, $query1 ); //if you are using pg_query and $conn is the connection resource
// Interni
$query = "";
if( !empty( $_POST['iname'] ) ) {
foreach( $_POST['iname'] as $key => $iname ) {
$isurname = empty( $_POST[$key]['isurname'] ) ? NULL : $_POST[$key]['isurname'];
$iemail = empty( $_POST[$key]['iemail'] ) ? NULL : $_POST[$key]['iemail'];
$query .= " ( '$iname', '$isurname', '$iemail' ) ";
}
}
if( !empty( $query ) ) {
$query2 = "INSERT INTO interni (nome_int, cognome_int, email_int) VALUES ".$query;
$result = pg_query($conn, $query2 ); //if you are using pg_query and $conn is the connection resource
}
// Esterni
$query = "";
if( !empty( $_POST['ename'] ) ) {
foreach( $_POST['ename'] as $key => $ename ) {
$esurname = empty( $_POST[$key]['esurname'] ) ? NULL : $_POST[$key]['esurname'];
$eemail = empty( $_POST[$key]['eemail'] ) ? NULL : $_POST[$key]['eemail'];
$query .= " ( '$ename', '$esurname', '$eemail' ) ";
}
}
if( !empty( $query ) ) {
$query3 = "INSERT INTO esterni (nome_est, cognome_est, email_est) VALUES " . $query;
$result = pg_query($conn, $query3 ); //if you are using pg_query and $conn is the connection resource
}
此代码正试图将其批量插入数据库。尝试回显查询字符串,以查看创建的sql查询并在PG上运行该查询,以查看查询是否存在任何问题。