表单字段为空时无法让我的PHP显示错误
我正在通过一个表单制作PHP日志,但无论我做什么,它总是显示为已成功发布。我尝试过使用empty和null,但我不确定我做错了什么 以下是表单的HTML:表单字段为空时无法让我的PHP显示错误,php,forms,Php,Forms,我正在通过一个表单制作PHP日志,但无论我做什么,它总是显示为已成功发布。我尝试过使用empty和null,但我不确定我做错了什么 以下是表单的HTML: <form action="journal_post.php" method="post"> <p>Give your entry a title.</p> <input class="input" type="text" name="title" /> <p>
<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>
以下是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);
?>
更改:
if($title !=''||$entry !=''){
致:
您需要检查$title和$entry是否都不是空的,而不是或。在插入数据库之前,您肯定应该使用isset检查字段
<textarea class="input" name="entry" rows="12" cols="75" type="text"> </textarea>
实际上在条目中添加空格,将其更改为
<textarea class="input" name="entry" rows="12" cols="75" type="text"></textarea>
@这个答案有用吗?如果有,请接受。如果没有,请反馈?
<textarea class="input" name="entry" rows="12" cols="75" type="text"></textarea>