I have an issue with the below code, it is showing the html part but not the data from my database. I am just trying to have the data from the database in the drop down list and when I click find to display it on the table below. Can you please help?
<?php
include 'config.php';
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<select name="manufacturer">
<option value="">----ALL----</option>
<?php
$sql = "SELECT id, manufacturer FROM coop";
$result = $conn->query($sql);
while ($r = mysqli_fetch_assoc($result))
{
echo "<option value='$r[id]'> $r[manufacturer]</option>";
}
?>
</select>
<input type="submit" name="find" value="find"/>
<br><br>
<table border="1">
<tr align="center">
<th>ID</th> <th>Manufacturer</th>
</tr>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$des=$_POST["manufacturer"];
if($des=="")
{
$result= mysqli_query("SELECT id, manufacturer FROM coop");
}
else
{
$result= mysqli_query($sql);
}
echo "<tr><td colspan='5'></td></tr>";
while($r= mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td align='center'>$r[0]</td>";
echo "<td width='200'>$r[1]" . " $r[2]</td>";
echo "<td alig='center' width='40'> $r[3]</td>";
echo "<td align='center' width='200'>$r[4]</td>";
echo "<td width='100' align='center'>$r[5]</td>";
echo "</tr>";
}
}
?>
</table>
</body>
</html>
your queries are wrong here
<?php
$sql = "SELECT id, manufacturer FROM coop";
$result = $conn->query($sql);
while ($r = mysqli_fetch_assoc($sql))
{
echo "<option value='$r[0]'> $r[0]</option>";
}
?>
this while ($r = mysqli_fetch_assoc($sql)) should be while ($r = mysqli_fetch_assoc($result ))
you should fetch your assoc with $result not with$sql
still you have some error bellow part
Related
I am trying to build a editable table with dynamic headers and dynamic rows based on header, but I lost my way in the process.
I got the dynamic headers:
<div class="jumbotron">
<div class="container">
<table class='table'>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Emails</th>
<?php
include("../connection/connection.php");
$get_title = "SELECT DISTINCT(title) FROM attendance";
$query1 = mysqli_query($con,$get_title);
if ($query1) {
# code...
while ($row=mysqli_fetch_array($query1)) {
# code...
$title[] = $row['title'];
}
$count = count($title);
for ($i=0; $i <$count ; $i++) {
# code...
echo "<th>".$title[$i]."</th>";
}
}
?>
</tr>
</thead>
<tbody>
<?php
$get_emails = "SELECT * FROM subjects";
$query2 = mysqli_query($con,$get_emails);
if ($query2) {
# code...
while ($row=mysqli_fetch_array($query2)) {
# code...
$emails = $row['email'];
$id = $row['id'];
$get_name = "SELECT * FROM users";
$query3 = mysqli_query($con,$get_name);
if ($query3) {
# code...
while ($row=mysqli_fetch_array($query3)) {
# code...
$fullname = $row['user_fullname'];
echo "<tr>";
echo "<td contenteditable>".$id."</td>";
echo "<td contenteditable>".$fullname."</td>";
echo "<td contenteditable>".$emails."</td>";
echo "</tr>";
}
} else {
echo "<tr>";
echo "<td contenteditable>".$id."</td>";
echo "<td contenteditable></td>";
echo "<td contenteditable>".$emails."</td>";
echo "</tr>";
}
}
} else {
echo "<tr>";
echo "<td contenteditable></td>";
echo "<td contenteditable></td>";
echo "<td contenteditable></td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
Now I need to echo the table td which would be dynamic based on the headers after the emails - td inside the tr.
Any help is appreciated.
The major problem you have is that the array element isn't going to be called title but rather DISTINCT(title) because you didn't alias it as something else, the code below shows it being aliased back as title, and as well eliminating what appears to be an unnecessary loop.
$get_title = "SELECT DISTINCT(title) title FROM attendance";
$query1 = mysqli_query($con,$get_title);
if ($query1) {
# code...
while ($row=mysqli_fetch_array($query1)) {
# code...
echo "<th>".$title[$i]$row['title']."</th>";
}
}
Is it possible to join to tables like in the following picture
There are no foreign key in svotes but there are the same records. In the select option tag I putted the school_year and the option are 2015-2016,2016-2017. If I click the 2016 it should that the other table svotes can display the year of 2016 can it be possible? and how?
Here's my code:
<select name="sy_no" onchange="getyr(this.value)">
<option></option>
<?php
include('../connect.php');
$sql2 = mysql_query("SELECT * FROM school_year");
while ($row1=mysql_fetch_array($sql2)) {
echo "<option value=".$row1['syearid'].">".$row1['from_year'].'-'.$row1['to_year']."</option>";
}
?>
</select>
get_winner.php
<?php
include('../connect.php');
$no = $_GET['no'];
//this is where I get the year
$stud = mysql_query("SELECT * FROM studentvotes,school_year WHERE school_year.from_year = studentvotes.year AND studentvotes.year = '$no' ") or die(mysql_error());
echo"<center>";
echo "<form action='' method='POST' enctype='multipart/form-data' >";
echo " <table class='table table-striped table-bordered table-hover' id='dataTables-example'>";
echo "<tr>";
echo "<th>IDno</th>";
echo "<th>Action</th>";
echo "</tr>";
while ($row=mysql_fetch_array($stud)) {
$result = mysql_query("SELECT * FROM studenvotes,school_year WHERE studenvotes.year = school_year.from_year") or die(mysql_error());
$r1 = mysql_fetch_assoc($result);
?>
<tr class="headings">
<?php
}
?>
Try this join query
<select name="sy_no" onchange="getyr(this.value)">
<option></option>
<?php
include('../connect.php');
$sql2 = mysql_query("SELECT school_year.*,svotes.* FROM school_year JOIN svotes ON , svotes.year = school_year.from_year ");
while ($row1=mysql_fetch_array($sql2)) {
echo "<option value=".$row1['syearid'].">".$row1['from_year'].'-'.$row1['to_year']."</option>";
}
?>
</select>
I need to select some data from mysql and echo them into a table,
I have 20 entries which I want to echo them into 5by4 table I can select them like this:
<?php
$sql = "SELECT player FROM `prize` WHERE inviter='$player'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table style="width: 100%;border:1px">
<tr> <td class="auto-style3">
<?php
while($row = $result->fetch_assoc()) {
?>
<?php echo "<br>". $row["player"].""; ?>
<?php
}}
?>
</td> </tr> </table>
it gives me something like this:
but I want it like this:
Can anyone help?
try it like this, using $count to count your item in array. and after 5 items have been echoed, then you put <tr> to echo in new row
<?php
$count=0;
echo "<table>";
while($row = $result->fetch_assoc()) {
if($count==0) {
echo "<tr>";
}
$count++;
echo "<td>".$row["player"]."</td>";
if($count==5) {
echo "</tr>";
$count=0;
}
}
echo "</table>";
?>
I changed your code to add a counter and close the tag every five records, this is the result:
<?php
$sql = "SELECT player FROM `prize` WHERE inviter='$player'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table style="width: 100%;border:1px">
<tr>
<?php
$counter=0;
while($row = $result->fetch_assoc()) {
if($counter == 4){
echo '</tr><tr>';
$counter=0;
}
?>
<td><?php echo $row["player"].""; ?></td>
<?php
$counter++;
}}
?>
</tr> </table>
I need to delete a specific row in php.. so how could I get the ID or other way to delete a specific record
dbconnect.php just a simple database connection
<?php
$con = mysqli_connect("localhost","root","","phpractice");
if(mysqli_connect_errno()){
echo "Database connection failed";
}
?>
index.php the page where the user can see
<html>
<head>
<link rel="stylesheet" type="text/css" href="../styles/index.css">
</head>
<title>Home Page</title>
<?php include'../script/dbconnect.php';?>
<body>
<div id="container">
<div id="table">
<?php
$result = mysqli_query($con,"SELECT * FROM users");
echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>Password</th>
<th colspan=2>Controls</th>
</tr>
";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['password']."</td>";
echo "<td>"."<a href='#'>EDIT</a>"."</td>";
echo "<td><a href='../script/delete.php?id=".$row['user_id']."'>DELETE</a></td>";
echo "</tr>";
}
echo "</table>";
?>
Add User
</div><!--table-->
</div><!--container-->
</body>
</html>
delete.php the delete script
<?php
include '../script/dbconnect.php';
$id = $_GET['user_id'];
$query = "DELETE FROM users WHERE user_id = $id";
mysqli_query($con, $query) or die (mysqli_error($con));
echo "DELETE USER SUCCESSFUL!";
echo "</br>";
echo "<a href='../main/index.php'>RETURN TO DISPLAY</a>";
?>
thanks in advance
in index.php use:
echo "<td><a href='../script/delete.php?user_id=".$row['user_id']."'>DELETE</a></td>";
then use $_GET['user_id']; in delete.php
I can populate correctly the dropbox but I cannot find a way to display the data of the selected item in a table when I click submit. Here is the code:
index.php
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Αναζήτηση Οφειλών Ανά Πολυκατοικία</title>
<link rel="stylesheet" href="tbl_style.css" type ="text/css"/>
</head>
<body>
<form id="form1" name="form1" method="POST" action="search.php">
<?php
include('config.php');
$query = "SELECT DISTINCT odos FROM ofeiles_results ORDER BY odos ASC";
mysql_query("SET CHARACTER SET 'utf8'");
mysql_query("SET NAMES 'utf8'");
$result = mysql_query($query);
echo "<select name='polykatoikia'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['odos'] . "'>" . $row['odos'] . "</option>";
}
echo "</select>";
?>
<input type="submit" name="Submit" value="Select" />
</form>
</html>
</body>
So far so good, the dropbox gets populated. Then in the file search.php I have the following code:
search.php
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Αποτελεσματα Αναζητησης Πολυκατοικιων</title>
<link rel="stylesheet" href="tbl_style.css" type ="text/css"/>
</head>
</html>
<?php
include('config_barcode.php');
if(isset($_POST['select'])){
$odoss = $_POST['select'];
mysql_query("SET CHARACTER SET 'utf8'");
mysql_query("SET NAMES 'utf8'");
$display_query = "SELECT odos FROM ofeiles_results WHERE odos LIKE '" . $odoss . "'";
$result_exoda = mysql_query($display_query) or die(mysql_error());
print $result_exoda;
$odos = $row['odos'];
$app = $row['perigrafh'];
$enoikos = $row['enoikos'];
$mhnas = $row['mhnas'];
$synolo = $row['synolo'];
echo "</br>";
echo "</br>";
echo "</br>";
echo "<table cellpadding='3' cellspacing='0'>";
echo "<tr>";
echo "<th align='center' bgcolor='#FFCC00'><strong>Οδος</strong></th>";
echo "<th align='center' bgcolor='#FFCC00'><strong>Διαμερισμα</strong></th>";
echo "<th align='center' bgcolor='#FFCC00'><strong>Όνομα</strong></th>";
echo "<th align='center' bgcolor='#FFCC00'><strong>Σύνολο</strong></th>";
echo "<th align='center' bgcolor='#FFCC00'><strong>Μήνας</strong></th>";
echo "</tr>";
echo "<td align='center'>".$odos."</td>";
echo " <td align='center'>".$app."</td>";
echo " <td align='center'>".$enoikos."</td>";
echo " <td align='center'>".$mhnas."</td>";
echo " <td align='center'>".$synolo."</td>";
echo "</table></td>";
echo $result_exoda;
}
?>
All I get is a blank page. What am I doing wrong? Thanks.
I use this to display the data in tables, you don't need to use many "echo" use LOOPS
you can add more rows in
$sql = "SELECT your_row , another_row , your_row_again FROM your_table_name";
if you had more rows in you database.
$conn = mysql_connect($dbhost, $dbuser);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT your_row , another_row , your_row_again FROM your_table_name";
mysql_select_db('your_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
echo "<div align = 'center'><table border = '1'>";
echo "<th>Your_row_Header</th><th>Row_Header_again</th><th>Row_Header_again</th>";
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "<tr><td align = center>{$row['Your_row1']}</td>".
"<td align = center>{$row['Your_Row2']}</td></tr>".
"<td align = center>{$row['Your_Row3']}</td>";
}
echo "</table></div><br />";
mysql_close($conn);