Flag icon besides the text in table from database - php

I did a form which adds record to database and it shows in HTML table.
One of the column in form and database is "country". I would like to insert a right flag icon beside the "country" column and I'd like to make it from form. I added example photo
Could you help me with any idea?
Form
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style2.css">
</head>
<body>
<?php
if (isset($_GET["date"]) ){
$date = $_GET["date"];
$country = $_GET["country"];
$city = $_GET["city"];
$place = $_GET["place"];
if( empty ( $date ) || empty ($country) || empty ($city) || empty ($place)){
echo "Wypelnij wszystkie pola";
} else {
$conn = new mysqli("localhost", "root", "", "tour");
$odp = $conn->query("INSERT INTO trasy (date, country, city, place)
VALUES ('$date', '$country', '$city', '$place')");
header("Location: index5.php");
if ($odp) {
echo "Dodano koncert";
}else{
echo "Nie udalo sie dodac";
}
$conn->close();
}
}
?>
<div id="login">
<form method="GET" action="index5.php">
<table id="customers">
<input name="date" type="date" placeholder="Date..."><br>
<input name="country" type="text" placeholder="Country..."><br>
<input name="city" type="text" placeholder="City..."><br>
<input name="place" type="text" placeholder="Place..."><br>
<input type="submit" value="OK">
</table>
</form>
</div>
</body>
</html>
HTML table
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style3.css">
</head>
<body>
<table id="customers">
<tr>
<th>Date</th>
<th>Country</th>
<th>City</th>
<th>Place</th>
<th>Info</th>
</tr>
<?php
$conn = new mysqli("localhost", "root", "", "tour")
or die ("Błąd");
$wynik = $conn->query("SELECT * FROM trasy ORDER BY date DESC");
if ($wynik->num_rows > 0){
while ( $wiersz = $wynik->fetch_assoc() ){
echo "<tr>";
echo "<td>" . $wiersz["date"] . "</td>";
echo "<td>" . $wiersz["country"] . "</td>";
echo "<td>" . $wiersz["city"] . "</td>";
echo "<td>" . $wiersz["place"] . "</td>";
echo "<td>" . $wiersz["info"] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "Nic tu nie ma";
}
$conn->close();
?>
</table>
</body>
</html>

I will address only your question but keep in mind you have all sort of wrong things with your code starting with security.
I will just point you in direction what I think is best for you to go.
First of all upload all flags in one directory on server. You can use img or you can use CSS svg like this one for example.
Second, don't make people type there country in input field at all (that way you make sure values for same country will be the same), make that a drop-down with pre-populated all countries and assign a value to each.
Example of value for CSS icons that I provided link for:
If user chooses Germany from drop-down you store in country field value: gr.
That way when you call your country you can do that like this:
echo '<td><span class="flag-icon flag-icon-' . $wiersz["country"] . '"></span></td>';
Which will produce for Germany:
<td><span class="flag-icon flag-icon-gr"></span></td>
You can do same thing just with HTML img tags. Just storing the flag value as img src.
I believe this is best way to go. Other that that you would need to have a whole lot of PHP switch statements.
EDIT:
Additionally if you want to have country name and flag store both of them as value in select and on database input separate it and store it in two columns like this:
<select id="country">
<option value="Croatia | hr">Croatia</option>
<option value="Germany | gr">Germany</option>
<select>
$country = mysqli_real_escape_string($_GET['country']);
$pieces = explode(" | ", $country);
$countryname = $pieces[0]; // piece1
$flag = $pieces[1]; // piece2

Related

PHP didnt select a correct value from while loop

I have a problem regarding while loop. I have a option tag and hidden value inside the loop, the option tag work correctly for the dropdown list but the hidden value is not match with the selected dropdown list.
This is my code :
<?php
session_start();
require 'config.php';
$option1 = '';
$idseason='';
$season = '';
$query1 = "select id,description from codemaster_local where codeclass = 'season' and description like '%1st%' group by description";
$result1 = mysqli_query($doa,$query1);
while($row1 = mysqli_fetch_array($result1)) {
$option1 .= "<option value='".$row1['description']."'>".$row1['description']."</option>";
$hidden = "<input type=hidden name=id value='".$row1['id']."'>";
}
if(isset($_POST['submit'])) {
$season = $_POST['season'];
$idseason = $_POST['id'];
echo "Season :"; echo $season;echo "<br>";
echo "ID : ";echo $idseason;
}
?>
<html>
<head>
<link href="style/style.css" rel="stylesheet" type="text/css">
<link href="style/sty.css" rel="stylesheet" type="text/css">
<link href="style/dropdown.css" rel="stylesheet" type="text/css">
</head>
<body>
<form class="form-style-9" action="" method="post">
<table>
<tr><td><b>Season:</b></td>
<td> <select name="season" class="select-css">
<?php echo $option1; ?>
</select>
<?php echo $hidden; ?>
</td>
</tr>
</table><br>
<input type="submit" value="Search" name="submit">
</form>
</body>
</html>
For example, the id for 1st season 2005 should be 24800 but instead it choose the last id 30539. I select any of the season also still choose the last id. Is there any way to fix this?
$hidden is being overwritten upon every loop within your while. Resulting in one hidden field with the name: id, containing the value of the last row id retrieved. That is the value you receive upon submission.
while($row1 = mysqli_fetch_array($result1)) {
$option1 .= "<option value='".$row1['description']."'>".$row1['description']."</option>";
$hidden = "<input type=hidden name=id value='".$row1['id']."'>";
}
I suggest you change the option's value attribute to $row1['id']:
while($row1 = mysqli_fetch_array($result1)) {
$option1 .= "<option value='".$row1['id']."'>".$row1['description']."</option>";
}
This way, $_POST['season'] will then hold the corresponding id for the season selected.
Try putting the id in the option tag instead of a hidden tag:
while($row1 = mysqli_fetch_array($result1)) {
$option1 .= "<option value='".$row1['description']."' id='".$row1['id']."'>".$row1['description']."</option>";
}

How to make data display in the table on my webpage once it's been input into my database?

I'm not sure how to describe it, so here's a video where I explain my problem.
I tried rearranging some of the code, as I do believe nothing is faulty, attempting to make sure that the table refreshes with the new data inside it, however every time I tried to place my code in a different order (executing the queries in different orders), it either functions differently than how I want it to function or it doesn't function at all.
Both queries do function separately, I'm just unsure why they're not working together.
Searchbar has the value seen inputted in the homepage on both my Search page and this page in question. However it was left blank for this page, which gave me the result of having the full table display which is what I wanted to happen. I'm just not sure how I can edit my code so, when submitted, it will display the newly added data.
My PHP:
<?php
$find = $_POST['searchbar'];
$host = "localhost";
$username = "FFF";
$pword = "L3FhqJNey8Op2qJY";
$database = "Project";
include 'includes/db.inc.php';
$Name2 = $_POST['Name'];
$YearOfRelease2 = $_POST['YearOfRelease'];
$Studio2 = $_POST['Studio'];
$Age2 = $_POST['Age'];
$Score2 = $_POST['Score'];
?>
My HTML:
<html>
<head>
<title>Add a Film - Films! Films! FILMS!</title>
</head>
<body>
<h1>Films! Films! FILMS!</h1>
<h2>Add a Film</h2>
<p>If you wish to add a film to our database, feel free to add data relating to the film in the respective boxes below. You should then refresh the page.</p>
<p>Add Film:</p>
<form method="POST" action="AddFilm.php">
<p>Name of Film: <input type="text" name="Name"></p>
<p>Year of Release: <input type="text" name="YearOfRelease"></p>
<p>Name of Studio: <input type="text" name="Studio"></p>
<p>Age Rating: <select name="Age" size="1">
<optgroup label="Select Age Rating">
<option value="U">U</option>
<option value="PG">PG</option>
<option value="12">12</option>
<option value="15">15</option>
<option value="18">18</option>
</optgroup>
</select></p>
<p>Review Score: <input type="text" name="Score"></p>
<p><input type="submit" name="submit" value="Submit and Refresh"></p>
</form>
<?php
echo "<h2>$output</h2>";
$query_string = "SELECT * FROM movies WHERE Name LIKE '%$find%' OR YearOfRelease LIKE '%$find%' OR Studio LIKE '%$find%' OR Age LIKE '%$find%' OR Score LIKE '%$find%'";
$query_string2 = "INSERT INTO movies (Name, YearOfRelease, Studio, Age, Score) VALUES ('$Name2', '$YearOfRelease2', '$Studio2', '$Age2', '$Score2');";
if ($result = $mysqli->query($query_string2)) {
$output2 = $Name2 ." has been added to the database.";
echo "<p>$output2</p>";
} else {
echo ("Error performing query: " . $mysqli->error() );
}
$result->close();
if ($result = $mysqli->query($query_string)) {
echo "<table border='1'>";
echo "<tr><th>FilmID</th><th>Name</th><th>YearOfRelease</th><th>Studio</th><th>Age</th><th>Score</th></tr>";
while ($row = $result->fetch_object())
{
$FilmID = $row->FilmID;
$Name = $row->Name;
$YearOfRelease = $row->YearOfRelease;
$Studio = $row->Studio;
$Age = $row->Age;
$Score = $row->Score;
$output ="<tr><td> $FilmID";
$output = $output . "<td> $Name";
$output = $output . "<td> $YearOfRelease";
$output = $output . "<td> $Studio";
$output = $output . "<td> $Age";
$output = $output . "<td> $Score </tr>";
echo "<p>$output</p>";
}
echo "</table>";
echo "<hr>";
echo '<p>Back to Home Page</p>';
$result->close();
} else {
echo ("Error performing query: " . $mysqli->error() );
}
$mysqli->close();
?>
</body>
</html>

Creating a table on HTML with information from various MySQL tables

i have a website i am constructing for a school project, in which i already have various webpages where i get tabular data from my database through the use of While and Foreach.
But on this page in question, i am attempting to retrieve data from various tables, in order for a user to choose a category, and a text and submit.
But for some reason it isn't outputting any data. If i attempt with only one table and without using the table.field method, and only typing the fields, it works.
But from what i know, to retrieve from various tables i have to do so.
Can annione help me out on this?
<html>
<head>
<script type="text/javascript" >
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
mysql_connect("localhost","root","") or die("problema na conexao");
mysql_select_db("trabalho1");
$query = "SELECT texto.titulo,texto.ID,categoria.categoria,categoria.id FROM categoria,texto";
$results = mysql_query($query) or die(mysql_error());
echo"<center>";
echo "<table border='2'>\n";
echo "<form id='formulario' name='post' method='post' onsubmit='return validar(this) action='../inserir/inseretexto.php>'";
echo "<button type='submit'>Submeter</button>";
echo "<tr><td colspan ='2'>Historico de Newsletters</td><td colspan='2'>Enviar Newsletter</td></tr>";
echo "<tr><td colspan='2'>Texto </td><td colspan='2'>Categoria</td></tr>";
while ($row = mysql_fetch_assoc($results)) {
foreach ($row as $campo=>$valor) {
if ($campo == "texto.titulo") {
echo "<tr><td>'".$valor."'</td>";
}
if ($campo == "texto.ID") {
echo "<td><input type='radio' name='nome' value='".$valor."'></td></tr>";
}
if ($campo == "categoria.categoria") {
echo "<td>'".$valor."'</td>";
}
if ($campo=="categoria.id") {
echo "<td><input type='radio' name='nome' value='".$valor."'></td></tr>";
}
}
}
echo "</form>";
echo "</table>";
echo "</center>";
?>
</body>
</html>
Added: Since both tables have a field called id, it won't let me simply put the field names, i have to also put the table name like i did. And yes, i have verified and both tables are populated with data, they work fine on other pages.
This is what I suggest (I didn't consider much code optimization).
<?php
$data = array();
mysql_connect('localhost', 'root', '') or die('problema na conexao');
mysql_select_db('trabalho1');
// Use the same field-names/aliases: id, info
$query = 'SELECT ID AS id, titulo AS info FROM texto';
$results = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($results)) {
$data[] = $row;
}
// Use the same field-names/aliases: id, info
$query = 'SELECT id, categoria AS info FROM categoria';
$results = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($results)) {
$data[] = $row;
}
?>
<html>
<head>
<script type="text/javascript">
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<center>
<table border='2'>
<form id='formulario' name='post' method='post' onsubmit='return validar(this) action='../inserir/inseretexto.php>
<tr><td colspan ='2'><button type='submit'>Submeter</button></td></tr>
<tr><td colspan ='2'>Historico de Newsletters</td><td colspan='2'>Enviar Newsletter</td></tr>
<tr><td colspan='2'>Texto </td><td colspan='2'>Categoria</td></tr>
<?php
foreach ($data as $row) {
echo '<td><input type="radio" name="nome" value="' . $row['id'] . '></td></tr>' .
'<tr><td>' . $row['id'] . '</td>';
}
?>
</form>
</table>
</center>
</body>
</html>
PS: Read about my response about double quotations. Should I use curly brackets or concatenate variables within strings?

From index.php to detail.php

i have a troubles with my code.
I have html table in index.php (php, mysql)..this:
<?php
require ('../../inc/config.inc.php');
require ('../../inc/ini.php');
mysql_set_charset('utf8');
$sql = "SELECT * FROM {$cfg['tbl_dily']}";
$result = mysql_query($sql)or die(mysql_error());
echo "<table class=\"vypis\">";
echo "<h1 id=\"vypis\">Nabídka náhradních dílů</h1>\n";
$i = 0; //defaultní hodnota pro obarvení řádku
//start cyklu pro výpis z tbl_dily
while ($row = mysql_fetch_array($result)){
//přístup ke sloupcum tbl_dily
$part_id =$row['part_id'];
$img150 =$row['img150'];
$nazevdilu =$row['nazev'];
$vyrobce =$row['vyrobce'];
$model =$row['model'];
$cena =$row['cena'];
//start --- coloring every 2nd row of table
$i=1-$i;
$trclass="radek".$i;
//end --- coloring every 2nd row of table
echo "<tr class=\"".$trclass."\">\n";
if($img150 == null){ // podmínka pro existenci fotografie produktu
echo "<td class=\"img150\"> <img class=\"obrazek\" src=\"fotoneni.gif\"/> </td>\n";
}
else {
echo "<td class=\"img150\"> <img class=\"obrazek\" src=\"".$img150."\"/> </td>\n";
}
echo "<td class=\"nazevdilu\">".$nazevdilu."</td>\n";
echo "<td class=\"modely\">".$vyrobce." ".$model."</td>\n";
if($cena == 0){ //podmínka pro existenci přesné ceny produktu nebo "dohodou"
echo "<td class=\"cena\">dohodou</td>\n";
}
else{
echo "<td class=\"cena\">".$cena." Kč"."</td>\n";
}
echo "</tr>\n";
}
//konec cyklu pro výpis z tbl_dily
echo "</table>\n";
?>
So I have linked out part_id with no problems. Problems shows when I want to see detail of some product. My detail.php looks like this now:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
<head>
<?php
require ('../../inc/config.inc.php');
require ('../../inc/ini.php');
mysql_set_charset('utf8');
$part_id=$_GET['part_id'];
$data = mysql_query("SELECT * FROM {$cfg['tbl_dily']} WHERE part_id='$part_id'") or die(mysql_error());
while ($detail = mysql_fetch_array($data)){
$id =$detail['part_id'];
}
?>
<title><?php $id; ?></title>
</head>
<body>
<div class="detail">
<span id="detail_id">Výpis nabídky id <?php $id; ?></span>
<div class="detail_foto">
</div>
<div class="detail_info">
</div>
</div>
</body>
</html>
I need to help at least with getting part_id number in page title of detail.php. I dont understand so much how $_GET works..I hope you somebody show me how-to..
THANKS for helping me out:))
Your explanation is not terribly nice. You should maybe edit the Question and explain where the problem is at. But for now, let me just explain '$_GET'
'$_GET' is a way to deliver Data from one PHP Script to another. You can find them in almost every Formular. As you can see in the Code below, there is a simple way that sends Data to an "action.php" - the attribute "method" is for telling the formular what you want to do. You should maybe take a look at both options because GET displays the data you want to deliver in the link. Your user could start manipulating that which would be a very unsafe thing in your case because you work with mysql-Databases. Also you should take a look at Mysql String escaping.
Back to the topic: The HTML below would redirect $_GET['name'] AND $_GET['age'] to the action.php where you can work with those.
<form action="action.php" method="get">
<p>Your ame: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>

