好吧我有一个Add的代码,我可以把详细信息放入但是在我点击添加后出于某种原因我认为它不会保存到数据库而只是给我一个空白页面 这是我添加的PHP代码
<?php
/*
NEW.PHP
Allows user to create a new entry in the database
*/
// creates the new record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($id, $tit, $sal, $loc, $desc, $cat, $error)
{
?>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<div>
<strong>ID: *</strong> <input type="text" name="id" value="<?php echo $id; ?>" /><br/>
<strong>TITLE: *</strong> <input type="text" name="title" value="<?php echo $tit; ?>" /><br/>
<strong>SALARY: *</strong> <input type="text" name="salary" value="<?php echo $sal; ?>" /><br/>
<strong>LOCATION: *</strong> <input type="text" name="location" value="<?php echo $loc; ?>" /><br/>
<strong>DESCRIPTION: *</strong> <input type="text" name="description" value="<?php echo $desc; ?>" /><br/>
<strong>CATEGORY: *</strong> <input type="text" name="category" value="<?php echo $cat; ?>" /><br/>
<p>* required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
<?php
}
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['Add']))
{
// get form data, making sure it is valid
$id = (htmlspecialchars($_POST['id']));
$title = (htmlspecialchars($_POST['title']));
$salary = (htmlspecialchars($_POST['salary']));
$location = (htmlspecialchars($_POST['location']));
$description = (htmlspecialchars($_POST['description']));
$category = (htmlspecialchars($_POST['category']));
// check to make sure both fields are entered
if ($id == '' || $title == '' || $salary == '' || $location == '' || $description == '' || $category == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($id, $title, $salary, $location, $description, $category, $error);
}
else
{
// save the data to the database
$pdo->query("INSERT jobs SET id='$id', title='$title', salary='$salary', description='$description', category='$category'")
or die;
// once saved, redirect back to the view page
header("Location: securepage.php");
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','','','','');
}
?>
我无法弄清楚出了什么问题,因为它没有向我显示任何错误,这更加困难。但我认为我确定它与插入后不保存到数据库有关。我希望有人可以帮助我。
我的数据库名为空缺,表是工作 这是表中的值 id,title,salary,location,description,category_id
请问我是否需要更多详细信息以帮助我更好地
答案 0 :(得分:1)
在表单标记内,您需要使用“添加名称”属性添加隐藏的输入。
<input type="hidden" name="Add" value="1" />
这是因为你有一个if语句检查它。
if(isset($_POST['Add']))
答案 1 :(得分:1)
替换此行
if (isset($_POST['Add']))
用
if (isset($_POST['submit']))
旁注:
在页面顶部的error_reporting上。
第二个问题是,正如我在评论中提到的,如果你在MYSQL而不是SQL中使用INSERT查询,那么你可以遵循这个例子:
INSERT INTO table (column1, column2...) VALUES (value1, value2...)