2016-01-07 43 views
1

我的总体目标是管理网页上的链接组。我有四个表单页来管理它们;使用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); 

?> 
+0

你在函数renderForm(...)中缺少''''!这应该给你一个语法错误消息 – Jeff

+1

,你在哪里调用函数'renderForm'呢? – Jeff

+0

也可以尝试删除'
'元素或使用像这样的引号正确地转义它; 'echo'
';' – Benjy1996

回答

0

是一点点slipup

的语法为<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);使用,无论其在此方面不使用正确的事情。

也作为用户名和密码区分大小写,检查用户名和密码实际上是否有大写字母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(); 
} 
+0

谢谢,我试过了,仍然得到了我之前得到的服务器错误。 –

+0

使用此代码,您是否在页面上获得了空白下拉列表或没有任何下拉列表 – RiggsFolly

+0

我已更改了代码以便更轻松地处理任何可能的sql错误。我还在下拉列表中添加了一个默认(始终存在)条目,这样即使查询不产生结果,您也可以在下拉列表中看到某些内容 – RiggsFolly