I've got a problem with a html table in a php code.
I try this code but I have a problem with href in a tag. In fact when I click on the href it post undefined while DetailsAnnonces.php exist.
Thanks for your response.
<?php
$conn=#mysqli_connect($_SESSION['servername'],$_SESSION['username'],
$_SESSION['password'], $_SESSION['database']) or die(mysql_error());
$verifExistence1 = "SELECT * FROM `annonces`;";
$result = mysqli_query($conn, $verifExistence1);
echo "<table border = 2>";
echo "<table align='center'> <thead>
<tr>
<th>Titre</th>
<th>Description</th>
<th>Image</th>
</tr>
</thead>
<tbody>";
while($row = #mysqli_fetch_assoc($result)){
$idbis=$row['id'];
$titrebis=$row['titre'];
echo "<tr>
<td>" . $row['titre'] . "</td><td>" . $row['description'] . "</td>
<td> $titrebis
</tr>";
}
echo "</table>";
//echo $html_table;
?>
The problem was caused by a javascript left on this page.
The problem is in the $idbis
<?php
echo "
<td>
'".$titrebis."
</td>
</tr>";
?>
Related
I want to generate Bootstrap 5 table .table-striped using data from MySQL, table: [users]
The table in plain HTML looks fine, but as soon as I generate it using PHP all styling disappears
PHP Code:
<?php
$result = mysqli_query($link,"SELECT * FROM users ORDER BY id ASC");
echo "<table class='table table-striped'>
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Rank</th>
<th>Created at</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['rank'] . "</td>";
echo "<td>" . $row['created_at'] . "</td>";
echo "</tr>";
echo " </tbody>";
}
echo "</table>";
mysqli_close($link);
?>
and I have <div class='table-responsive'> before <?php and </div> after ?>
I would be glad if you could explain me where I am making a mistake and I would like the table to have the same styling as in pure HTML
EDIT:
rendered HTML
<div class='table-responsive'>
<table class='table table-striped'>
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Rank</th>
<th>Created at</th>
</tr>
</thead><tbody><tr><td>39</td><td></td><td>julia</td><td>2021-12-20 00:42:14</td></tr> </tbody><tbody><tr><td>40</td><td>test</td><td>user</td><td>2021-12-20 02:35:37</td></tr> </tbody><tbody><tr><td>41</td><td>testt</td><td>user</td><td>2021-12-20 17:22:43</td></tr> </tbody></table></div>
You are building a new tbody for every row. You should move the tbody creation outside the while loop. You also only need 1 echo, rather than echoing every line. I would do:
<?php
$result = mysqli_query($link,"SELECT * FROM users ORDER BY id ASC");
$output = "<div class='table-responsive'>
<table class='table table-striped'>
<thead>
<tr>
<th style='background-color:#ad8c70'>#</th>
<th style='background-color:#ad8c70'>Username</th>
<th style='background-color:#ad8c70'>Rank</th>
<th style='background-color:#ad8c70'>Created at</th>
</tr>
</thead>
<tbdoy>";
while($row = mysqli_fetch_array($result))
{
$output .= "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['username'] . "</td>
<td>" . $row['rank'] . "</td>
<td>" . $row['created_at'] . "</td>
</tr>";
}
mysqli_close($link);
$output .= '</tbody></table>';
echo $output;
?>
I'm currently trying to refresh my table every 5 seconds, but I'm not being successful.
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Número</th>
<th>Nome do colaborador / secção</th>
</tr>
</thead>
<tbody>
<?php
$req = mysqli_query($con, 'select user_id, user_name from users');
while ($dnn = mysqli_fetch_array($req)) {
echo "<tr>";
echo " <td>" . $dnn['user_id'] . "</td>";
echo " <td>" . $dnn['user_name'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
What I want is to refresh the page every 5 seconds and of course if someone edits he will be able to see the changes that were made.
What can I do to complete this action?
You need to use $.ajax like
$(function(){
function getdata(){
$.ajax({
url:'getdata.php',
success:function(response){
$('#dataTables-example tbody').html(response);
}
});
}
setInterval(function(){getdata()},5000);
});
getdata.php
<?php
$req = mysqli_query($con, 'select user_id, user_name from users');
while ($dnn = mysqli_fetch_array($req)) {
echo "<tr>
<td>" . $dnn['user_id'] . "</td>
<td>" . $dnn['user_name'] . "</td>
</tr>";
}
?>
Im new to php and i have this code tested with XAMPP at backend
$name = addslashes ($_POST['name']);
$email = addslashes ($_POST['email']);
$paswd = addslashes ($_POST['paswd']);
$sql = "INSERT INTO webusers (username,email,paswd)VALUES('$name','$email','$paswd')";
and here's my code for displaying data
include_once('dbcon.php');
$db = mysql_select_db('test');
$sql = "SELECT * FROM webusers";
$result = mysql_query($sql);
$urow = mysql_num_rows($result);
echo"
<table border='1'>
<th>ID</th>
<th>USERNAME</th>
<th>EMAIL</th>
<th>PASSWORD</th>
";
if($result > 0){
while($urow = mysql_fetch_array($result)){
echo "<tr>";
echo" <td class='myinput'>'" .$urow['id']. "'</td>";
echo" <td class='myinput'>'" .$urow['username']. "'</td>";
echo" <td class='myinput'>'" .$urow['email']. "'</td>";
echo" <td class='myinput'>'" .$urow['paswd']. "'</td>";
echo "</tr>";
echo"</table>";
}
}else{
echo"No record";
}
?>
Those code works, except for the data which surprise me why it has a single quote when i show/display it on a table. though i input the data in html . and my magic_quote_gpc was off. is there something i missed or anything wrong with my code? or there is something with my database collation?
i also tried mysql_real_escape_string and mysql_escape_string, nothings change.
thanks for the help.
Otep
Because you put Single Quotes in the HTML ?
echo"
<table border='1'>
<tr>
<th>ID</th>
<th>USERNAME</th>
<th>EMAIL</th>
<th>PASSWORD</th>
</tr>
";
if($result > 0){
while($urow = mysql_fetch_array($result)){
echo "<tr>";
echo" <td class=\"myinput\">" .$urow['id']. "</td>";
echo" <td class=\"myinput\">" .$urow['username']. "</td>";
echo" <td class=\"myinput\">" .$urow['email']. "</td>";
echo" <td class=\"myinput\">" .$urow['paswd']. "</td>";
echo "</tr>";
echo"</table>";
}
}else{
echo"No record";
}
Try that, your code without Single quotes
You are printing a ' around your values in your HTML Markup
echo "<tr>";
echo" <td class='myinput'>" .$urow['id']. "</td>";
echo" <td class='myinput'>" .$urow['username']. "</td>";
echo" <td class='myinput'>" .$urow['email']. "</td>";
echo" <td class='myinput'>" .$urow['paswd']. "</td>";
echo "</tr>";
is what you really want to echo
Try this one
if($result > 0){
while($urow = mysql_fetch_array($result)){
echo "<tr>";
echo" <td class='myinput'>" .stripslashes($urow['id']). "</td>";
echo" <td class='myinput'>" .stripslashes($urow['username']). "</td>";
echo" <td class='myinput'>" .stripslashes($urow['email']). "</td>";
echo" <td class='myinput'>" .stripslashes($urow['paswd']). "</td>";
echo "</tr>";
echo"</table>";
}
}else{
echo"No record";
}
every time user insert information and click add button, new data will store to database and echo into this table
table
<tr>
<td colspan="4" align="right">
<input type="image" value="image" src="images/btn_add.gif" onclick="action_1()">
</td>
</tr>
<tr>
<td colspan="2" class="title_all_u">Family Member Summary</td>
</tr>
<tr>
<td>
<?php
$query = "SELECT * FROM family_child WHERE LAS_login_id = ($emp_id)";
$result = mysql_query($query) or die(mysql_error());
echo "<table border='1'>
<tr>
<th>Family Type</th>
<th>Name</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['family_child_type'] . "</td>";
echo "<td>" . $row['family_child_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</td>
</tr>
and this is insert query
if ( $action ==3 ) {
$spouse_type = $_POST['spouse_type'];
$spouse_name = $_POST['spouse_name'];
$sql1 = "INSERT INTO family_spouse (LAS_login_id, spouse_type, spouse_name) VALUES ('$LAS_login_id', '".strtoupper($spouse_type)."','".strtoupper($spouse_name)."')";
this 2 code is working for insert into database and echo in the page.
How can I add delete button below echo "<td>" . $row['family_child_name'] . "</td>"; for each row that I echo so user can delete the wrong row in the display table.
echo "<td>" . $row['family_child_name'] . " <a href='page.php&action=delete&id=family_name_id'>Delete</a></td>
Then in your page get (if action == delete) > execute your query to delete the row where the ID is the id of the family child name table.
$query = "SELECT * FROM family_child WHERE LAS_login_id = ($emp_id)";
$result = mysql_query($query) or die(mysql_error());
echo "<table border='1'>
<tr>
<th>Family Type</th>
<th>Name</th>
<th>Action</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>
<td>" . $row['family_child_type'] . "</td>
<td>" . $row['family_child_name'] . "</td>
<td>
<a href='../path/process.php?action=delete&id=".$row['family_child_id']."'>
DELETE RECORD
</a>
</td>
</tr>";
}
echo "</table>";
?>
on process page check action = delete and write query for delete there
family_child_id = ur primary key of table change according to ur table details
EDIT
PROCESS PAGE:
if($_GET["action"] == "delete")
{
$sql3="DELETE FROM family_child WHERE LAS_login_id =".$_GET["LAS_login_id"];
$result3 = mysql_query($sql3);
if (mysql_affected_rows() > 0)
{
header("location:dashboard.php?tab=1");
}
}
this coding working perfectly. i tried in my pc and posted this code here. it will insert the delete button automatically when u insert the new record. and then if u click delete button it will delete the row details in mysql db..
<body>
<?
echo "<tr>
<td>";
// your database connection
// select database
$query = ("SELECT * FROM family_child");
$result = mysql_query($query) or die(mysql_error());
echo "<table border=1>
<tr>
<th>Family Type</th>
<th>Name</th>
<th>Delete Record</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['family_child_type'] . "</td>";
echo "<td>" . $row['family_child_name'] . "</td>";
echo "<td><form method=post>
<input name=id type=hidden value='".$row['family_child_name']."';>
<input type=submit name=submit value=Delete>
</form></td>";
echo "</tr>";
}
echo "</table>";
echo "</td>
</tr>";
// delete record
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(isset($_POST['id']))
{
$id = mysql_real_escape_string($_POST['id']);
$sql = mysql_query("DELETE FROM family_child WHERE family_child_name ='$id'");
if(!$sql)
{
echo ("Could not delete rows" .mysql_error());
}
}
}
?>
I'm using php to create a table from the data extracted from MySQL database. I'm using SELECT * From table_name and all data is appearing as I wish. But I want to create a Drop Down menu in the last column for that I'm using the function displayDMenu. But the drop down menu is still not appearing in the table. Can you please suggest what is causing the problem and solution? Or alternate solutions? Here's the code:
<div id="main">
<?php
include("config.php");
$result = mysql_query("SELECT * FROM schedule");
echo "<table border='1'>
<tr>
<th>Appoint No.</th>
<th>CR number</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>Year</th>
<th>Department</th>
<th>Status</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td style='padding:3px'>" . $row['S_ID'] . "</td>";
echo "<td style='padding:3px'>" . $row['crnumber'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdTime'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdDate'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdMonth'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdYear'] . "</td>";
echo "<td style='padding:3px'>" . $row['DName'] . "</td>";
echo "<td 'style='padding:3px'>" . displayDMenu() . "</td>";
echo "</tr>";
}
echo "</table>";
function displayDMenu() {
$r = '';
$r .='<form method="post" action="ScdApproval.php">';
$r .='<select name="Status">';
$r .='<option value="approved" selected>Approve</option>';
$r .='<option value="disapproved">Disapprove</option>';
$r .='</select>';
$r .='</form>';
}
?>
</div> <!--main ends here -->
Not sure why you need the user function its not doing anything logical its just spitting back html which there is no reason not to put that html in the while loop.
<?php
include("config.php");
$result = mysql_query("SELECT * FROM schedule");
?>
<style>#main table tr td{padding:3px;}</style>
<div id="main">
<table border="1">
<tr>
<th>Appoint No.</th>
<th>CR number</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>Year</th>
<th>Department</th>
<th>Status</th>
</tr>
<?php
if(mysql_num_rows($result) > 0):
while($row = mysql_fetch_array($result)): ?>
<tr>
<td><?php echo $row['S_ID']?></td>
<td><?php echo $row['crnumber']?></td>
<td><?php echo $row['ScdTime']?></td>
<td><?php echo $row['ScdDate']?></td>
<td><?php echo $row['ScdMonth']?></td>
<td><?php echo $row['ScdYear']?></td>
<td><?php echo $row['DName']?></td>
<td><form method="post" action="ScdApproval.php">
<select name="Status">
<option value="approved" selected>Approve</option>
<option value="disapproved">Disapprove</option>
</select>
</form>
</td>
</tr>
<?php
endwhile;
else: ?>
<tr>
<td rowspan="8">No results</td>
</tr>
<?php endif;?>
</table>
</div> <!--main ends here -->
Also:
Really you should also switch to using PDO or mysqli prepared querys for new code.
You need to return the value $r in the function displayDMenu()
ie
function displayDMenu()
{
.........
return $r;
}