我的总体目标是管理网页上的链接组。我有四个表单页来管理它们;使用php mysqli填充html下拉列表
- 一个进入一个新的链接组的名称(节)
- 一个列出部分
- 一个进入一个新的链接的名称,它的URL,并在其下部分它应该出现
- 一个列出的链接,URL和部分名
为此,我有两个表“链接”和“部分”我想检索来自段表的段名称,在显示它们在新的链接页面下拉菜单,然后写入他们与链接名称和URL链接到链接表中。
我现在的问题是检索部分列表作为下拉列表。我尝试了一些东西,但是很缺乏经验,所以他们都是无稽之谈,或者我犯了一些小错误,但我不知道是哪一个。
这是我有(不起作用);
<?php
include("connect-db.php");
function renderForm($Link = '', $URL = '', $Sectionname = '', $error = '', $ID = '') {
;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>New Link</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<h1>New Link</h1>
<?php if ($error != '') {
echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error
. "</div>";
} ?>
<form action="" method="post">
<div>
<?php if ($ID != '') { ?>
<input type="hidden" name="ID" value="<?php echo $ID; ?>" />
<p>ID: <?php echo $ID; ?></p>
<?php } ?>
<strong>Link name: *</strong> <input type="text" name="Link"
value="<?php echo $Link; ?>"/><br/>
<strong>URL: *</strong> <input type="text" name="URL"
value="<?php echo $URL; ?>"/>
<?php
}
$query = 'SELECT Section FROM sections';
$result = mysqli_query($mysqli, $query);
if(! $result) {
echo mysql_error();
exit;
}
echo '<select name="dropdown">';
echo '<option value="0">Select a section please</option>';
while ($row=mysqli_fetch_array($result)) {
echo '<option value="' . $row['Section'] . '">' . $row['Section'] . '</option>';
}
echo '</select>';
}
?>
<p>* required</p>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body>
</html>
<?php }
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// get the form data
$Link = htmlentities($_POST['Link'], ENT_QUOTES);
$URL = htmlentities($_POST['URL'], ENT_QUOTES);
// check that Link and URLame are both not empty
if ($Link == '' || $URL == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($Link, $URL, $Sectionname, $error);
}
else
{
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT links (Link, URL) VALUES (?, ?)"))
{
$stmt->bind_param("ss", $Link, $URL, $Sectionname);
$stmt->execute();
$stmt->close();
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}
// redirect the user
header("Location: linkview.php");
}
}
// if the form hasn't been submitted yet, show the form
else
{
renderForm();
}
}
// close the mysqli connection
$mysqli->close();
?>
非常感谢
GD
连接-db.php中如下
<?php
// server info
$servername = "localhost";
$username = "Admin";
$password = "Admin";
$dbname = "dashboard2";
// connect to the database
$mysqli = new mysqli($servername, $username, $password, $dbname);
// show errors (remove this line if on a live site)
mysqli_report(MYSQLI_REPORT_ERROR);
?>
你在函数renderForm(...)中缺少''''!这应该给你一个语法错误消息 – Jeff
,你在哪里调用函数'renderForm'呢? – Jeff
也可以尝试删除'
'元素或使用像这样的引号正确地转义它; 'echo'
';' – Benjy1996