dongli7870 2017-11-10 05:34
浏览 76
已采纳

PHP表单插入数据库,检查没有输入

My question is, how do I get the form to display an error when the user doesn't input a field into the form. I can get it to work when they don't fill out the form at all and just hit enter, but cannot get it to work if they only fill out a few fields. They could fill out the title field and the run time field and will be able to submit it. How can I get it to make sure that every field is filled out?

<?php
$databaseName = 'movie_database';
$databaseUser = 'root';
$databasePassword = 'root';
$databaseHost = '127.0.0.1';

$conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databaseName);

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

echo "Connected successfully";

if(isset($_POST['submit'])) {
$value = mysqli_real_escape_string($conn,$_POST['title']);
$value2 = mysqli_real_escape_string($conn,$_POST['rating']);
$value3 = mysqli_real_escape_string($conn,(int)$_POST['Runtime']);
$value4 = mysqli_real_escape_string($conn,(float)$_POST['movie_rating']);
$value5 = mysqli_real_escape_string($conn,$_POST['release_date']);
// Checks for empty fields 
if (empty($value) && empty($value2) && empty($value3) && empty($value4) && empty($value5)) {
    echo 'Please correct the fields';
    return false;
}
// Concatenate the $values into the string
$sql = "INSERT INTO movies(title,rating,Runtime,movie_rating,release_date) 
    VALUES('$value','$value2','$value3','$value4','$value5')";

if ($conn->query($sql)) {
    $msg = "New record created successfully";
} else {
    $msg = "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}
?>
<form method="post"/>
<p>Enter Movie Title: <input type="text" name="title"/></p> 
<p>Enter Movie Rating(G,PG,PG-13,R): <input type="text" name="rating"/></p> 
<p>Enter Movie Runtime in minutes: <input type="text" name="Runtime"/></p> 
<p>Enter IMDB Movie Rating(0-10.0): <input type="text" name="movie_rating"/></p> 
<p>Enter Movie Release date(YYYY-MM-DD): <input type="text" name="release_date"/></p>  
<button type="submit" name="submit">Submit</button
</form> 

<?php 
if (isset($msg)) {
    echo $msg;
}
?>

</div>
  • 写回答

2条回答 默认 最新

  • dongmeng1868 2017-11-10 05:40
    关注

    From Front side you can add required attribute to each input. So add it in every input field like : <input type="text" name="title" required/>

    From back-end side (using PHP) If you want all value must be fill up then change && with || in your condition

    // Checks for empty fields 
    if (empty($value) || empty($value2) || empty($value3) || empty($value4) || empty($value5)) {
        echo 'Please correct the fields';
        return false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题