I have searched for an answer that relates to my code but I'm new to php and html so I would appreciate the help.
So I've created code that uses loops to display a html table with the contents of the specified table from a mysql database. I would like to give the user an option to delete a row when the results are displayed.
This is my code so far to display the results of my database table:
<html><head><title>MySQL Table Viewer</title></head><body>
<?php
$db_host = 'localhost';
$db_user = 'username';
$db_pwd = 'password';
$database = 'dvdproject';
$table = 'Employee';
$con= mysql_connect($db_host,$db_user,$db_pwd,$database);
if(!$con){
die("Can not connect" . mysql_error());
}
mysql_select_db($database,$con);
// sending query
$result = mysql_query("SELECT * FROM {$table}");
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
foreach($row as $cell)
echo "<td>$cell</td>";
echo '<form method="POST" name="deleterequest" action =
"deleterequest.php">';
echo "<input name='record_id' type='hidden' value='".$row['id']."'
>";
echo "<input name='delete'type='submit' value='Delete' >";
echo "</form>";
echo "</tr>\n";
}
echo "</table>";
mysql_close($con);
?>
No matter where I place my form to delete button, 6 delete buttons take up an entire row followed by the actual rows.
I would like the delete button to be after each row but I just cant get my head around it!
Apologies if I am overlooking something here, but have you tried wrapping your delete button with <td> tags?
foreach($row as $cell)
echo "<td>$cell</td>";
echo "<td>";
echo '<form method="POST" name="deleterequest" action="deleterequest.php">';
echo "<input name='record_id' type='hidden' value='".$row['id']."'>";
echo "<input name='delete'type='submit' value='Delete' >";
echo "</form>";
echo "</td>";
echo "</tr>\n";
}
I stripped everything bar HTML away from this and found a <td> tag around the form worked a treat.
Related
I have to make a dynamic table n*n , the user first gives the number n and the program makes a 5*5 table with check box this part I have make it, the second part is the user checks same of the checkbox and clicks on submit and the program makes again a table 5*5 but in the place of check box which checks is colored. I have uploaded and image.
Sorry for my bad English, thanks for your time.
enter image description here
<form name="form" action="" method="get">
<input type="text" name="subject" id="subject" value="Give value">
</form>
<?php
$rows = $cols = $name = "";
if(isset($_GET['subject']))
$rows = $cols = $_GET['subject'];
if(isset($_POST['check_list']))
$name = $_POST['check_list'];
if(isset($_GET['subject'])){
echo "<form action='my.php' method='post'>";
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td><input type='checkbox' name='check_list[]' value='value ".$td."'></td>";
}
echo "<tr>";
}
echo "</table>";
echo "<input type='submit' />
</form>";
}
// this part of code is not make the third excecution the number 3 image
echo $cols;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
foreach($_POST['check_list'] as $value){
if($tr == $value[td])echo "<td bgcolor='#FF0000'></td>";
else
echo "<td> </td>";
}
echo "</tr>";
}
echo "</table>";
?>
I am working on Simple Admin Panel,
The method i am working on is to select the data from database and put it into textarea and behind the textarea update button,
when i update the textarea click update to execute query to update the table
but when i click update at the first row for example it execute the third row only even if i clicked the first row update button " picture attached "
<?php
include 'config.php';
echo '<link rel="stylesheet" href="style.css"type="text/css">';
$result = mysql_query("SELECT * FROM English");
while($row = mysql_fetch_array($result))
{
echo "<form action='' method='post'>";
echo "<table>";
echo "<tr>";
echo "<td><textarea rows='1' cols='1' name='txtid' readonly style='overflow:auto;resize:none'>" . $row['ID'] . "</textarea></td>";
echo "<td><textarea rows='4' cols='50' name='txtarea'>" . $row['Content'] . "</textarea></td>";
echo "<td><input type='submit' name='button' value='Update!'/></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
if(isset($_POST['button'])){
$textarea =$_POST['txtarea'];
$id = $_POST['txtid'];
$sql = "UPDATE English SET Content='".$textarea."' WHERE ID='".$id."'";
echo $textarea; echo $id;
mysql_query( $sql, $conn );
}
mysql_close($conn);
?>
Example
Well,
Changed the location for closing braces
to be after
echo "</table>";
echo "</form>";
it fixed the problem
In viewsessions.php, I am attempting to select multiple values from a db and insert it into grouped checkbox fields (eg. typeofactivity, employer, date, time, amount as one single checkbox field) using <input type="checkbox" name="example" id="example"> or similar in JQuery mobile, so that when displayed on the hmtl page, various checkboxes can be selected to send to another page. So far, i have managed to display the contents of the db using a table, but can't find a working solution to display the data in checkboxes?
Any ideas greatly appreciated! :)
Code so far:
viewsessions.php
<?php
$servername = "localhost";
$username = "root";
$password = "cornwall";
$con=mysqli_connect('localhost','root','cornwall','ibill');
// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill':
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// The connection is then checked, if it fails, an echo is sent back to the page stating a connection error.
$viewsessions = "SELECT typeofactivity, employer, date, time, amount FROM session_details";
$result = $con->query($viewsessions);
if ($result->num_rows > 0) {
echo "<table><tr>
<th>Type of Activity</th>
<th>Employer</th>
<th>Date</th>
<th>Time</th>
<th>Amount (GBP)</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["typeofactivity"]."</td>
<td>".$row["employer"]."</td>
<td>".$row["date"]."</td>
<td>".$row["time"]."</td>
<td>".$row["amount"]."</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$con->close();
?>
echo "<tr>";
echo "<td>" .$row['typeofactivity']. "</td>";
echo "<td>" .$row['employer']. "</td>";
echo "<td>" .$row['date']. "</td>";
echo "<td>" .$row['time']. "</td>";
echo "<td>" .$row['amount']. "</td>";
echo "<td> <input type='checkbox' name='checkbox' value='' id='checkbox' /></td>";
echo "</tr>";
I'm trying to get my table to look like this
http://gyazo.com/6a134b723c30e97fa99559158cde4b1e
but when I use my code looks like this
http://gyazo.com/1c08f83e744b3a20c99b55eee5313045
I can't get the quantity field or add field to work for the life of me; the quantity field is just blank. I would greatly appreciate some help because I can't get it to work.
<?php require_once("include/db_connect.php"); ?>
<html>
<head><title>Displaying Image files using PHP</title></head>
<body>
<h1>Displaying Images from an Image table using PHP</h1>
<?php
$db_link = db_connect("project");
// Retrieve table properties
$fields = mysql_list_fields("project", "bookstore");
$num_columns = mysql_num_fields($fields);
// Make a simple database query to select all columns and rows
$query = "SELECT * FROM bookstore";
$result = mysql_query($query) or die("SQL query failed");
// Display results as an HTML table. Note how mysql_field name
// uses the $fields object to extract the column names
echo '<table border="1" cellpadding = "5" >';
// Display the column names
echo "<tr>";
for ($i = 0; $i < $num_columns; $i++)
{
echo "<th>", mysql_field_name($fields, $i), "</th>";
}
echo "<th>"Quantity "</th>";
echo "</tr>";
// Loop over the rows of the table.
// $row contains the information for each row
// Refer to the names of the fields in the table
// Must ahow the path where the image files are held
while ($row = mysql_fetch_assoc($result))
{
echo "<tr>
<form action='somethingToHandleForm.php' method='post'>";
echo "<td>". $row['isbn']. "</td>";
echo "<td>". $row['title']."</td>";
echo "<td>". $row['author']."</td>";
echo "<td>". $row['pub']."</td>";
echo "<td>". $row['year']."</td>";
echo "<td>". $row['price']."</td>";
echo "<td><input type='text' name='quantity' value=".$row['quantity']."/></td>";
echo "<td><input type='submit' value='add></td>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
// Free the resources and close the connection
mysql_free_result($result);
mysql_close($db_link);
?>
</body>
</html>
One thing I noticed is you're missing a single quote in this line after value=add:
<input type='submit' value='add>
You have some formatting problems in your HTML, which look to be the source of your problems when rendering the table. Specifically the lines:
echo "<td><input type='text' name='quantity' value=".$row['quantity']."/></td>";
echo "<td><input type='submit' value='add></td>";
The value attribute for the first line is not properly enclosed in quotes, and there is no end single-quote for the value add in the second line (nor are you closing the input tag), try the following:
echo "<td><input type='text' name='quantity' value='".$row['quantity']."' /></td>";
echo "<td><input type='submit' value='add' /></td>";
I'm having bad time to update multiple tables from a form. I've done the query and checked for errors using (or die), but it seems that there are no errors in my MySQL codes. I can update the main table's data, but I can't update the other table. I'm suspecting that my form fields have some problem. This is my form codes :
<?php
$sql= "SELECT * FROM client WHERE resID=".$_GET["resID"];
$rs = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$sqlM= "SELECT * FROM menu WHERE resID=".$_GET["resID"];
$rsM = mysql_query($sqlM) or die($sqlM."<br/><br/>".mysql_error());
$i = 0;
echo '<table width="50%">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>Name</td>';
echo '<td>Edit</td>';
echo '</tr>';
echo "<form name='form_update' method='post' action='client_admin_post.php'>\n";
$f=mysql_fetch_array($rs);echo '<tr>';
echo "<td>Res ID :</td>";
echo "<td>{$f['resID']}<input type='hidden' name='resID' value='{$f['resID']}' /></td>";
echo '</tr>';
++$i;
echo '<tr>';
echo "<td>Restaurant Name :</td>";
echo "<td><input type='text' size='40' name='resName' value='{$f['resName']}' /></td>";
echo '</tr>';
++$i;
while ($fM = mysql_fetch_array($rsM)) {
echo '<tr>';
echo "<td>Menu :</td>";
echo "<td><input type='text' size='40' name='mname[$i]' value='{$fM['name']}' /></td>";
echo "<td>{$fM['id']}<input type='hidden' name='mid[$i]' value='{$fM['id']}' /></td>";
echo '</tr>';
++$i;
}
echo '<tr>';
echo "<td><input type='submit' value='submit' /></td>";
echo '</tr>';
echo "</form>";
echo '</table>';
?>
This is my POST codes :
<?php
//session_start();
include_once("connection.php");
$resID= $_POST["resID"];
$resName= $_POST["resName"];
$sql = "UPDATE client ".
"SET resName = '$resName' ".
"WHERE resID = '$resID' " ;
mysql_query($sql) or die ('query failed:' . mysql_error());
$size = count($_POST['mname']);
$i = 0;
while ($i < $size) {
$name= $_POST['mname'][$i];
$id = $_POST['mid'][$i];
$sqlM = "UPDATE menu SET name = '$name' WHERE id = '$id' LIMIT 1";
mysql_query($sqlM) or die ("Error in query: $sqlM");
echo "$name<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
?>
As you guys know, one restaurant got many menus. I can edit the main restaurant info, but I can't edit the menus. Please help me. Really appreciate your help. Thanks :D
You ++$i at least twice before outputing it as $mname index. So $_POST['mname'] would have indices 2 .. count_of_rows+1. Your while() ignores this fact and counts 0 .. size.
If you had all warnings turned on (as you should) you would get a warning indicating that invalid index 0 is used on line starting "$name= $_POST..." and you would have been able to figure out from there.