我有这个php / html表单,用户可以将作业分配给驱动程序,并在提交时:它将被添加到数据库中。
这个表格一直在运行,直到我添加了驱动程序ID字段(这是基于事先选择的驱动程序)
我还绑定了网址,以通过ID代表所选的驱动程序:http://localhost/finalyear/add-job.php?driver_id=13
点击提交按钮前的代码:
if (!isset($_GET['driver_id']))
{
echo "You shouldn't have got to this page";
exit;
}
$drive = $_GET['driver_id'];
$query = "SELECT * FROM driver WHERE driver.driver_id = :driver";
$term = $conn->prepare($query);
$term->bindValue(':driver', $drive);
$term->execute();
$ drive = $ term-> fetch(PDO :: FETCH_OBJ);
HTML:
<select class="form-control" name="ID" >
<option value="<? echo $term['driver_id'];?>"><?php echo $drive->driver_id; ?></option>
</select>
单击按钮后的代码:
try{
$conn = new PDO(DB_DATA_SOURCE, DB_USERNAME, DB_PASSWORD);
}
catch (PDOException $exception)
{
echo "Oh no, there was a problem" . $exception->getMessage();
}
//get the form data
$newDriver = $_POST['ID'];
$conn=getConn();
$successJob=insertJob($conn,$newDriver);
if ($successJob) {
$error = "The job has now been added to the site!";
include("add-job.php");
}
else {
$error = "This failed";
}
function insertJob($conn,$newDriver)
{
$query="INSERT INTO jobs VALUES (NULL, :driver_id)";
$stmt=$conn->prepare($query);
$stmt->bindValue(':driver_id', $newDriver);
$affected_rows = $stmt->execute();
if($affected_rows==1){
return true;
}else{
return false;
}
}
$conn=NULL; //close the connection
}
所以现在添加driver_id,页面是空白的,不会出现任何错误,我不确定为什么会发生这种情况。做了各种测试等。
这一切都很好,直到我引入了选项控件来保存驱动程序的ID。 还要注意它也可能是由url绑定引起的,因为当你点击提交按钮时,它会把我带到一个黑色的页面上写着“我不应该到达那里”,而url会删除我所做的driver_id绑定。