我遇到此代码无法将值插入数据库的问题......
<html>
<head>
<title>Add Data</title>
</head>
<body>
<?php
//including the database connection file
include("config.php");
if(isset($_POST['Submit'])) {
$sitenumber = $_POST['sitenumber'];
$videolink = $_POST['videolink'];
$daynight = $_POST['daynight'];
$maxtents = $_POST['maxtents'];
$maxpersons = $_POST['maxpersons'];
$geography = $_POST['geography'];
$view = $_POST['view'];
$forestcover = $_POST['forestcover'];
$waterfront = $_POST['waterfront'];
$firepit = $_POST['firepit'];
$description = $_POST['description'];
$reslink = $_POST['reslink'];
// checking empty fields
if(empty($sitenumber) || empty($videolink) || empty($daynight) || empty($maxtents) || empty($maxpersons) || empty($geography) ||
empty($view) || empty($forestcover) || empty($waterfront) || empty($firepit) || empty($description) || empty($reslink)) {
if(empty($sitenumber)) {
echo "<font color='red'>Site Number field is empty.</font><br/>";
}
if(empty($videolink)) {
echo "<font color='red'>YouTube Link field is empty.</font><br/>";
}
if(empty($daynight)) {
echo "<font color='red'>Day or Overnight field is empty.</font><br/>";
}
if(empty($maxtents)) {
echo "<font color='red'>Maximum Tents field is empty.</font><br/>";
}
if(empty($maxpersons)) {
echo "<font color='red'>Maximum Persons field is empty.</font><br/>";
}
if(empty($geography)) {
echo "<font color='red'>Geography field is empty.</font><br/>";
}
if(empty($view)) {
echo "<font color='red'>View field is empty.</font><br/>";
}
if(empty($forestcover)) {
echo "<font color='red'>Forest Cover field is empty.</font><br/>";
}
if(empty($waterfront)) {
echo "<font color='red'>Waterfront Access field is empty.</font><br/>";
}
if(empty($firepit)) {
echo "<font color='red'>Firepit field is empty.</font><br/>";
}
if(empty($description)) {
echo "<font color='red'>Description field is empty.</font><br/>";
}
if(empty($reslink)) {
echo "<font color='red'>Reservation Link field is empty.</font><br/>";
}
//link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
// if all the fields are filled (not empty)
//insert data to database
$result = mysqli_query($mysqli, "INSERT INTO sites (sitenumber,videolink,daynight,maxtents,maxpersons,geography,view,forestcover,waterfront,firepit,description)
VALUES ('{$sitenumber}','{$videolink}','{$daynight}','{$maxtents}','{$maxpersons}','{$geography}','{$view}','{$forestcover}','{$waterfront}','{$firepit}','{$description}')");
//display success message
echo "<font color='green'>Data added successfully.";
echo "<br/><a href='index.php'>View Result</a>";
}
}
?>
</body>
</html>
我无法弄清楚......我确保这些值是从HTML表单页面传递的。我已经能够让删除页面正常工作了。 &#39; mysqli_query(插入&#39;只是不会将值插入数据库。
答案 0 :(得分:0)
如果 ELSE 正在运行, MYSQL连接正在运行,而不是查询。
mysqli_query($mysqli, "INSERT INTO sites(
`sitenumber`,
`videolink`,
`daynight`,
`maxtents`,
`maxpersons`,
`geography`,
`VIEW`,
`forestcover`,
`waterfront`,
`firepit`,
`description`
)
VALUES(
'$sitenumber',
'$videolink',
'$daynight',
'$maxtents',
'$maxpersons',
'$geography',
'$view',
'$forestcover',
'$waterfront',
'$firepit',
'$description'
)";
答案 1 :(得分:0)
我想回答一下,由于echo mysqli_error($ mysqli)而且我很清楚。我刚刚没有插入插入的reslink,因为我没有为它分配一个默认值,它就停在那里。只要我将它添加到查询中,就可以了。