dtzd65908 2015-10-30 23:55
浏览 6

当表单字段为空时,无法让我的PHP显示错误

I am making a PHP Journal through a form, but no matter what I do, it always comes up as successfully posted. I've tried using empty() and null but I'm not sure what I'm doing wrong.

Here is the HTML for the form:

<form action="journal_post.php" method="post">
    <p>Give your entry a title.</p>
    <input class="input" type="text" name="title" />
    <p>What is your mood today?</p>
    <input class="input" type="text" name="mood" />
    <p>Now please tell me about your day.</p>
    <textarea class="input" name="entry" rows="12" cols="75" type="text"> </textarea>
    <br>
    <br>
    <input class="submit" type="image" src="submit.png" name="submit" value="Submit" /> 
<form>

Here is the PHP:

<?php
$servername = "localhost";
$username = "root";
$password = "blahblah";
debug_to_console($password);
//Establishing Connection with Server
$connection = mysql_connect($servername, $username, $password);

//Selecting Database from Server
$journal_db = mysql_select_db("journal_db", $connection);
if(isset($_POST['submit'])){
    //Fetching variables of the form which travels in URL
    $title = $_POST['title'];
    $mood = $_POST['mood'];
    $entry = $_POST['entry'];
    if($title !=''||$entry !=''){
        //Insert Query of SQL
        $query = mysql_query("insert into entrys(j_title, j_mood, j_entry) values ( '$title', '$mood', '$entry')");

        echo "<br/><br/><span>Journal entry recorded..!!</span>";
    }
    else{
        echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
    }
}
//Closing Connection with Server
mysql_close($connection);
?>
  • 写回答

3条回答 默认 最新

  • dpxw7293 2015-10-30 23:59
    关注

    Change:

    if($title !=''||$entry !=''){
    

    to:

    if($title !=''&&$entry !=''){
    

    You want to check that both $title and $entry aren't empty, not either-or.

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题