Cannot fix the cyrillic issue. htmlspecialchars/PHP/MySQL

guys. When I type anything on cyrillic like: Цветана Пиронкова, and click submit (into the table), it is showing it (and saving it in the mysql table), like that: &#1062;&#1074;&#1077;&#1090;&#1072;&#1085;&#1072; &#1055;&#1080;&#1088;&#1086;&#1085;&#1082;&#1086;&#1074;&#1072;
And I don't have any ideas how to fix it. I think that the problem comes from htmlspecialchars, but I don't know. Here is my index file:
<?php // connect to the database
include('connect-db.php');
// get results from database
$result = mysql_query("SELECT * FROM players")
or die(mysql_error());
mysql_query("SET NAMES UTF8");
// display data in table
echo "<p><b>View All</b></p>";
echo "<table class=\"table table-bordered table-hover\" border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Mqsto</th> <th>Ime</th> <th>Tochki</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['mqsto'] . '</td>';
echo '<td>' . $row['ime'] . '</td>';
echo '<td>' . $row['tochki'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p>Add a new record</p><br><br>
Here is my new.php file:
<?php
/*
NEW.PHP
Allows user to create a new entry in the database
*/
// creates the new record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($mqsto, $ime, $tochki, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>New Record</title>
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<div>
<strong>Mqsto: *</strong> <input type="text" name="mqsto" value="<?php echo $mqsto; ?>" /><br/>
<strong>Ime: *</strong> <input type="text" name="ime" value="<?php echo $ime; ?>" /><br/>
<strong>Tochki: *</strong> <input type="text" name="tochki" value="<?php echo $tochki; ?>" /><br/>
<p>* required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect-db.php');
mysql_query("SET NAMES UTF8");
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$mqsto = mysql_real_escape_string(htmlspecialchars($_POST['mqsto']));
$ime = mysql_real_escape_string(htmlspecialchars($_POST['ime']));
$tochki = mysql_real_escape_string(htmlspecialchars($_POST['tochki']));
// check to make sure both fields are entered
if ($mqsto == '' || $ime == '' || $tochki == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($mqsto, $ime, $tochki, $error);
}
else
{
// save the data to the database
mysql_query("INSERT players SET mqsto='$mqsto', ime='$ime', tochki='$tochki'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: ranglista.php");
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','');
}
?>
Have you setted the database charset to UTF-8 and used this inside your html?
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
Remember also that UTF-8 cyrillic inside database takes 2 byte, so pay attention when you set varchar or similar size (if you want to display 3000 character you should set a size of 6000)
Add to the top of your new.php file
echo "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />";

Categories