我为我的客户应用创建了一个地址簿。我可以登录和退出。当我点击刷新,重定向以重新登录时,浏览器不会记住这些数据,这一切都很好,但是在添加下面的文件后添加新客户端。我无法这样做。有人可以协助我找出为什么我不能成功添加新客户吗?
<?php
session_start();
// if user is not logged in
if(!$_SESSION['loggedInUser']) {
// send them to the login page
header("Location: index.php");
}
// connect to the database
include('includes/connection.php');
// include functions file
include('includes/functions.php');
// if add button was submitted
if(isset($_POST['add'])) {
// set all variables to empty by default
$clientName = $clientEmail = $clientPhone = $clientAddress =$clientCompany = $clientNotes = "";
// check to see if inputs are empty
// create variables with form data
// wrap the data with our function
if($_POST["clientName"]) {
$nameError = "Please enter a name <br>";
} else {
$clientName = validateFormData($_POST["clientName"]);
}
if($_POST["clientEmail"]) {
$emailError = "Please enter a email <br>";
} else {
$clientEmail = validateFormData($_POST["clientEmail"]);
}
// these inputs are not required
// so we'll just store whatever has been entered
$clientPhone = validateFormData($_POST["clientPhone"]);
$clientAddress = validateFormData($_POST["clientAddress"]);
$clientCompany = validateFormData($_POST["clientCompany"]);
$clientNotes = validateFormData($_POST["clientNotes"]);
// if required fields have data
if($clientName && $clientEmail) {
// create query
$query = "INSERT INTO client(id, name, email, phone, address, company, notes, date_added) VALUES(NULL, '$clientName', '$clientEmail', '$clientPhone', '$clientAddress', 'clientCompany', 'clientNotes', CURRENT_TIMESTAMP)";
$result = mysqli_query($conn, $query);
// if query was successful
if($result) {
// refresh page with query string
header("Location: clients.php?alert=success");
} else {
// something went wrong
echo "Error: ".$query."<br>".mysqli_error($conn);
}
}
}
// close the mysql connection
mysqli_close($conn);
include('includes/header.php');
?>
<h1>Add Client</h1>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" class="row">
<div class="form-group col-sm-6">
<label for="client-name">Name *</label>
<input type="text" class="form-control input-lg" id="client-name" name="clientName" value="">
</div>
<div class="form-group col-sm-6">
<label for="client-name">Email *</label>
<input type="text" class="form-control input-lg" id="client-email" name="clientEmail" value="">
</div>
<div class="form-group col-sm-6">
<label for="client-phone">Phone</label>
<input type="text"
class="form-control input-lg" id="client-phone" name="clientPhone" value="">
</div>
<div class="form-group col-sm-6">
<label for="client-address">Address</label>
<input type="text" class="form-control input-lg" id="client-address" name="clientAddress" value="">
</div>
<div class="form-group col-sm-6">
<label for="client-company">Company</label>
<input type="text" class="form-control input-lg" id="client-company" name="clientCompany" value="">
</div>
<div class="form-group col-sm-6">
<label for="client-notes">Notes</label>
<textarea type="text" class="form-control input-lg" id="client-notes" name="clientNotes"></textarea>
</div>
<div class="col-sm-12">
<a href="clients.php" type="button" class="btn btn-lg btn-default">Cancel</a>
<button type="submit" class="btn btn-lg btn-success pull-right" name="add">Add Client</button>
</div>
</form>
<?php
include('includes/footer.php');
?>
答案 0 :(得分:0)
检查您的“客户端”表是否允许ID值为NULL。我认为这是问题所在,因为如果你正确地设置你的表,那将是你的主键,它不应该是NULL。