用于编辑存储记录不起作用的空字段验证

时间:2014-01-23 14:15:48

标签: edit

我已经连接到我的数据库来编辑存储的记录,我已经使用了一个变量来处理错误。

当我尝试使用空字段提交时,它会重定向并显示更改已保存的消息,而不是显示空字段消息

我确定我的循环结构有问题,而且我把东西放错了地方?

<?php
//session_start();
// connect to the database
include('connect.php');
$message = $_GET['message'];
// check if the form has been submitted then process it
if (isset($_POST['submit']))
{
// Get data from table
//set the id manually for test purposes
$id = "429";
$forename = $_POST['forename'];
$surname = $_POST['surname'];
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
// check for empty fields and display error message
if (empty($forename) &&!empty($surname) &&!empty($username) &&!empty ($password) &&!empty ($email))
{
$message = "Please enter data in all fields" ;
header("Location: edit.php?message=$message");
}
else
{
// save the data to the table
mysql_query("UPDATE registration SET forename='$forename', surname='$surname', username='$username', email='$email', password='$password' WHERE id='$id'")
or die(mysql_error()); 
}
// redirecr and display message
$message = "Your changes have been saved";
header("Location: edit.php?message=$message");
exit;
}
$id=429;// this line could have been $id=$_SESSION['id'];
$result = mysql_query("SELECT * FROM registration WHERE id=$id LIMIT 1")
or die(mysql_error()); 
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from the table
$forename = $row['forename'];
$surname = $row['surname'];
$username = $row['username'];
$email = $row['email'];
$password = $row['password'];
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">

<link rel="stylesheet" href="styles/all.css" />
<link rel="stylesheet" href="styles/forms.css" />

<script type="text/javascript" src="javascript/jquery-1.7.1.min.js"></script>

<link href='//fonts.googleapis.com/css?family=Cantora+One' rel='stylesheet' type='text/css'>
<link href='//fonts.googleapis.com/css?family=Voltaire' rel='stylesheet' type='text/css'>
<link href='//fonts.googleapis.com/css?family=Ubuntu:400,500' rel='stylesheet' type='text/css'>

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />

<title>Chocolate Review</title>
<meta name="Description" content="Chocolate Review" />
<meta name="Keywords" content="Chocolate Review" />

</head>

<body>


<div class="container">

<div id="navigation">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="dairymilk.php">Dairy Milk</a></li>
<li><a href="ferrerorocher.php">Ferrero Rocher</a></li>
<li><a href="kitkat.php">Kit Kat</a></li>
<li><a href="mars.php">Mars</a></li>
<li><a href="snickers.php">Snickers</a></li>
<li><a href="twix.php">Twix</a></li>
<li><a href="register.php">Register</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</div>

<form action="" method="post" enctype="multipart/form-data" name="edit" id="editrecord">
<fieldset>
<legend><span class="headingreg">Edit Details</span></legend>

<div class="formreg">

<br style="clear:left;"/>
<label class="editlabel" for="forename">Forename</label><div><input type="text" id="forename" name="forename" class="insetedit" value="<?php echo $forename; ?>"/><br/></div>
<label class="editlabel" for="forename">Surname</label><div><input type="text" name="surname" class="insetedit" value="<?php echo $surname; ?>"/><br/></div>
<label class="editlabel" for="forename">Username</label><div><input type="text" name="username" class="insetedit" value="<?php echo $username; ?>"/><br/></div>
<label class="editlabel" for="forename">Password</label><div><input type="text" name="password" class="insetedit" value="<?php echo $password; ?>"/><br/></div>
<label class="editlabel" for="forename">email</label><div><input type="text" name="email" class="insetedit" value="<?php echo $email; ?>"/><br/></div>

<input type="submit" name="submit" class="submit2" value="submit">
</div>

<?php print $message; ?>

</fieldset>
</form>


<br style="clear:left;"/>
<br style="clear:left;"/>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

试试这个

if (empty($forename) || empty($surname) || empty($username) || empty ($password) || empty ($email))
    {
    $message = "Please enter data in all fields" ;
    header("Location: edit.php?message=$message");
    }