i have html table which output all my database :
Edit(button)
pkg | kodsk | namask | tahun | makmal | catatan | murid | netbook
a | b | c | d | e | f | g | h
After user click edit button above table, user can change all the data in table.
My problem is, only some row can be edited. For example my database have 8 row, only row number 8 and 7 can be edit. Other row if try to change the data, nothing happen. My code is ok without any error, so i don't know where the problem is. Please someone help me,i just learn for fun.
<?php
session_start();
include("connections.php");
?>
<meta http-equiv="refresh" content="10";>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="3" style= "background-color: #84ed86; color: #761a9b; margin: 5 auto;" >
<?php
$result = $connect->query("SELECT * FROM data2017 INNER JOIN pengguna USING (pkg)
WHERE pengguna.username = '$_SESSION[username]'");
echo "<tr>";
echo "<th>pkg</th>";
echo "<th>kodsk</th>";
echo "<th>sek</th>";
echo "<th>tahun</th>";
echo "<th>makmal</th>";
echo "<th>catatan</th>";
echo "<th>murid</th>";
echo "<th>netbook</th>";
echo "</tr>";
while($row = $result->fetch(PDO::FETCH_ASSOC)){
echo "<tr>";
echo "<input type='hidden' name='bil' value='".$row['bil']."' />";
echo "<td><input type='text' name='pkg' value='".$row['pkg']."' /></td>";
echo "<td><input type='text' name='kodsk' value='".$row['kodsk']."' /></td>";
echo "<td><input type='text' name='namask' value='".$row['namask']."' /></td>";
echo "<td><input type='text' name='tahun' value='".$row['tahun']."' /></td>";
echo "<td><input type='text' name='makmal' value='".$row['makmal']."' /></td>";
echo "<td><input type='text' name='catatan' value='".$row['catatan']."' /></td>";
echo "<td><input type='text' name='murid' value='".$row['murid']."' /></td>";
echo "<td><input type='text' name='netbook' value='".$row['netbook']."' /></td>";
echo "</tr>";
}
echo "<input type='submit' name='update' value='UPDATE' />";
?>
</table>
</form>
<?php
if(isset($_POST['update']))
{
$bil = $_POST['bil'];
$pkg = $_POST['pkg'];
$kodsk = $_POST['kodsk'];
$namask = $_POST['namask'];
$tahun = $_POST['tahun'];
$makmal = $_POST['makmal'];
$catatan = $_POST['catatan'];
$murid = $_POST['murid'];
$netbook = $_POST['netbook'];
$sql = "UPDATE `data2017` SET `pkg`=:pkg,`kodsk`=:kodsk,`namask`=:namask,`tahun`=:tahun,`makmal`=:makmal,`catatan`=:catatan,`murid`=:murid,`netbook`=:netbook WHERE `bil`=:bil";
$stmt = $connect->prepare($sql);
$pdoExec = $stmt->execute(array(":pkg"=>$pkg,":kodsk"=>$kodsk,":namask"=>$namask,":tahun"=>$tahun,":makmal"=>$makmal,":catatan"=>$catatan,":murid"=>$murid,":netbook"=>$netbook,":bil"=>$bil));
if($pdoExec)
{
echo 'Data Updated';
}
else
{
echo 'Fail To Update';
}
}
?>
The problem is that you are not uniquely identifying each and every form element. If you have 8 rows, then you have 8 values named pkg, but there can only be one $_POST['pkg'] This is why the last row usually wins and is the only one updated.
You are going to have to add the bil field to every input name, then separate it out later.
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
Source
Try this:
edit 1: I had to edit this when I realized placing $bil at the beginning of the input name would violate the statement I placed above. I moved it to the right side of the name.
edit 2: Modified my method to build the array in html like showdev taught me
<?php
session_start();
include("connections.php");
?>
<meta http-equiv="refresh" content="10";>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="3" style= "background-color: #84ed86; color: #761a9b; margin: 5 auto;" >
<?php
$result = $connect->query("SELECT * FROM data2017 INNER JOIN pengguna USING (pkg)
WHERE pengguna.username = '$_SESSION[username]'");
echo "<tr>";
echo "<th>pkg</th>";
echo "<th>kodsk</th>";
echo "<th>sek</th>";
echo "<th>tahun</th>";
echo "<th>makmal</th>";
echo "<th>catatan</th>";
echo "<th>murid</th>";
echo "<th>netbook</th>";
echo "</tr>";
while($row = $result->fetch(PDO::FETCH_ASSOC)){
$bil = $row['bil'];
echo "<tr>";
echo "<input type='hidden' name='row[$bil][:bil]' value='$bil' />";
echo "<td><input type='text' name='row[$bil][:pkg]' value='".$row['pkg']."' /></td>";
echo "<td><input type='text' name='row[$bil][:kodsk]' value='".$row['kodsk']."' /></td>";
echo "<td><input type='text' name='row[$bil][:namask]' value='".$row['namask']."' /></td>";
echo "<td><input type='text' name='row[$bil][:tahun]' value='".$row['tahun']."' /></td>";
echo "<td><input type='text' name='row[$bil][:makmal]' value='".$row['makmal']."' /></td>";
echo "<td><input type='text' name='row[$bil][:catatan]' value='".$row['catatan']."' /></td>";
echo "<td><input type='text' name='row[$bil][:murid]' value='".$row['murid']."' /></td>";
echo "<td><input type='text' name='row[$bil][:netbook]' value='".$row['netbook']."' /></td>";
echo "</tr>";
}
echo "<input type='submit' name='update' value='UPDATE' />";
?>
</table>
</form>
<?php
if(isset($_POST['update']))
{
$sql = "UPDATE `data2017` SET `pkg`=:pkg,`kodsk`=:kodsk,`namask`=:namask,`tahun`=:tahun,`makmal`=:makmal,`catatan`=:catatan,`murid`=:murid,`netbook`=:netbook WHERE `bil`=:bil";
$stmt = $connect->prepare($sql);
foreach($_POST['row'] as $data)
{
$pdoExec = $stmt->execute($data);
if($pdoExec) { echo 'Data Updated'; } else { echo 'Fail To Update'; }
}
}
?>
The inputs have the same names for every row, so the row values overwrite each other and only the last row is effectively posted.
One method is to structure posted data as an array, including a row identifier to distinguish values for different rows. Ideally, the row identifier is a numeric PRIMARY index.
For reference, see How do I create arrays in a HTML <form>?
Here's an example:
while($row = $result->fetch(PDO::FETCH_ASSOC)){
echo '<tr>
<td>
<input type="text" name="row['.$row['bil'].'][pkg]" value="'.$row['pkg'].'">
</td>
<td>
<input type="text" name="row['.$row['bil'].'][kodsk]" value="'.$row['kodsk'].'">
</td>
</tr>";
}
Then you'll end up with a posted array like this:
Array
(
[row] => Array
(
[1] => Array
(
[pkg] => stuff
[kodsk] => things
)
[2] => Array
(
[pkg] => other
[kodsk] => another
)
)
)
And you can iterate through that array to update the database, something like this:
if (!empty($_POST['row'])) {
$sql = "UPDATE `data2017` SET `pkg`=:pkg, `kodsk`=:kodsk WHERE `bil`=:bil";
$stmt = $connect->prepare($sql);
foreach ($_POST['row'] as $bil => $row) {
$pdoExec = $stmt->execute(
array(":pkg"=>$row['pkg'],
":kodsk"=>$row['kodsk'],
":bil"=>$bil)
);
}
}
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'm displaying a table of suppliers with their products.
$productID = $_POST['simplexID'];
$suppliers = "SELECT * FROM suppliers_products, suppliers WHERE supplier_id = suppliers.id AND our_simp_code = '$productID'";
$supplierProducts = mysqli_query($connect , $suppliers)or die(mysqli_error($connect));
echo "<table>";
echo "<tr><th>Supplier</th><th>Pack Qty</th><th>Cost</th><th>Edit</th></tr>";
while ($row = mysqli_fetch_array($supplierProducts , MYSQLI_ASSOC)) {
$id = $row['id'];
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['pack_qty'];
echo "</td><td>";
echo $row['cost'];
echo "</td><td>";
echo "<a href='editsupplierproduct.php?id=$id' >Edit</a>";
echo "</td></tr>";
}
echo "</table>";
As you see the last data field in the table is an Edit link that brings the user to a page to edit this record.
The thing is in the first line of the table loop I get the id of that row but my problem is that it doesn't know which table to grab the id from as I'm using a join.
How can I overcome this?
EDIT: Here's the edit product page
//Get id of product being edited
$editID= $_GET['id'];
$editProduct = "SELECT * FROM suppliers_products WHERE id = $editID";
$product = mysqli_query($connect , $editProduct)or die("Error retrieving data!");
echo "<div class='editsupproduct'>";
echo "<form method='post' action='updatesupplierproduct.php'>";
while ($row = mysqli_fetch_array($product , MYSQLI_ASSOC)) {
echo "Product Code: <input type='text' name='procode' value='" . $row['sup_product_code'] . "'><br>";
echo "Simplex Code: <input type='text' name='simcode' value='" . $row['our_simp_code'] . "'><br>";
echo "Pack Quantity: <input type='text' name='qty' value='" . $row['pack_qty'] . "'><br>";
echo "Cost: <input type='text' name='cost' value='" . $row['cost'] . "'>";
echo "<input type='hidden' name='proid' value='$editID'>";
}
echo "<input type='submit' value='Update'>";
echo "</form>";
echo "<form method='post' action='deletesupplierproduct.php'>";
echo "<input type='hidden' name='supproduct' value='$editID'>";
echo "<input type='submit' value='Delete'>";
echo "</form>";
echo "</div>";
The 'id' must be unique, or SQL will throw a wobbly.
From your tables it looks like 'id' only exists in 'suppliers' - so it will be that one.
If you want a specific 'id' to be returned, then you can achieve this in the query:
SELECT suppliers_products.supplier_id as id,
suppliers.*
FROM suppliers_products, suppliers
WHERE....
This will specifically return the 'supplier_id' from the 'suppliers_products' table, but aliased 'id'
I have table which is dynamically generated. I am getting all correct data. I want to store html data to database table. So how do I store it? following is the code for html table
foreach($res->result() as $row ){
echo "<tr>";
echo "<td><input type='hidden' style='width:80%;' value='".$row->product_id."'
name='product_id[]'/></td>";
echo "<td><input type='hidden' style='width:80%;' value='".$product_name."'
name='product_name[]'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:40%;'>".$product_name."</td>";
echo "<td><input type='text' style='width:30%;' id='packing' name='packing[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:80%;' readonly=''
value='".$row->quantity."' name='quantity[]'/></td>";
echo "<td><input type='text' name='rate' style='width:80%;' class='rate'
name='rate[]'/></td>";
echo "<td><input type='text' style='width:100%;' class='amount' readonly=''
name='amount[]'/></td>";
echo "</tr>";
}
On form submit I have done this..
$data['cart']=array(
'product_id'=>$this->input->post('product_id'),
'product_name'=>$this->input->post('product_name'),
'packing'=>$this->input->post('packing'),
'quantity'=>$this->input->post('quantity'),
'rate'=>$this->input->post('rate'),
'amount'=>$this->input->post('amount'),
);
print_r($data);
$i=0;
foreach($data['cart'] as $row){
$product_id=$row['product_id'];
$product_name=$row['product_name'];
$packing=$row['packing'];
$quantity=$row['quantity'];
$rate=$row['rate'];
$amount=$row['amount'];
$query = $this->db->query("insert into
phppos_billing_items(product_id,product_name,packing,quantity,rate,amount) values
('$product_id','$product_name','$packing','$quantity','$rate','$amount')");
$i++;
}
But it displaying only last record in table..Anybody has any idea about this?? I want total table records to save in another table.
$product_id =$this->input->post('product_id'),
$product_name =$this->input->post('product_name'),
$packing =$this->input->post('packing'),
$quantity =$this->input->post('quantity'),
$rate =$this->input->post('rate'),
$amount =$this->input->post('amount'),
$total = count ($product_id);
for($i=0;$i<$total;$i++){
$query = $this->db->query("insert into
phppos_billing_items(product_id,product_name,packing,quantity,rate,amount) values
("$product_id[$i]","$product_name[$i]","$packing[$i]","$quantity[$i]","$rate[$i]","$amount[$i]")");
}
But it will gives error when no data found for any data So please validate all fields before this code
The trick here is to use the [] notation in your HTML form elements.
So instead of this:
<input type='text' name='someVar'/>
You have:
<input type='text' name='someVar[]'/>
If you have specific keys you can do:
<input type='text' name='someVar[myKey1]'/>
In your case I would do this (HTML generation):
foreach($res->result() as $row ){
echo "<tr>";
echo "<td><input type='hidden' value='".$row->product_id."' name='product_id[]'/></td>";
echo "<td><input type='hidden' value='".$product_name."' name='product_name[]'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:40%;'>".$product_name."</td>";
echo "<td><input type='text' name='packing[]'/></td>";
echo "<td><input type='text' class='quantity' readonly='' value='".$row->quantity."' name='quantity[]'/></td>";
echo "<td><input type='text' name='rate' class='rate' name='rate[]'/></td>";
echo "<td><input type='text' class='amount' readonly='' name='amount[]'/></td>";
echo "</tr>";
}
Notice the [] after every input name? And this would be the code that responds to the form submission:
foreach ($_GET['product_id'] as $index => $product_id) {
$product_id = $_GET['product_id'][$index];
$product_name = $_GET['product_name'][$index];
$packing = $_GET['packing'][$index];
$quantity = $_GET['quantity'][$index];
$rate = $_GET['rate'][$index];
$amount = $_GET['amount'][$index];
phppos_billing_items(product_id,product_name,packing,quantity,rate,amount) values ('$product_id','$product_name','$packing','$quantity','$rate','$amount')");
}
http://php.net/manual/en/faq.html.php#faq.html.arrays
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 trying to make a quiz.It's working but not giving the right result. On a correct answer for example answer 1 variable rans should be incremented by one but it is incrementing after submitting the 2nd question, that's why the value of the 10th current answer is not including in the total correct answer.
<?php
require_once("global.inc.php");?>
<form name="test" method="post" action="test.php">
<?php
$qid=(!isset($_POST['q_id'])) ? 0 : $_POST['q_id'];
$rans=(!isset($_POST["rans"])) ? 0 : $_POST["rans"];
$totalquestion=(!isset($_POST["totalquestion"])) ?
0 : $_POST["totalquestion"];
echo $rans;
if(isset($_POST["submit"]))
{
echo "<table align='center' style='border:1px solid silver' width='80%'
bgcolor='green'>";
echo "<tr><td>Total Question Attempt</td><td>",$totalquestion,"</td><tr>";
echo "<tr><td>Correct Answer</td><td>",$rans,"</td></tr>";
echo "<tr><td>Wrong Answer</td><td>",$totalquestion-$rans,"</td></tr>";
echo "<tr><td>Correct Answer Percentage</td> <td>",$rans/$totalquestion*100,"%</td></tr>";
echo "<tr><td>Wrong Answer Percenntage</td><td>",($totalquestion-$rans)/$totalquestion*100,"%</td></tr>";
echo "</table><br><br>";
$query="select * from questions,answers
where questions.q_id=answers.q_id";
echo "<table cellpadding='5px' align='center' style='border:1px
solid silver'>";
echo "<tr><th colspan='4' id='heading'>Online Quiz Test
Question</td></tr>";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>",$row['q_id'],"</td><td colspan='2'>",$row['question'],"</td></tr><tr><td></td>";
echo "<td colspan='2'>A. ",$row['opt1'],"</td>";
echo "<td colspan='2'>B. ",$row['opt2'],"</td></tr>";
echo "<tr><td></td><td colspan='2'>C. ",$row['opt3'],"</td>";
echo "<td colspan='1'>D. ",$row['opt4'],"</td></tr>";
echo "<tr><td colspan='4' align='right'
style='color:orange'>Correct option is ",strtoupper($row['correct_ans']),"</td></tr>";
echo "<tr><td colspan='4' align='right'
style='color:orange'><hr></td></tr>";
}
echo "</table>";
echo "<p align='right'><a href='#' onclick='window.print()'>Print</a></p>";
echo "<div style='visibility:hidden;display:none'>";
}
?>
<form name="test" method="post" action="test.php">
<?php
if(!isset($a))
{
$a=0;
//unset($_SESSION['score']);
}
if(isset($_POST['next'])) {
$a=$_POST['a'];
$totalquestion=$_POST['totalquestion'];
if(isset($_POST['rans']))
$rans=$_POST['rans'];
}
$sql1="SELECT * FROM questions,answers
where questions.q_id=answers.q_id limit 1 offset $a";
$result=mysql_query($sql1);
$num = mysql_num_rows($result);
echo "<form method='post' action=''>";
if($result) {
while ($row = mysql_fetch_array($result))
{
$qid = $row["q_id"];
$questions = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$opt4 = $row["opt4"];
$correct = $row["correct_ans"];
echo $rans;
?>
<p >Q.<?php echo $qid ?> <?php echo $questions;?></p>
<input type="radio" value="<?php echo $opt1;?>" name="choice"/><?php echo $opt1;?> <br/>
<input type="radio" value="<?php echo $opt2;?>" name="choice"/><?php echo $opt2;?><br/>
<input type="radio" value="<?php echo $opt3;?>" name="choice"/><?php echo $opt3;?><br/>
<input type="radio" value="<?php echo $opt4;?>" name="choice"/><?php echo $opt4;?><br/>
<input type="hidden" value="$answer" name="rightanswer[$qid]"/>
<?php
$b=$a+1;
$sql2="SELECT * FROM questions where q_id=$qid-1 ";
$result2=mysql_query($sql2);
while ($row2 = mysql_fetch_array($result2)) {
$ans=$row2['correct_ans'];
}
if(isset($_POST['choice'])) {
if($ans==$_POST['choice']){
//echo "<input type='hidden' name='rans' value='".($rans+1). "'>";
$rans=$rans+1;
}
else {
//echo "<input type='hidden' name='rans' value='" . $rans . "'>";
$rans=$rans;
}
}
//$query="select correct_ans from questions where q_id='$qid'";
//$result=mysql_query($query);
//while ($row = mysql_fetch_array($result)) {
//echo $row['correct_ans'];
echo "<input type='hidden' value='$b' name='a'>";
echo "<input type='hidden' value='count' name='count'>";
echo "<input type='hidden' name=qid value='$qid'>";
echo "<input type='hidden' name='totalquestion' value='".$totalquestion+1)."'>";
echo "<input type='hidden' name='rans' value='" . $rans . "'>";
echo "<input type='submit' name='next' value='next'> ";
echo "<input type='submit' name='submit' value='submit'><br><br>";
echo "</form>";
}
}
?>
Okay, your code is a bit of a mess.
You have random tautologies (like the $rans=$rans; which does absolutely nothing. If the answerer clicks "next" you're assigning $totalquestion twice. Definitely take a good, hard look and refactor this page.
But the answer to your question is probably because you're checking to see if they entered in the right answer at the bottom of the code -- after you've presented the results or the next question.
You've utilized the scripting capabilities of PHP without touching on any functions so it will evaluate top to bottom.
I'd move everything around: Move the handler for "next" to the top, underneath your default variable assignments, then put the check for right answer underneath that, then do the presentation of the next question, then the "submit"handler.
I'd break up the different units into functions for readability and reusability, also. For example, make a function to print out the specified question, make another one to validate the user entered in the right answer.