简体   繁体   中英

Populate an html drop down using php mysqli

my overall aim is to manage groups of links on a web page. I have four form pages to manage them;

  • one to enter the name of a new link group (section)
  • one to list the sections
  • one to enter the name of a new link, it's URL, and the section under which it should appear
  • one to list the links, URLs, and section names

For this I have two tables "links" and "sections" I want to retrieve the section names from the section table, display them in a drop-down on the new links page, and write them into the links table with the link name and URL.

My problem at the moment is retrieving the section list as a drop-down. I've tried a few things but am very inexperienced so either they are nonsense or I've made small mistakes that trip it up, but I can't tell which.

Here's what I have (that doesn't work);

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

Many thanks

GD

connect-db.php follows

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

?>

Yes just a little slipup

The syntax for a <option> tag is

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

And it is the value="1" part that is sent back to the script when you press the input button.

So you could try this code to replace yours

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

Ah the connect-db.php might be your issue, I have never seen mysqli_report(MYSQLI_REPORT_ERROR); used and regardless its not the right thing to use in this context.

Also as userid and password are case sensitive , check that the userid and password actually have an upper case A?

Try this instead :

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

ADDITIONAL ERROR:

I just spotted another error here in this lines

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

The bind_param method call is incorrect, change it to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM