Reputation: 21
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
<?php
session_start();
?>
<!DOCTYPE html>
<HTML>
<HEAD>
<title>BMA.WALLET</title>
<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='https://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600,600italic,700,700italic,800,800italic' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Roboto:400,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"/>
</HEAD>
<BODY>
<div id="wrapper">
<div class="center" id="HEADER">
<h1><span>BMA.</span>WALLET</h1>
<div class="motto">Cu noi știi unde ți-au zburat banii!</div>
<div class="main-menu">
<ul>
<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>
</ul>
</div>
</div>
<div id="CONTENT" class="center">
<div class="header-shadow">
<img src="../img/shadow.png" alt="decorative - header shadow">
</div>
<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">
<?php
require('Connection.php');
try {
$session = $_SESSION['user'];
$sql = $conn->prepare("SELECT categoryName FROM Categories WHERE ID_User = :ID_User");
$sql->execute();
$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ă">
</form>
</div>
</div>
<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="http://www.facebook.com" target="_blank"><img src="../img/fbMedia.png" width="50" alt="Facebook Page"></a>
<a href="http://www.twitter.com" target="_blank"><img src="../img/twitterMedia.png" width="50" alt="Twitter Page"></a>
</div>
<div class="contact">
<h1>Contact</h1>
<p>Ai vreo problema sau o recomandare pentru noi?</p>
<p>Nu ezita sa ne contactezi!</p>
<p>[email protected]</p>
</div>
<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>
</div>
</div>
</div>
</BODY>
</HTML>
Upvotes: 0
Views: 215
Reputation: 181
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
Upvotes: 0
Reputation: 23892
You are using a placeholder but aren't binding anything. Try:
$sql = $conn->prepare("SELECT categoryName FROM Categories WHERE ID_User = :ID_User");
$sql->execute(array(':ID_User' => $session));
$result = $sql->fetchAll();
Note the array(':ID_User' => $session)
in the execute, this binds the SESSION
value with your query.
For more information on prepared statements with PDO see, http://php.net/manual/en/pdo.prepared-statements.php.
Also your query should select incomeCatList
if you want that value. Currently you are selecting categoryName
; or perhaps incomeCatList
should be an alias for that column?
Upvotes: 2