I have table which will be populated (from reading from mysql table). I should provide the user an option to select a row and delete it from the database. Till now I have populated the table (by reading from the mysql). But I dont know how to add a checkbox to each row. This is what I have till now.
<html>
<body>
<?php
$username="root";
$password="root";
$database="test";
mysql_connect(localhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM table1";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<?php
$i=0;
echo "<table width='600' cellpadding='5' cellspacing='5' border='1'>";
while ($i < $num) {
$f1=mysql_result($result,$i,"sno");
$f2=mysql_result($result,$i,"lastname");
$f3=mysql_result($result,$i,"firstname");
?>
<font face="Arial, Helvetica, sans-serif"><?php echo "<tr><td> $f1 </td><td>$f2 </td>
<td> $f3 </td></tr>"; ?></font>
<?php
$i++;
}
echo "</table>";
?>
</body>
</html>
Can anyone please help me how to add a checkbox to each row.
Thanks
Just add the HTML code for rendering a checkbox in your php code e.g
<?php echo "<tr><td> $f1 </td><td>$f2 </td>
<td> $f3 </td><td></td><td><input type=\"checkbox\" name=\"checkbox\" value=\"\" id=\"checkbox\"></td></tr>"; ?>
Note the backslashes before the double quotes.
Sadly, i still cannot comment on answers. But i want to improve Max answered code.
I would use this instead :
<?php echo "<tr><td> $f1 </td><td>$f2 </td>
<td> $f3 </td><td></td><td><input type=\"checkbox\" name=\"checkbox[$f1]\" value=\"\" id=\"checkbox\"></td></tr>"; ?>
please note that i add '$f1' variable after 'checkbox' on name variable, so you can post checked row all at once. You can change '$f1' variable into some unique value that suit your needs. I think you'll need it since you want to add checkbox on your data rows. ;)
Related
I have a table in a database with some values referring to humidity, temperature, brightness and noise of a specific room in a specific hour (in a specific date). I want to put them on a html table like this:
example html table
The problem is that my html table has to display only the values of a precise day, so at the end of the day the html table has to have 96 values (from 00:00 to 23:45) and in the next day it has to be emptied.
I realized that there might be two ways to solve it:
At the end of the day, the rows must be deleted, so with the two loops
(one to create the rows and the other for the single cells), the table for the next day can be re-created. I don't know how to delete the html rows!!
At the end of the day, with a "pointer" it is possible to return to
the first row and to change cell's contents.
In both cases, I need a "pointer" that allows to return in the beginning of the table.
Here is my code but there are some bugs. Please also let me know if you have different ideas.
<?php
// Ignore user aborts and allow the script
// to run forever
ignore_user_abort(true);
?>
<html>
<head>
<meta charset="utf-8">
<script>
function waiting()
{
//wait 15 minutes (900 seconds) until the insertion of new data
setTimeout(function() {alert("New line added!"); }, 900);
}
</script>
<title> Benvenuto </title>
</head>
<body>
<table style="width:100%" border='0'>
<tr>
<td valign="top">
<input type="date" value="<?php echo date("Y-m-d");?>">
</td>
</tr>
</table>
<table border='0' style="width:100%">
<tr>
<td>
<b> Hour </b>
</td>
<td>
<b> ID room </b>
</td>
<td>
<b> Humidity </b>
</td>
<td>
<b> Temperature </b>
</td>
<td>
<b> Brightness </b>
</td>
<td>
<b> Noise </b>
</td>
</tr>
</table>
<?php
mysql_connect(" localhost", "root", "root");
mysql_select_db("my_db");
$date = date("d/m/Y");
$hour_now = date('H:i', strtotime('00:00'));
$hour_end = date('H:i', strtotime('23:45'));
$row = 96; //Dynamic number for rows
$col = 6; // Dynamic number for columns
do
{
echo "<table>";
//rows loop
for($i=0;$i<$row;$i++)
{
echo "<tr>";
//cells loop
for($j=0;$j<$col;$j++)
{
echo "<td>";
//hour
$hour_on_table = mysql_query("SELECT hour FROM DATA WHERE hour='$hour_now' AND date='$date'");
echo mysql_result($hour_on_table, 0);
echo "</td>";
echo "<td>";
//id_room
$id_room = mysql_query("SELECT id_room FROM DATA WHERE hour='$hour_now' AND date='$date'");
echo mysql_result($id_room, 0);
echo "</td>";
echo "<td>";
//humidity
$humidity = mysql_query("SELECT humidity FROM DATA WHERE hour='$hour_now' AND date='$date'");
echo mysql_result($humidity, 0);
echo "</td>";
echo "<td>";
//temperature
$temperature = mysql_query("SELECT temperature FROM DATA WHERE hour='$hour_now' AND date='$date'");
echo mysql_result($temperature, 0);
echo "</td>";
echo "<td>";
//brightness
$brightness = mysql_query("SELECT brightness FROM DATA WHERE hour='$hour_now' AND date='$date'");
echo mysql_result($brightness, 0);
echo "</td>";
echo "<td>";
//noise
$noise = mysql_query("SELECT noise FROM DATA WHERE hour='$hour_now' AND date='$date'");
echo mysql_result($noise, 0);
echo "</td>";
echo '<script type="text/javascript"> attesa(); </script>';
$hour_now = date('H:i', strtotime($hour_now) +900); //add 15 min
}
echo "</tr>";
}
echo "<table>";
}
?>
</body>
</html>
I think your best solution would be to write a better query that selects all fields of all records for a day. And then just iterate through the results and echo it.
<?php
if($_REQUEST['date'])
$date = new DateTime($_REQUEST['date']);
else
$date = new DateTime("now");
$db = mysqli_connect('localhost', 'root', 'root', 'my_db');
// Check for SQL connection errors here
$records = mysqli_query($db, "SELECT * FROM DATA WHERE `date`='".$date->format('m/d/Y')."' ORDER BY `hour`");
?>
<html>
<head>
</head>
<body>
<form>
<input type='date' name='date' value='<?php echo $date->format('m/d/Y'); ?>' />
<button type='submit'>Update</button>
</form>
<table>
<thead><tr>
<td>Hour</td>
<td>ID Room</td>
<td>Humidity</td>
<td>Temperature</td>
<td>Brightness<td>
<td>Noise<td>
</tr></head>
<tbody>
<?php
while($record = mysqli_fetch_assoc($records)){
echo "<tr>";
echo "<td>{$record['hour']}</td>";
echo "<td>{$record['ID_room']}</td>";
echo "<td>{$record['humidity']}</td>";
echo "<td>{$record['temperature']}</td>";
echo "<td>{$record['brightness']}</td>";
echo "<td>{$record['noise']}</td>";
echo "</td>";
}
?>
</tbody>
</table>
</body>
</html>
No. You sent it out to the browser. You have to use javascript to delete it.
But you should probably not make your php script run forever. Something will time out sooner or later between your server and your browser.
You should use an ajax call in setTimeout to refresh the data. E.g. you could use jquery.Ajax, parse the response with jQuery.parseHTML, cut out the table from it, and replace the old table with replaceWith
Something like this (untested):
$.ajax(url).done(function(data) {
$('table:first') // this finds the first table element in the html
.replaceWith(
$.parseHTML(response).find('table:first') // cut out the table element from the response
); // replace the table in the html with the one in the ajax response
})
The values are not updated in mysql database while using the following code.
I wanted to update database hell with the new values entered in textbox.
<?php
$con=mysql_connect("localhost","host","pass");
mysql_select_db("Host",$con);
if(isset($_POST['update'])){
$upd= "UPDATE hell SET name='".$_POST['name']."'
WHERE rno='".$_POST['rno']."'";
mysql_query($upd,$con);
}
$sql="SELECT * FROM hell";
$rec=mysql_query($sql,$con);
?>
<html>
<body>
<table width="600" border="1" cellspacing="1" cellpadding="1">
<tr>
<th>Name</th>
<th>Roll No.</th>
</tr>
<?php
while($arr=mysql_fetch_assoc($rec))
{
echo "<form action=untitled2.php method=post>";
echo "<tr>";
echo "<td>"."<input type=text name='name' value='".$arr['name']."'>"."
</td>";
echo "<td>".$arr['rno']."</td>";
echo "<input type=hidden name='rno' id='rno' value='".$arr['rno']."'>";
echo "<td>"."<input type=submit value='update'>"."</td>";
echo "</tr>";
echo "</form>";
}
?>
</table>
</body>
</html>
So many errors in your code :
1) Dont use mysql_*. It is deprecated and removed from PHP 7. Use mysqli_* or PDO.
2) Mysql connection should be used like this :
$con = mysql_connect("localhost","host","pass");
mysql_select_db("Host",$con);
3) Your update query is wrong and it's execution.
Try this :
$upd= "UPDATE hell SET name='".$_POST['name']."' WHERE rno='".$_POST['rno']."'";
mysql_query($upd,$con);
4) Select query execution should be like this :
$sql="SELECT * FROM hell";
$rec=mysql_query($sql,$con);
5) You can not use form inside table. It's invalid html format.
6) Read this : How can I prevent SQL injection in PHP?
I have the following code:
$sql = "SELECT * FROM Tickets WHERE stat='Open'";
$result = mysql_query($sql);
mysql_close($con);
?>
<!DOCTYPE>
<html>
<body>
<table class="striped">
<tr class="header">
<td>Username</td>
<td>Title</td>
<td>Description</td>
<td>Admin Name</td>
<td>Category</td>
<td>Status</td>
<td>Urgency</td>
<td>Time In</td>
<td> </td>
</tr>
<?php
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row[username]."</td>";
echo "<td>".$row[title]."</td>";
echo "<td>".$row[description]."</td>";?>
<td><select>
<?php
echo "<option value'".$row[admin_name]."'>".$row[admin_name]."</option>";
$sql = mysql_query("SELECT username FROM Users WHERE user_type='admin'");
while ($u = mysql_fetch_array($sql)){
echo "<option value='".$u['username']."'>".$u['username']."</option>";
}
?>
</select></td>
<?php
echo "<td>".$row[category]."</td>";
echo "<td>".$row[stat]."</td>";
echo "<td>".$row[urgency]."</td>";
echo "<td>".$row[time_in]."</td>";
echo "<td><a href='close.php'>Close Ticket</a></td>";
echo "</tr>";
}
?>
</table>
<a href='update.php'>Update</a>
</body>
</html>
I have two links on this page. Both of them need to update a SQL database. The Close ticket link needs to just update the single row, while the update link should update all of them. I am not sure how to get the info from one php to the next. It seems like you can put the individual row information into a Post array for the close ticket link, but I am not sure how. For the update link it needs to take the value of the dropdown in the table and change the admin_name field to that value.
I am using the book "PHP and SQL for dummies 4th edition" to learn web programming. My PetCatalog interface is meant to retrieve and display all petTypes from my Database. It is retrieving the pets in the database but it is doing so without the radio buttons. I do not know where the error is from because everything seems to be in place in the code.
Note: Everything works perfectly except that the radio buttons symbol are not being displayed. PS: There need to be a fictional database for this program to make any sense to anyone trying to help because the database is in my system. Thanks.
<?php
/* Program: PetCatalog.php
* Desc: Displays a list of pet categories from the
* PetType table. Includes descriptions.
* Displays radio buttons for user to check.
*/
?>
<html>
<head><title>Pet Types</title></head>
<body>
<?php
$user="root";
$host="localhost";
$password="";
$database="PetCatalog";
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
/* Select all categories from PetType table */
$query = "SELECT * FROM PetType ORDER BY petType";
$result = mysqli_query($cxn,$query)
or die ("Couldn't execute query.");
/* Display text before form */
echo "<div style='margin-left: .1in'>\n
<h1 style='text-align: center'>Pet Catalog</h1>\n
<h2 style='text-align: center'>The following animal
friends are waiting for you.</h2>\n
<p style='text-align: center'>Find just what you want
and hurry in to the store to pick up your
new friend.</p>
<h3>Which pet are you interested in?</h3>\n";
/* Create form containing selection list */
echo "<form action='ShowPets.php' method='POST'>\n";
echo "<table cellpadding='5' border='1'>";
$counter=1;
while($row = mysqli_fetch_assoc($result))
{
extract($row);
echo "<tr><td valign='top' width='15%'
style='font-weight: bold;
font-size:1.2em'\n";
echo "<input type='radio' name='interest' value='$petType'\n";
if( $counter == 1 )
{
echo "checked='checked'";
}
echo ">$petType</td>";
echo "<td>$typeDescription</td></tr>";
$counter++;
}
echo "</table>";
echo "<p><input type='submit' value='Select Pet Type'>
</form></p>\n";
?>
</div>
</body>
</html>
The first thing that jumps out at me is the fact that your line with
echo "<tr><td valign='top' width='15%'
style='font-weight: bold;
font-size:1.2em'\n";
does not close the <td> tag. This could interfere with the creation of your <input> tags.
Try
echo "<tr><td valign='top' width='15%'
style='font-weight: bold;
font-size:1.2em'>\n";
and see if that works.
Like justathoughtor2 said close the table data and table row </td></tr> i have tested your without closing the table data and table row
after closing the table data and table row
With the code below I have to report to a table of rows from a database.
But in addition to the lines that I need, is displayed containing parts in php code (noName).
I try to keep only the echo tag
echo $row[0];
but i get the error
Notice: Undefined offset: 1 in test.php on line 16
MYSQL
ID | Team
------------------------------
1 test1
2 test2
3 test3
PHP
<?php
$connection = mysqli_connect("YourHost","user","password","dbName") or die('connection to DB failed');
$query = mysqli_query($connection,"SELECT team FROM s1");
while ($row = mysqli_fetch_array($query,MYSQLI_NUM)) {
?>
<table>
<tr>
<td><p> team name <?php if(isset($row[0])){
echo $row[0];
}else{
echo 'noName';
} ?></p></td>
</tr>
<tr>
<td><p> team name <?php if(isset($row[1])){
echo $row[1];
}else{
echo 'noName';
} ?></p></td>
</tr>
<?php } ?>
Output in php page:
test1
noName
test2
noName
You are selecting just 1 field that is team from the database.
So in your code PHP if you write $row[0] is the same if you write $row['team'].
$row[1] so is empty.
If you want print the name of all team you have to write this code:
...
echo "<table>";
while ($row = mysqli_fetch_array($query,MYSQLI_NUM)) {
?>
<tr>
<td><p> team name <?php echo $row[0]; ?></p></td>
</tr>
i would write your code like this:
<?php
$connection = mysqli_connect("YourHost","user","password","dbName") or die('connection to DB failed');
$query = mysqli_query($connection,"SELECT team FROM s1");
if($query) {
echo "<table>";
while ($row = mysqli_fetch_array($query,MYSQLI_NUM)) {
echo "<tr>";
echo "<td><p> team name " . $row['team'] . "</p></td>";
echo "</tr>";
}
echo "</table>";
}
?>
Look at your SQL query:
"SELECT team FROM s1"
You're only selecting one value from the table, the value called team. So each row returned by this query will have only one value. That value is in $row[0]. Hence, there is no value in $row[1]. In order to populate $row[1] with something, your SELECT will need at least a second value:
"SELECT team, someOtherValue FROM s1"
Of course, if your table has no other values, then there's nothing else to display...
It looks like you're trying to display the same conceptual value twice in each loop. Why? Generally each iteration of the loop would add a single row to the HTML table, not two rows. Something like this:
while ($row = mysqli_fetch_array($query,MYSQLI_NUM)) {
?>
<table>
<tr>
<td><p> team name <?php if(isset($row[0])){
echo $row[0];
}else{
echo 'noName';
} ?></p></td>
</tr>
<?php } ?>