我无法提交表单。我使用类似的代码提交了一个不同的表单,它工作正常,我只是看不出为什么它这次不起作用。
我没有收到任何错误。我尝试过错误报告我没有收到任何错误。表单输入是粘性的,因此页面重新加载,输入仍然存在。
这是我的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.
我的数据库表的屏幕截图:
谁能看到我做错了什么?我一直盯着这几个小时。
答案 0 :(得分:1)
您可能有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);
}