php 表单未提交,页面刚刚重新加载
php form not submitting, page just reloades
我无法提交表单。我用类似的代码提交了一个不同的表单并且它工作正常,我只是不明白为什么这次不起作用。
我没有收到任何错误。我尝试了错误报告,但没有收到任何错误。表单输入是粘性的,因此页面重新加载并且输入仍然存在。
这是我的 html 表格:
<form action="evenement_maken.php" method="POST" enctype="multipart/form-data">
<input type="text" name="ev_naam" class="input-lg form-control" value='<?php echo (isset($_POST['ev_naam']) ? $_POST['ev_naam'] : "" );?>'>
<input type="text" name="ev_datum">
<input type="text" name="ev_adres" class="input-lg form-control" placeholder="Vul hier het adres van het evenement in..." value='<?php echo (isset($_POST['ev_adres']) ? $_POST['ev_adres'] : "" );?>'>
<textarea class="input-lg form-control" rows="10" name="ev_omschrijving" id="textarea" placeholder="Korte omschrijving van het evenement...">
<?php
if(isset($_POST['ev_omschrijving'])){
echo htmlentities($_POST['ev_omschrijving'], ENT_QUOTES);
}
?>
</textarea>
<button type="submit" class="pull-right btn btn-danger" name="submit">Opslaan</button>
</form>
还有我的 php 代码:
<?php
$ev_naam = $ev_datum = $ev_omschrijving = $ev_adres = "";
if(isset($_POST['submit'])) {
$ev_naam = mysqli_real_escape_string($conn, $_POST['ev_naam']);
$ev_datum = mysqli_real_escape_string($conn, $_POST['ev_datum']);
$ev_omschrijving = mysqli_real_escape_string($conn, $_POST['ev_omschrijving']);
$ev_adres = mysqli_real_escape_string($conn, $_POST['ev_adres']);
if ($ev_naam=='') {
echo "<script>alert('Vul alsjeblieft alle velden in!')</script>";
exit();// zorgt ervoor dat de rest van het script niet wordt uitgevoerd
} else {
$insert_evenementen = "INSERT INTO evenementen (ev_naam,ev_datum,ev_omschrijving,ev_adres)
VALUES ( '$ev_naam','$ev_datum','$ev_omschrijving','$ev_adres')";
$run_evenementen = mysqli_query($conn, $insert_evenementen);
if (mysqli_query($conn, $insert_evenementen)) {
echo "<script>alert('Post is succesvol opgeslagen!')</script>";
echo "<script>window.open('evenement_maken.php','_self')</script>";
}
}
}
?>
这是正确提交的表单(仅将 img 上传到 ftp 无效):
<form action="post_maken.php" method="post" enctype="multipart/form-data">
<h4>Titel: </h4>
<input type="text" name="post_titel" class="input-lg form-control" value='<?php echo (isset($_POST['post_titel']) ? $_POST['post_titel'] : "" );?>' required>
<h4>Inhoud: </h4>
<textarea class="input-lg form-control" rows="10" name="post_inhoud" id="textarea" required>
if(isset($_POST['post_inhoud'])){
echo htmlentities($_POST['post_inhoud'], ENT_QUOTES);
}
?>
</textarea>
<h4>Categorie:</h4>
<select class="form-control" name="categorie_id" >
<option value="null" >selecteer een categorie...</option>
<?php
$categorie = mysqli_query($conn, "SELECT * FROM categorie");
while ($cat_row=mysqli_fetch_array($categorie, MYSQLI_ASSOC)) {
$cat_naam=$cat_row['cat_naam'];
echo "<option value='$cat_naam'>$cat_naam</option>";
}
</select>
<h4>Afbeelding toevoegen</h4>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
Zoeken…
</span>
</span>
</div>
<input type="file" name="post_img"/>
<p class="help-block">Voeg een afbeelding voor je blogpost toe.</p>
<br>
<button type="submit" class="pull-right btn btn-danger" name="submit">Opslaan</button>
</form>
和 php 代码:
<?php
$post_titel = $post_datum = $post_inhoud = $categorie_id = "";
if(isset($_POST['submit'])) {
$post_titel = mysqli_real_escape_string($conn, $_POST['post_titel']);
$post_datum = mysqli_real_escape_string($conn, date('m-d-y'));
$post_inhoud = mysqli_real_escape_string($conn, $_POST['post_inhoud']);
$categorie_id = mysqli_real_escape_string($conn, $_POST['categorie_id']);
$post_img = mysqli_real_escape_string($conn, $_FILES['post_img']['name']);
$post_img_tmp = mysqli_real_escape_string($conn, $_FILES['post_img']['tmp_name']);
if ($post_titel=='' || $categorie_id=='null' || $post_inhoud=='') {
echo "<script>alert('Vul alsjeblieft alle velden in!')</script>";
exit();
} else {
move_uploaded_file($post_img_tmp, "post_img/$post_img");
$post_bron = 0;
$post_datum = date("y-m-d");
$insert_posts = "INSERT INTO post (post_title,post_inhoud,post_datum,categorie_id, post_img, post_bron)
VALUES ( '$post_titel','$post_inhoud','$post_datum','$categorie_id','$post_img','$post_bron')";
$run_posts = mysqli_query($conn, $insert_posts);
if (mysqli_query($conn, $insert_posts)) {
echo "<script>alert('Post is succesvol opgeslagen!')</script>";
echo "<script>window.open('post_maken.php','_self')</script>";
}
}
}
?>
我正在从两个数据库(连接有效)中检索值并将其显示在网站上,这也有效。我正在使用 bootstrap 3.
我的数据库截图table:
谁能看出我做错了什么?我已经盯着这个看了好几个小时了。
您可能遇到了 SQL 错误。当 mysqli_erros 出现时检查并输出总是好的。切换代码以执行此操作
if (mysqli_query($conn, $insert_evenementen)) {
// query was succesful
}else{
echo mysqli_erro($cnon); // sthing went wrong
}
您的代码对 SQL 注入开放,我建议您查看准备好的语句
<?php
//procedural example from http://php.net/manual/en/mysqli.prepare.php
$city = "Amersfoort";
/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "SELECT District FROM City WHERE Name=?")) {
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "s", $city);
/* execute query */
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $district);
/* fetch value */
mysqli_stmt_fetch($stmt);
printf("%s is in district %s\n", $city, $district);
/* close statement */
mysqli_stmt_close($stmt);
}
我无法提交表单。我用类似的代码提交了一个不同的表单并且它工作正常,我只是不明白为什么这次不起作用。
我没有收到任何错误。我尝试了错误报告,但没有收到任何错误。表单输入是粘性的,因此页面重新加载并且输入仍然存在。
这是我的 html 表格:
<form action="evenement_maken.php" method="POST" enctype="multipart/form-data">
<input type="text" name="ev_naam" class="input-lg form-control" value='<?php echo (isset($_POST['ev_naam']) ? $_POST['ev_naam'] : "" );?>'>
<input type="text" name="ev_datum">
<input type="text" name="ev_adres" class="input-lg form-control" placeholder="Vul hier het adres van het evenement in..." value='<?php echo (isset($_POST['ev_adres']) ? $_POST['ev_adres'] : "" );?>'>
<textarea class="input-lg form-control" rows="10" name="ev_omschrijving" id="textarea" placeholder="Korte omschrijving van het evenement...">
<?php
if(isset($_POST['ev_omschrijving'])){
echo htmlentities($_POST['ev_omschrijving'], ENT_QUOTES);
}
?>
</textarea>
<button type="submit" class="pull-right btn btn-danger" name="submit">Opslaan</button>
</form>
还有我的 php 代码:
<?php
$ev_naam = $ev_datum = $ev_omschrijving = $ev_adres = "";
if(isset($_POST['submit'])) {
$ev_naam = mysqli_real_escape_string($conn, $_POST['ev_naam']);
$ev_datum = mysqli_real_escape_string($conn, $_POST['ev_datum']);
$ev_omschrijving = mysqli_real_escape_string($conn, $_POST['ev_omschrijving']);
$ev_adres = mysqli_real_escape_string($conn, $_POST['ev_adres']);
if ($ev_naam=='') {
echo "<script>alert('Vul alsjeblieft alle velden in!')</script>";
exit();// zorgt ervoor dat de rest van het script niet wordt uitgevoerd
} else {
$insert_evenementen = "INSERT INTO evenementen (ev_naam,ev_datum,ev_omschrijving,ev_adres)
VALUES ( '$ev_naam','$ev_datum','$ev_omschrijving','$ev_adres')";
$run_evenementen = mysqli_query($conn, $insert_evenementen);
if (mysqli_query($conn, $insert_evenementen)) {
echo "<script>alert('Post is succesvol opgeslagen!')</script>";
echo "<script>window.open('evenement_maken.php','_self')</script>";
}
}
}
?>
这是正确提交的表单(仅将 img 上传到 ftp 无效):
<form action="post_maken.php" method="post" enctype="multipart/form-data">
<h4>Titel: </h4>
<input type="text" name="post_titel" class="input-lg form-control" value='<?php echo (isset($_POST['post_titel']) ? $_POST['post_titel'] : "" );?>' required>
<h4>Inhoud: </h4>
<textarea class="input-lg form-control" rows="10" name="post_inhoud" id="textarea" required>
if(isset($_POST['post_inhoud'])){
echo htmlentities($_POST['post_inhoud'], ENT_QUOTES);
}
?>
</textarea>
<h4>Categorie:</h4>
<select class="form-control" name="categorie_id" >
<option value="null" >selecteer een categorie...</option>
<?php
$categorie = mysqli_query($conn, "SELECT * FROM categorie");
while ($cat_row=mysqli_fetch_array($categorie, MYSQLI_ASSOC)) {
$cat_naam=$cat_row['cat_naam'];
echo "<option value='$cat_naam'>$cat_naam</option>";
}
</select>
<h4>Afbeelding toevoegen</h4>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
Zoeken…
</span>
</span>
</div>
<input type="file" name="post_img"/>
<p class="help-block">Voeg een afbeelding voor je blogpost toe.</p>
<br>
<button type="submit" class="pull-right btn btn-danger" name="submit">Opslaan</button>
</form>
和 php 代码:
<?php
$post_titel = $post_datum = $post_inhoud = $categorie_id = "";
if(isset($_POST['submit'])) {
$post_titel = mysqli_real_escape_string($conn, $_POST['post_titel']);
$post_datum = mysqli_real_escape_string($conn, date('m-d-y'));
$post_inhoud = mysqli_real_escape_string($conn, $_POST['post_inhoud']);
$categorie_id = mysqli_real_escape_string($conn, $_POST['categorie_id']);
$post_img = mysqli_real_escape_string($conn, $_FILES['post_img']['name']);
$post_img_tmp = mysqli_real_escape_string($conn, $_FILES['post_img']['tmp_name']);
if ($post_titel=='' || $categorie_id=='null' || $post_inhoud=='') {
echo "<script>alert('Vul alsjeblieft alle velden in!')</script>";
exit();
} else {
move_uploaded_file($post_img_tmp, "post_img/$post_img");
$post_bron = 0;
$post_datum = date("y-m-d");
$insert_posts = "INSERT INTO post (post_title,post_inhoud,post_datum,categorie_id, post_img, post_bron)
VALUES ( '$post_titel','$post_inhoud','$post_datum','$categorie_id','$post_img','$post_bron')";
$run_posts = mysqli_query($conn, $insert_posts);
if (mysqli_query($conn, $insert_posts)) {
echo "<script>alert('Post is succesvol opgeslagen!')</script>";
echo "<script>window.open('post_maken.php','_self')</script>";
}
}
}
?>
我正在从两个数据库(连接有效)中检索值并将其显示在网站上,这也有效。我正在使用 bootstrap 3.
我的数据库截图table:
谁能看出我做错了什么?我已经盯着这个看了好几个小时了。
您可能遇到了 SQL 错误。当 mysqli_erros 出现时检查并输出总是好的。切换代码以执行此操作
if (mysqli_query($conn, $insert_evenementen)) {
// query was succesful
}else{
echo mysqli_erro($cnon); // sthing went wrong
}
您的代码对 SQL 注入开放,我建议您查看准备好的语句
<?php
//procedural example from http://php.net/manual/en/mysqli.prepare.php
$city = "Amersfoort";
/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "SELECT District FROM City WHERE Name=?")) {
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "s", $city);
/* execute query */
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $district);
/* fetch value */
mysqli_stmt_fetch($stmt);
printf("%s is in district %s\n", $city, $district);
/* close statement */
mysqli_stmt_close($stmt);
}