我已经构建了一个名为jobs的数据库,我试图通过一个调用php文件的html表单将数据插入其中。提交表单后,我在控制台中看到以下错误。
错误:无法执行INSERT INTO作业(id,title,pay,description,location,max_people,people_going,tasks,start_time,end_time,start,end) VALUES(默认,'testTitle','4.00','testd','testl','4','1','testt','13:00:00','14:00:00','2016 -05-31 13:00:00','2016-05-31 14:00:00')。
我可以通过phpMyadmin手动输入数据,这只有在我尝试通过表单更新数据库时才会发生。我对使用数据库相对较新,所以我确信它非常简单。我将不胜感激任何帮助。
数据库的布局如下,没有什么可以是Null:
insert.php
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$host= "localhost";
$user= "";
$pass= "";
$link = mysql_connect($host, $user, $pass);
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$job_title = $_POST['j_title'];
$job_pay = $_POST['j_pay'];
$job_start_time = $_POST['j_start'];
$job_end_time = $_POST['j_end'];
$original_job_date = $_POST['j_date'];
$job_summary = $_POST['j_description'];
$job_location = $_POST['j_location'];
$job_people = $_POST['j_people'];
$job_tasks = $_POST['j_tasks'];
$j_going=1;
$job_date=date('Y-m-d',strtotime("$original_job_date"));
$event_start= date('Y-m-d H:i:s', strtotime("$job_date $job_start_time"));
$event_end= date('Y-m-d H:i:s', strtotime("$job_date $job_end_time"));
// attempt insert query execution
$sql = "INSERT INTO jobs (id, title, pay, description, location, max_people, people_going, tasks, start_time, end_time, start, end) VALUES (DEFAULT, '$job_title', '$job_pay', '$job_summary', '$job_location', '$job_people', '$j_going', '$job_tasks', '$job_start_time', '$job_end_time', '$event_start', '$event_end')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
来自job_creation.html的表单代码
<div class="container container-wide z-index">
<h2>Job Creation</h2>
<form class='rd-mailform row' id="job_form" method="post" action="insert.php">
<!-- RD Mailform Type -->
<input type="hidden" name="form-type" value="contact"/>
<!-- END RD Mailform Type -->
<div class="col-xs-12 col-sm-6">
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_title">Job Title</label>
<input id="j_title"
type="text"
name="j_title"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_pay">Job Pay</label>
<input id="j_pay"
type="number"
min="0"
step="0.01"
data-number-to-fixed="2"
data-number-stepfactor="100"
name="j_pay"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_start">Job Start Time</label>
<input id="j_start"
class="time"
type="text"
name="j_start"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_end">Job End Time</label>
<input id="j_end"
class="time"
type="text"
name="j_end"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_date">Job Date</label>
<input id="j_date"
class="datepicker"
type="text"
name="j_date"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_location">Job Location</label>
<input id="j_location"
type="text"
name="j_location"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_people">Number of People</label>
<input id="j_people"
type="number"
name="j_people"
/>
</div>
</div>
<div class="col-xs-12 col-sm-6">
<div class="form-group textarea">
<label class="form-label" data-add-placeholder for="j_description">Job Description</label>
<textarea id="j_description"
name="j_description"
></textarea>
</div>
<div class="form-group textarea">
<label class="form-label" data-add-placeholder for="j_tasks">What Needs to be Done</label>
<textarea id="j_tasks"
name="j_tasks"
></textarea>
</div>
</div>
<div class="form-group btn-wr text-center">
<input type="submit" class="btn btn-sm btn-success" value="Create Job" >
<div class="mfInfo"></div>
</div>
</form>
</div>
解决方案:代码实际上存在多个问题。删除整数和小数后的引号,以及切换我的所有语句以使用mysqli;我收到错误,它无法连接到数据库。通过在代码中添加mysqli_connect以及一些变量来解决这个问题。
答案 0 :(得分:-4)
试试这个
$sql = "INSERT INTO jobs (`title`, `pay`, `description`, `location`, `max_people`, `people_going`, `tasks, `start_time, `end_time`, `start`, `end`) VALUES ('$job_title', '$job_pay', '$job_summary', '$job_location', '$job_people', '$j_going', '$job_tasks', '$job_start_time', '$job_end_time', '$event_start', '$event_end')";
开始和结束是来自MySQL的保留字