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>";
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 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.
I'm trying to make a HTML table as a frontend to a mySQL database. The table displays fine and I can type in the edits I want to make to each row of the table but when I press the submit button the changes aren't actually made. Can anyone see where I'm going wrong?
<?php
include("db.php");
$sql = "SELECT * FROM `artist`";
$result = mysqli_query($conn, $sql);
if (isset($_POST['update'])){
$artID = $_POST['artID'];
$artName = $_POST['artName'];
$key = $_POST['hidden'];
$UpdateQuery = "UPDATE `artist` SET `artID` = '$artID', `artName` = '$artName' WHERE `artist`.`artID` = '$key'";
mysqli_query($conn,$UpdateQuery);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
};
echo "<table border='1'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "</tr>";
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
The db.php file simply includes the connection info to the mySQL database and I'm 100% sure there's nothing wrong with it as it retrieves the table correctly it just doesn't update.
You are putting form tag inside tr which is not allowed td are only allowed
so you have to remove that tr from there.
You have to use jquery or you can replace the table with some other grid structure so that it can look the same and the form can be placed there as well
One more suggestion Don't mix the php and html together separate them for the clean code
If you do all these you code will be like this
Your form is being constructed with multiple elements with the same name. When you submit the form it is using the last elements as the values so regardless of the record you want updated the last record is being updated (or throwing an error because of string encapsulation). You should use parameterized queries as well.
So instead of:
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
Use:
if ($result->num_rows > 0) {
// output data of each row
while($row = mysqli_fetch_array($result)) {?>
<form class='artisttable' action ='getartiststable.php' method ='post'>
<tr>
<td><input type='text' name ='artID' value ='<?php echo $row['artID'];?>' /></td>
<td><input type='text' name ='artName' value ='<?php echo $row["artName"];?>' /></td>
<td><input type = 'hidden' name ='hidden' value='<?php echo $row['artID'];?>' /></td>
<td><input type='submit' name ='update'" . " </td>
</tr>
</form>
<?php } ?>
</table>
So you get a form for each data set. Here's a link on prepared statements with mysqli: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php. You also should update your mark up. Tables for formatting aren't the best approach. Your inputs also weren't closed missing >.
Also this changed artisttable from an id to class because there will be multiples. Update CSS/JS accordingly.
Im a newbie and working on a project for school
I have a website that lists foods.
I have an update table that allows me to change and add data.
For the food group field I have it cross reference another table called food_group which has the food_group(name) and an id.
When you view the food data you can see the name that it pulls instead of the ID. On the update page I would like a drop down to be in the place of the ID. So you can see the "friendly" name instead of the ID number, but it has to store the ID not the friendly name in the food table.
Website can be found at http://web.nmsu.edu/~jrortiz/ICT458/FINAL/
The code I have is:
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect("localhost","user","pw","db");
if (!$con){
die("Can not connect: " . mysql_error());
}
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE food SET food_group='$_POST[Food_group]', food='$_POST[Food]', ph='$_POST[PH]' WHERE food='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM food WHERE Food='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO food (Food_group, Food, PH) VALUES ('$_POST[addGroup]','$_POST[addFood]','$_POST[addPH]')";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM food";
$myData = mysqli_query($con,$sql);
echo "<table border=1>
<tr>
<th>Food Group</th>
<th>Food</th>
<th>PH</th>
<th>Update/Add</th>
<th>Delete</th>
</tr>";
while($record = mysqli_fetch_array($myData)){
echo "<form action=updateFood.php method=post>";
echo "<tr>";
echo "<td><input type='text' name='Food_group' value='$record[food_group]'/></td>";
echo "<td><input type='text' name='Food' value='$record[food]'/></td>";
echo "<td><input type='text' name='PH' value='$record[ph]'/></td>";
echo "<td><input type='submit' name='update' value='update'/></td>";
echo "<td><input type='submit' name='delete' value='delete'/></td>";
echo "<td><input type='hidden' name='hidden' value='$record[food]'/></td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=updateFood.php method=post>";
echo "<tr>";
echo "<td><input type='text' name='addGroup'></td>";
echo "<td><input type='text' name='addFood'></td>";
echo "<td><input type='text' name='addPH'></td>";
echo "<td><input type='submit' name='add' value='add'/></td>";
echo "</tr>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
</body>
</html>
____________ Update 12/2/13 10:30pm ___________________
Ok so if I create a new php page like the following it will work. However, I have no idea how to combine it into the original above... Can anyone help?
<html>
<head>
</head>
<body>
<?php
// Connect to the database server
$con = mysql_connect("localhost","user","pw");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("db",$con);
$sql2="SELECT id, food_group FROM food_group";
$result = mysql_query($sql2,$con) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$type=$row["food_group"];
$options.= '<option value="'.$row['id'].'">'.$row['food_group'].'</option>';
};?>
<SELECT NAME=Food_group>
<OPTION VALUE=0>Choose</OPTION>
<?php echo $options; ?>
</SELECT>
</body>
</html>
Thank you for all your help!
Jason
Your script is nice but I just want to point the following:
There's no need to concatenate this
"<td>" . "<input type=text name=Food_group value=" . $record['food_group'] . " </td>";
you can type it like this:
echo "<td><input type=text name=Food_group value='$record[food_group]'</td>";
also you missed to close your input tag
echo "<td><input type=text name=Food_group value='$record[food_group]' /></td>";
and another is you need to quote your attribute values , see below
echo "<td><input type='text' name='Food_group' value='$record[food_group]'</td>";
Last thing is that you're open to SQL injection, so you should start learning mysqli and prepared statement
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.