使用php-mysqli填充一个html下拉列表



我的总体目标是管理网页上的链接组。我有四个表单页面来管理它们;

  • 一个用于输入新链接组(节)的名称
  • 一个列出部分
  • 一个用于输入新链接的名称、它的URL以及它应该出现的部分
  • 一个用于列出链接、URL和节名称

为此,我有两个表"链接"one_answers"部分",我想从部分表中检索部分名称,在新链接页面的下拉列表中显示它们,并将它们与链接名称和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-

connect-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);
?>

是的,只是有点失误

<option>标签的语法为

<option value="1">What the user gets to see</option>
<option value="2">Something else the user gets to see</option>

当您按下输入按钮时,会将value="1"部分发送回脚本。

所以你可以试试这个代码来代替你的

$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>';

啊,connect-db.php可能是你的问题,我从来没有见过mysqli_report(MYSQLI_REPORT_ERROR);被使用过,不管它在这种情况下使用是不对的。

同样,由于用户ID和密码区分大小写,请检查用户ID和口令是否真的有大写字母A?

试试这个:

<?php 
// server info 
$servername = "localhost"; 
$username = "Admin"; 
$password = "Admin"; 
$dbname = "dashboard2"; 
// connect to the database 
$mysqli = new mysqli($servername, $username, $password, $dbname);

if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}
?>

附加错误:

我刚刚在中发现了另一个错误

// 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();
}

bind_param方法调用不正确,请将其更改为

// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT links (Link, URL) VALUES (?, ?)"))
{
    $stmt->bind_param("ss", $Link, $URL);  //<- removed last param
    $stmt->execute();
    $stmt->close();
}

最新更新