Andreea Prodan Andreea Prodan - 1 year ago 176
PHP Question

create select with options from mysql pdo

I want to make an select categoryName from my Database, and show the result in html box with options for each User ... I don't know why the select is not displaying anything... I know that it's a "block" there, but I don't know where (because it's not showing Date box anymore) . I already tried others solutions from stackoverflow but it's still not working.
I would appreciate some help


<!DOCTYPE html>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="stylesheet" type="text/css" href="../css/page-style.css">
<link rel="stylesheet" type="text/css" href="../css/account-style.css">
<link rel="stylesheet" type="text/css" href="../css/login-style.css">
<link rel="stylesheet" type="text/css" href="../css/expense-operation-style.css">
<link rel="stylesheet" type="text/css" href="../css/income-operation-style.css">

<link href=',300,300italic,400italic,600,600italic,700,700italic,800,800italic' rel='stylesheet' type='text/css'>
<link href=',100,300,100italic,300italic,400italic,500,500italic,700,700italic,900,900italic&subset=latin,latin-ext' rel='stylesheet' type='text/css'>

<link rel="shortcut icon" href="../img/wallet.png"/>

<div id="wrapper">
<div class="center" id="HEADER">
<div class="motto">Cu noi știi unde ți-au zburat banii!</div>
<div class="main-menu">
<li><a href="Facilities.html"><img src="../img/facilities.png" width="40" alt="logout icon" title="Facilități"></a></li>
<li><a href="myAccount.html"><img src="../img/report.png" width="40" alt="logout icon" title="Rapoartele tale"></a></li>
<li><a href="groupPage.html"><img src="../img/group.png" width="40" alt="logout icon" title="Grupurile tale"></a></li>
<li><a href="Settings.html"><img src="../img/settings.png" width="40" alt="settings icon" title="Setările tale"></a></li>
<li><a href="#"><img src="../img/iconLogout.png" width="40" alt="logout icon" title="Deconectează-te!"></a></li>
<div id="CONTENT" class="center">
<div class="header-shadow">
<img src="../img/shadow.png" alt="decorative - header shadow">

<div class="operation-content">
<img src="../img/addtowallet.png" alt="wallet">
<form action="../php/operations.php" method = "post">
<label>Introduceți suma:</label>
<input type="number" min="1" name = "incomeSum" required><br>
<label>Alegeți o categorie:</label>
<select name="incomeCat">
try {

$session = $_SESSION['user'];

$sql = $conn->prepare("SELECT categoryName FROM Categories WHERE ID_User = :ID_User");
$result = $sql->fetchAll();

if ( count($result) ) {

print '<select id="incomeCat">';
foreach ($result as $row) {
echo '<option value="'.$row['incomeCatList'].'">'.$row['incomeCatList'].'</option>';
print '</select>';
print '<input type="submit" value="Submit">';

} else {
echo "No rows returned.";
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();


<br><label>Selectați data:</label>
<input type="date" max="1990-12-31" name ="incomeDate" required><br>
<input type="submit" name="addSubmit" value="adaugă">


<div class="center" id="FOOTER">
<div class="social-media">
<p>Iti place proiectul nostru?</p>
<p><span>Impartaseste-l cu prietenii!</span></p>
<a href="" target="_blank"><img src="../img/fbMedia.png" width="50" alt="Facebook Page"></a>
<a href="" target="_blank"><img src="../img/twitterMedia.png" width="50" alt="Twitter Page"></a>

<div class="contact">
<p>Ai vreo problema sau o recomandare pentru noi?</p>
<p>Nu ezita sa ne contactezi!</p>

<div class="group-shortcut">
<p>Creează un grup pentru a gestiona cheltuielile împreună cu prietenii!</p>
<div class="small-footer-button"><a href="createGroup.html">Creează un grup</a></div>
<p>Ai deja un grup? Intră acum și vezi ce au mai făcut membrii.</p>
<div class="small-footer-button"><a href="myGroups.html">Verifică grupurile tale</a></div>

Answer Source

I've tried to remake your project and I've figured out that you have to write the require with " " require("Connection.php");, also make sure you use your column name in foreach, like this foreach ($result as $row) { print '<option value="'.$row['categoryName'].'">'.$row['categoryName'].'</option>'; } also make sure your file extension is .php and it's in the same folder with Connection.php file

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download