How do I retrieve data from a SQL table, modify the data and store it in another database table with multiple rows & columns and with single submit button I want insert every rows at a time I don't know how to get that hidden value and work properly with that
<?php
include"connect_database.php";
if(isset($_POST['submit'])) {
$amt = $_POST['total'];
if($amt > 0) {
$qry = "INSERT INTO attendance(rollno, name, year, attendance, reason) VALUES "; // Split the mysql_query
for($i=1; $i<=$amt; $i++) {
$qry .= "('".$_POST["rollno$i"]."', '".$_POST["name$i"]."', '".$_POST["year$i"]."', '".$_POST["attendance$i"]."', '".$_POST["reason$i"]."' ),"; // loop the mysql_query values to avoid more server loding time
}
$qry = substr($qry, 0, strlen($qry)-2);
$insert = mysqli_query($dbcon, $qry); // Execute the mysql_query
}
// Redirect for each cases
if($insert) {
$msg = '<script type="text/javascript">alert("added");</script>';
}
else {
$msg = '<script type="text/javascript">alert("Server Error, Kindly Try Again");</script>';
}
};
if (isset($_POST['select']))
{
$sql = "SELECT * FROM data WHERE year='" . $_POST['yr'] . "'";
$myData = mysqli_query($dbcon, $sql);
$num = mysqli_num_rows($myData);
echo "<table border=1>
<tr>
<th>Rollno</th>
<th>Name</th>
<th>Year</th>
<th>Attendance</th>
<th>reason</th>
</tr>";
for ($i=0; $i <$num; $i++)
{
$record = mysqli_fetch_array($myData);
echo "<form action=smanage.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=rollno$i value=" . $record['rollno'] . " </td>";
echo "<td>" . "<input type=text name=name$i value=" . $record['name'] . " </td>";
echo "<td>" . "<input type=text name=year$i value=" . $record['year'] . " </td>";
echo "<td> "."<select name=attendance$i >
<option value=Present >present</option>
<option value=Absent >Absent</option>
</select>"."</td>";
echo "<td>". "<textarea cols=15 rows=2 name=reason$i placeholder=Enter reason ...></textarea>" . "</td>" ;
echo "<td>" . "<input type=hidden name=total value=" . $i-1 . "</td>";
echo "</tr>";
}
echo"</table>";
echo "<input type=submit name=submit value=save class=Button3>";
echo "</form>";
};
mysqli_close($dbcon);
?>
you are opening multiple forms, for each row in your table on.
This causes your html to be invalid, just start the form before displaying the table.
You could use this html
<table>
<?php
for ($i = 0; $i < $num; $i++) {
$record = mysqli_fetch_array($myData);
?>
<tr>
<td><input type="text" name="rollno[<?= $record['rollno'] ?>]" value="<?= $record['rollno'] ?>" </td>
<td><input type="text" name="name[<?= $record['rollno'] ?>]" value="<?= $record['name']?>" </td>
<td><input type="text" name="year[<?= $record['rollno'] ?>]" value="<?= $record['year'] ?>" </td>
<td><select name="attendance[<?= $record['rollno'] ?>]" >
<option value="Present" >present</option>
<option value="Absent" >Absent</option>
</select></td>
<td><textarea cols="15" rows="2" name="reason[<?= $record['rollno'] ?>]" placeholder="Enter reason ..."></textarea></td>
</tr>
<?php
}
?>
</table>
with this your values will every row will be put into the $_POST-Array, you can access the values via the indexes (I am guessing rollno represents the ID of the dataset).
When you really only want to insert all the values into a table, you can leave the index out. Meaning you could write
<td><input type="text" name="rollno[]" value="<?= $record['rollno'] ?>" </td>
Instead of
<td><input type="text" name="rollno[<?= $record['rollno'] ?>]" value="<?= $record['rollno'] ?>" </td>
You don't need the hidden field, you can just count the items in the array.
$total = count($_POST['rollno']);
<?php
include"connect_database.php";
if(isset($_POST['submit'])) {
$amt = $_POST['total'];
$rollnos= $_POST['rollno'];
if($amt > 0) {
$qry = "INSERT INTO attendance(rollno, name, year, attendance, reason) VALUES "; // Split the mysql_query
$i=0;
foreach($rollnos as $rollno) {
$qry .= "('".$rollno."', '".$_POST["name"][$i]."', '".$_POST["year"][$i]."', '".$_POST["attendance"][$i]."', '".$_POST["reason"][$i]."' ),"; // loop the mysql_query values to avoid more server loding time
$i=$i+1;
}
$qry = substr($qry, 0, strlen($qry)-2);
$insert = mysqli_query($dbcon, $qry); // Execute the mysql_query
}
// Redirect for each cases
if($insert) {
$msg = '<script type="text/javascript">alert("added");</script>';
}
else {
$msg = '<script type="text/javascript">alert("Server Error, Kindly Try Again");</script>';
}
};
if (isset($_POST['select']))
{
$sql = "SELECT * FROM data WHERE year='" . $_POST['yr'] . "'";
$myData = mysqli_query($dbcon, $sql);
$num = mysqli_num_rows($myData);
echo "<table border=1>
<tr>
<th>Rollno</th>
<th>Name</th>
<th>Year</th>
<th>Attendance</th>
<th>reason</th>
</tr>";
for ($i=0; $i <$num; $i++)
{
$record = mysqli_fetch_array($myData);
echo "<form action=smanage.php method=post>";
echo "<tr>";
echo "<td>" . "<input type='text' name='rollno[]' value='" . $record['rollno'] . "'> </td>";
echo "<td>" . "<input type='text' name='name[]' value='" . $record['name'] . "'> </td>";
echo "<td>" . "<input type='text' name='year[]' value='" . $record['year'] . "'> </td>";
echo "<td> "."<select name='attendance[]' >
<option value='Present' >present</option>
<option value='Absent' >Absent</option>
</select>"."</td>";
echo "<td>". "<textarea cols='15' rows='2' name='reason[]' placeholder='Enter reason ...'></textarea>" . "</td>" ;
echo "<td></td>";
echo "</tr>";
}
echo "<input type='hidden' name='total' value='" . $i-1 . "'>";
echo"</table>";
echo "<input type='submit' name='submit' value='save' class='Button3'>";
echo "</form>";
};
mysqli_close($dbcon);
?>
Related
So i guess i don't understand this i am a very newbie to coding in general. I have searched and can't find a good enogh explanation to get it to work in my situatation. I need to fill a table from a fetch command then update each result with an input of information into a new column. Here is the code i have:
This fills the table:
echo "<table border='1'>
<tr>
<th>Envelope</th>
<th>Budget</th>
<th>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount"></td><?php;
echo "</tr>";
}
echo "</table>";
?>
<input type="submit">
And this writes the input for budgetname into the column budgetname:
$paycheckname = mysqli_real_escape_string($con, $_POST['paycheckname']);
$budgetamount = mysqli_real_escape_string($con, $_POST['budgetamount']);
$envelopename = mysqli_real_escape_string($con, $_POST['envelopename']);
}
$sql="UPDATE envelopes SET $paycheckname='$budgetamount' WHERE envelopename ='$envelopename'";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
Right now it just doesn't write anything. I have used echo var_dump($envelopename)."<br>"; and echo var_dump($budgetamount)."<br>"; to try and see what it is doing but $envelopename is always blank. Thanks for any help you can provide.
only <input> , <textarea>, <select> and <button> are submitted to the server in a form
If you want to re-submit some static values create some hidden inputs
while($row = mysqli_fetch_array($result)) {
echo "<input type='hidden' value='$row[envelopename]' name='envelopename'/>";
echo "<input type='hidden' value='$row[envelopebudget]' name='envelopebudget'/>";
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount"></td><?php;
echo "</tr>";
}
echo "</table>";
But your next issue will be that you're creating the elements in a loop so you'll have multiple inputs with the same name
<tr>
<input type='hidden' value='envName1' name='envelopename'/>
<input type='hidden' value='envBudget1' name='envelopename'/>
<td>envName1</td>
<td>envBudget1</td>
<td><input type="text" name="budgetamount"></td>
</tr>
<tr>
<input type='hidden' value='envName2' name='envelopename'/>
<input type='hidden' value='envBudget2' name='envelopename'/>
<td>envName2</td>
<td>envBudget2</td>
<td><input type="text" name="budgetamount"></td>
</tr>
so you must submit as an array
while($row = mysqli_fetch_array($result)) {
echo "<input type='hidden' value='$row[envelopename]' name='envelopename[]'/>";
echo "<input type='hidden' value='$row[envelopebudget]' name='envelopebudget[]'/>";
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount[]"></td><?php;
echo "</tr>";
}
echo "</table>";
and at the server end process as an array
foreach ($_POST['budgetamount'] as $budgetamount){
echo $budgetamount. '<br>';
}
suppose the form rendered looks like this:
<tr>
<input type='hidden' value='envName1' name='envelopename[]'/>
<input type='hidden' value='envBudget1' name='envelopebudget[]'/>
<td>envName1</td>
<td>envBudget1</td>
<td><input type="text" name="budgetamount[]"></td>
</tr>
<tr>
<input type='hidden' value='envName2' name='envelopename[]'/>
<input type='hidden' value='envBudget2' name='envelopebudget[]'/>
<td>envName2</td>
<td>envBudget2</td>
<td><input type="text" name="budgetamount[]"></td>
</tr>
<tr>
<input type='hidden' value='envName3' name='envelopename[]'/>
<input type='hidden' value='envBudget3' name='envelopebudget[]'/>
<td>envName3</td>
<td>envBudget3</td>
<td><input type="text" name="budgetamount[]"></td>
</tr>
when the user hits submit the $_POST that arrives at newpaycheck.php will look like:
$_POST
['envelopename']{
[0]=>'envName1',
[1]=>'envName2',
[2]=>'envName3'
},['envelopebudget']{
[0]=>'envBudget1',
[1]=>'envBudget2',
[2]=>'envBudget3'
},['budgetamount']{
[0]=>'someValueEnteredByUser',
[1]=>'anotherValueEnteredByUser',
[2]=>'yetAnotherValueEnteredByUser'
}
so you can do something like this:
foreach ($_POST['envelopename'] as $envelopename){
$arrayIndex = array_search($envelopename,$_POST['envelopename']);
$envelopebudget = $_POST['envelopebudget'][$arrayIndex];
$budgetamount= $_POST['budgetamount'][$arrayIndex];
$paycheckname = mysqli_real_escape_string($con, $envelopebudget);
$budgetamount = mysqli_real_escape_string($con,$budgetamount);
$envelopename = mysqli_real_escape_string($con,$envelopename);
$sql="UPDATE envelopes SET $paycheckname='$budgetamount' WHERE envelopename ='$envelopename'";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
}
I am trying to filter my results using a series of dropdown boxes which are populated from other tables within the database, except for a primary option which is returned from the form as *
Currently the form submits the data as expected, but no results are returned. I suspect it is because the query is searching for * in the respective columns.
So the question is, can an asterisk be used as a wildcard in a WHERE statement?
And if not, how would I go about this instead?
offending code is included, i understand that using a $variable in a query is bad practice, but at this stage i'm searching for a functional solution, rather than production code.
cheers.
echo " <form method=\"post\" action=\"$self\">
<table>
<tr>
<th>ID</th>
<th>REGISTER</th>
<th>LOCATION</th>
<th>TYPE</th>
<th>CAPACITY</th>
<th>LENGTH</th>
<th>QTY</th>
<th>SERIAL#</th>
<th>CERT#</th>
<th>LAST INSPECTION</th>
<th>BY</th>
<th>DATE IN</th>
<th>DATE OUT</th>
<th>STATUS</th>
<th>NOTES</th>
</tr>";
?>
<!-- START OF FILTER ROW -->
<tr>
<td></td>
<td> <select name="register" id="register">
<option value="*">---</option>
<?php
$sql = "SELECT * FROM valid_registers";
foreach ($dbh->query($sql) as $row)
{
echo "<option value\"" . $row['register'] . "\">" . $row['register'] . "</option>";
}
?>
</select></td>
<td> <select name="location" id="location">
<option value="*">---</option>
<?php
$sql = "SELECT * FROM valid_locations";
foreach ($dbh->query($sql) as $row)
{
echo "<option value\"" . $row['location'] . "\">" . $row['location'] . "</option>";
}
?>
</select></td>
<td> <select name="type" id="type">
<option value="*">---</option>
<?php
$sql = "SELECT * FROM valid_types";
foreach ($dbh->query($sql) as $row)
{
echo "<option value\"" . $row['type'] . "\">" . $row['type'] . "</option>";
}
?>
</select> </td>
<td><input type="radio" name="capacity" id="cap_asc" value="cap_asc">
<
<input type="radio" name="capacity" id="cap_dec" value="cap_dec">
></td>
<td><input type="radio" name="length" id="length_asc" value="length_asc">
<
<input type="radio" name="length" id="length_des" value="length_des">
></td>
<td> </td>
<td> </td>
<td> </td>
<td><input type="radio" name="lastinsp" id="lastinsp_asc" value="lastinsp_asc">
<
<input type="radio" name="lastinsp" id="lastinsp_dec" value="lastinsp_dec">
></td>
<td> </td>
<td><input type="radio" name="datein" id="datein_asc" value="datein_asc">
<
<input type="radio" name="datein" id="datein_dec" value="datein_dec">
></td>
<td><input type="radio" name="dateout" id="dateout_asc" value="dateout_asc">
<
<input type="radio" name="dateout" id="dateout_dec" value="dateout_dec">
></td>
<td> <select name="status" id="status">
<option value="*">---</option>
<?php
$sql = "SELECT * FROM valid_status";
foreach ($dbh->query($sql) as $row)
{
echo "<option value\"" . $row['status'] . "\">" . $row['status'] . "</option>";
}
?>
</select> </td>
<td> </td>
<td><input type="submit" name="submit_filter" id="submit_filter" value="Filter"></td>
</tr>
<!--END OF FILTER ROW -->
<?
//get data from the db
if(isset($_POST['submit_filter'])) {
//fetch filter options
$register = $_POST['register'];
$location = $_POST['location'];
$type = $_POST['type'];
$status = $_POST['status'];
//prepare and execute the query
$sql = "SELECT * FROM register WHERE register=$reigster AND location=$location AND type=$type AND status=$status ";
}
else { $sql = "SELECT * FROM register"; }
foreach ($dbh->query($sql) as $row)
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['register'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['capacity'] . "</td>";
echo "<td>" . $row['length'] . "</td>";
echo "<td>" . $row['qty'] . "</td>";
echo "<td>" . $row['serial'] . "</td>";
echo "<td>" . $row['cert'] . "</td>";
echo "<td>" . $row['lastinsp'] . "</td>";
echo "<td>" . $row['inspby'] . "</td>";
echo "<td>" . $row['datein'] . "</td>";
echo "<td>" . $row['dateout'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td><a href='" . $self . "?edit=" . $row['id'] . "'>Edit</a></td>";
//echo "<td><input type=\"submit\" name=\"edit\" id=\"edit\" value=\"Edit\" /></td>";
echo "<td><a href='" . $self . "?delete=" . $row['id'] . "'>Delete</a></td>";
//echo "<td><input type=\"submit\" name=\"delete\" id=\"delete\" value=\"Delete\" /></td>";
echo "</tr>";
}
echo "</table></form>";
}
No, the * is really only valid in the columns you select, not in the where clause.
The usual way to do this is to catch the * from the form and use that to modify the actual SQL statement by removing that part of the where clause altogether, something like (pseudo-code):
if param['userid'] == '*':
query = 'select name from users'
else:
query = 'select name from users where id = ?'
Although I have seen cases where parameterised queries needed a consistent number of parameters regardless of wild-carding (in some BIRT reports I've looked at where the query is modified dynamically but the parameter count is harder to change), so you would get something like:
if param['userid'] == '*':
query = 'select name from users where (id = ? or 1 = 1)'
else:
query = 'select name from users where id = ?'
That's a bit of a kludge but it's sometimes used to ease the developers workload. I'd rather do it the first way where possible.
To do that for multiple conditions, you can do something like:
joiner = " where "
query = "select something from mytable"
if param['userid'] != '*':
query = query + joiner + "user_id = '" + param['id'] + "'"
joiner = " and "
if param['age'] != '*':
query = query + joiner + "user_age = " + param['age']
joiner = " and "
if param['gender'] != '*':
query = query + joiner + "user_sex = '" + param['gender'] + "'"
joiner = " and "
keeping in mind that, unless you have already sanitised your param[] array values, you risk SQL injection attacks). I leave out the solution for that since it's irrelevant to the question at hand.
The Problem: Trying to calculate sum of a total variable which is inside the table invoicesub in the Form Process (2nd page). Trying to make the sum(total) appear in 3rd page which is display_invoice and also get the customer_name in the form process page(2nd page). somehow $_get["$cust_name"]; is not working and im getting a blank from it. Next, after i can get the sum_total variable how do i parse all these to the 4th page using a form?
Edit:http://imageshack.com/a/img19/8729/btls.png (picture of output)
http://imageshack.com/a/img20/8744/s3ut.png (picture of my database)
Too low rep, i cant post images. I uploaded it to image shack. The 1 on top of the table is from the mysqli_num_rows. This is the first row i entered in the form page. The 2nd row is missing
edit:
New Invoicefinal
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>Services Rendered</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Price($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "<th>Amount</th>\n";
echo "</tr>";
$cname = $_GET["cname"];
global $connection;
$sql1="SELECT description,quantity, amount, discount, total, SUM(total) as sumtotal FROM invoicesub WHERE cust_name='$cname' GROUP BY description ORDER BY id";
$result2 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));
echo $count;
while ($rows = mysqli_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $rows['description'] . "</td>";
echo "<td>" . $rows['quantity'] . "</td>";
echo "<td>" . $rows['amount'] . "</td>";
echo "<td>" . $rows['discount']. "%" . "</td>";
echo "<td>" ."$". $rows['total'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
$sql1="SELECT SUM(total) as total_amt FROM invoicesub WHERE cust_name='$cname'";
$result3 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));
while ($rows = mysqli_fetch_array($result3)){
echo "<tr>";
echo "<td>". "Total Amount:" ."$". $rows['total_amt'] . "</td>";
echo "</tr>";
echo "<form action=\"4thpage.php\" method=\"POST\">";
echo "<input type=\"hidden\" name=\"total_amt\"/>";
echo "Customer Paid:";
echo "<input type=\"text\" name=\"paid\" value=\"\"/>";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit\"/>";
echo "<input type=\"button\" value=\"Cancel\" onclick=\"window.location='manage_content.php';\"/>";
echo "</form>";
}
The Form: It consists of a form that can allow more than 1 row of inputs, which means i will have more than 2 descriptions, quantity, amount and discount. Thus, i put them inside an array
Form Process: This is the 2nd page, it will take in all the arrays parsed in, make a loop to insert all the inputted variables in the form into the table invoicesub.
Display_invoice: This is the 3rd page, i want to show all the customer_name, descriptions, quantity, amount, total_amt that was keyed into invoicesub and then do a sum(total) and show it at the bottom of the page. I also want to insert the customer_name, sum(total) variable into the fourth page through the form at display_invoice. Is this possible?
Form
function addTextArea(){
count= count+1;
var div = document.getElementById('name');
div.innerHTML += "<div> <input type='text' name='name[]' value='' "+"id=name"+count+"> </div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('quantity');
div.innerHTML += "<div><input type='text' name='quantity[]' value ='' "+"id=quantity"+count+"></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('amount');
div.innerHTML += "<div><input type='text' name='amount[]' value ='' "+"id=amount"+count+"></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('discount');
div.innerHTML += "<div><input type='text' name='discount[]' value ='' "+"id=discount"+count+"></div>";
div.innerHTML += "\n<br />";
}
function removeTextArea(){
document.getElementById("name"+count).remove();
document.getElementById("quantity"+count).remove();
document.getElementById("amount"+count).remove();
document.getElementById("discount"+count).remove();
count = count-1;
}
</script>
</head>
<body>
<form action="invoiceprocess.php" method="POST">
<?php
echo "<table border='2'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "</tr>";
echo "<tr>";
echo "<td>"?><input type='text' size="50" name='name[]' value='Examination and Consultation' readonly/><?php "</td>";
echo "<td>"?><input type='text' size="50" name='quantity[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='amount[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='discount[]' value='' /><?php "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"?><div id="name"></div> <?php "</td>";
echo "<td>"?><div id="quantity"></div> <?php "</td>";
echo "<td>"?><div id="amount"></div> <?php "</td>";
echo "<td>"?><div id="discount"></div> <?php "</td>";
echo "</tr>";
?>
Customer Name:
<br />
<input type="text" name="cust_name" value="" />
<br />
<input type="button" value="Add Description" onClick="addTextArea();">
<input type="button" value="Remove Description" onClick="removeTextArea();">
<input type="submit" name="submit" value="submit">
</form>
</body>
invoiceprocess (The 2nd page)
if (isset($_POST['submit'])){ // Process the form
$name_array = $_POST['name'];
$quantity_array = $_POST['quantity'];
$amount_array = $_POST['amount'];
$discount_array = $_POST['discount'];
$cust_name_array = mysql_prep( $_POST['cust_name']);
for ($i = 0; $i < count($name_array); $i++){
$cust_name = $cust_name_array;
$name = $name_array[$i];
$quantity = $quantity_array[$i];
$amount = $amount_array[$i];
$discount = $discount_array[$i];
$total_amt = ($amount - ($amount * ($discount / 100))) * $quantity;
global $connection;
$query = "INSERT INTO invoicesub (";
$query.= " cust_name, description, quantity, amount, discount, total";
$query.= ") VALUES (";
$query.= " '{$cust_name}', '{$name}', {$quantity}, {$amount}, {$discount}, {$total_amt}";
$query.= ")";
$result = mysqli_query($connection, $query);
}
redirect_to("invoicesubmitfinal.php?cname=".urlencode($cust_name));
}
Display_Invoice(invoicesubmitfinal.php)
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>Customer_name</th>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "<th>Total_amt</th>\n";
echo "</tr>";
$cust_name = $_GET["$cust_name"];
global $connection;
$sql1="SELECT SUM(total) FROM invoicesub WHERE cust_name='$cust_name'";
$result2 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));
while ($rows = mysqli_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $rows['quantity'] . "</td>";
echo "<td>" . $rows['amount'] . "</td>";
echo "<td>" . $rows['discount']. "%" . "</td>";
echo "<td>" ."$". $rows['total'] . "</td>";
echo "<td>" . "$" . $total_amt . "</td>";
echo "</tr>";
<form action="insertfinal.php" method="POST">
Customer Paid:
<input type="text" name="paid" value="" />
</form>
I want to take attendance of a particular class and store the values in database.
I have used INNER JOIN to get the data from two tables and used those tables values in a form name attendance.
Now once i take attendance using the form i want to store that values in my database so i created another file name insertattendance.php.
The problem is it shows undefined index variables. ex.undefined index classid ..etc
so i tried using it in *if(isset($_POST['submit'])* There is no erros but the values are not posted.
My doubt is since iam using the values of my old tables is it showing error?.
Tell me how can i do this?
attendance.php
<html>
<head>
<title>grade1</title>
</head>
<body>
<table border="1" cellspacing="1" cellpadding="1" width="200" height="200">
<tr>
<th>classid</th>
<th>studentid</th>
<th>teacherid</th>
<th>locid</th>
<th>date</th>
<th>flag</th>
<th>comments</th>
</tr>
<?php
include 'conn.php';
$query = "(SELECT a.classid, a.fname, b.teacherid, c.locid
FROM class_master c JOIN student_master a
ON c.classid = a.classid JOIN teacher_link b
ON c.classid = b.classid
WHERE c.classid = 'grade1' )";
$result = mysql_query($query);
$i=1;
while( $row = mysql_fetch_array($result))
{
echo "<form action=insertattend.php method=POST>";
echo "<tr>";
echo "<td>" . "<input name=classid[$i] type=text value=" .$row['classid']." </td>";
echo "<td>" . "<input name=fname[$i] type=text value=" .$row['fname']." </td>";
echo "<td>" . "<input name=teacherid[$i] type=number value="
.$row['teacherid']." </td>";
echo "<td>" . "<input type=number name=locid[$i] value=" .$row['locid']." </td>";
echo "<td>" . "<input name=date[$i] type=date value='date'></td>";
echo "<td>" . "<input type=radio id=attend name=attend[$i] value='present'>";?>P
<?php echo "<input type=radio id=attend name=attend[$i] value='absent'>";?>A
<?php
echo"</td>";
echo "<td><input name=comment type=comment[$i] row=3 column=5></td>";
echo "</tr>";
$i++;
}
?>
</table>
<input type="submit" value="submit">
</form>
</body>
</html>
Here is my Insertattendance.php code
<?php
if (isset($_POST['submit'])){
include 'conn.php';
$clnm = mysql_real_escape_string($_POST['classid']);
$stfn = mysql_real_escape_string($_POST['fname']);
$dt = mysql_real_escape_string($_POST['date']);
$fg = mysql_real_escape_string($_POST['attend']);
$tid = mysql_real_escape_string($_POST['teacherid']);
$lid = mysql_real_escape_string($_POST['locid']);
$cmt = mysql_real_escape_string($_POST['comment']);
$inquery =("INSERT INTO attendance(classid, studentid, dateid, flag, teacherid,
locid, comments) VALUES('$clnm', '$stfn', '$dt', '$fg', '$tid', '$lid', '$cmt')");
mysql_query($inquery, $dbconnection);
echo "<br>";
echo "values inserted successfully!!!!";
mysql_close($dbconnection);
};
?>
Fix your html code:
<html>
<head>
<title>grade1</title>
</head>
<body>
<table border="1" cellspacing="1" cellpadding="1" width="200" height="200">
<tr>
<th>classid</th>
<th>studentid</th>
<th>teacherid</th>
<th>locid</th>
<th>date</th>
<th>flag</th>
<th>comments</th>
</tr>
<?php
include 'conn.php';
$query = "(SELECT a.classid, a.fname, b.teacherid, c.locid
FROM class_master c JOIN student_master a
ON c.classid = a.classid JOIN teacher_link b
ON c.classid = b.classid
WHERE c.classid = 'grade1' )";
$result = mysql_query($query);
while( $row = mysql_fetch_array($result))
{
echo "<form action='insertattend.php' method='POST'>";
echo "<tr>";
echo "<td>" . "<input name=classid type=text value=" .$row['classid']." ></td>";
echo "<td>" . "<input name=fname type=text value=" .$row['fname']." ></td>";
echo "<td>" . "<input name=teacherid type=number value=" .$row['teacherid']." ></td>";
echo "<td>" . "<input type=number name=locid value=" .$row['locid']." ></td>";
echo "<td>" . "<input name=date type=date value='date'></td>";
echo "<td>" . "<input type=radio id=attend name=attend value='present'>";?>P
<?php echo "<input type=radio id=attend name=attend value='absent'>";?>A
<?php
echo"</td>";
echo "<td><input name=comment type=comment row=3 column=5></td>";
echo "</tr>";
//echo "</form>";
?>
<input type="submit" name="submit" value="submit">
</form>
<?php } ?>
<!--<form action="insertattend.php">-->
</table>
</body>
</html>
These changes ought to be done on your attendance.php code
First move this echo "<form action=insertattend.php method=POST>"; out of your while
Actually you are closing the <form> tag before the submit button.
echo "</tr>";
echo "</form>"; //<--- Comment or Remove this line
Also, remove this line too (because you already defined above the while loop)
<form action="insertattend.php"> <!-- Remove this line -->
<input type="submit" value="submit">
</form>
Another thing is.. echo "<td><input name=comment type=comment row=3 column=5></td>"; I don't think there is something called type=comment replace that to type=text or use a <textarea>
You missed name="submit" in your attendance.php form.
<input type="submit" name="submit" value="submit">
use form opening and closing tags like this:
<html>
<head>
<title>grade1</title>
</head>
<body>
<table border="1" cellspacing="1" cellpadding="1" width="200" height="200">
<tr>
<th>classid</th>
<th>studentid</th>
<th>teacherid</th>
<th>locid</th>
<th>date</th>
<th>flag</th>
<th>comments</th>
</tr>
<?php
include 'conn.php';
$query = "(SELECT a.classid, a.fname, b.teacherid, c.locid
FROM class_master c JOIN student_master a
ON c.classid = a.classid JOIN teacher_link b
ON c.classid = b.classid
WHERE c.classid = 'grade1' )";
$result = mysql_query($query);
?>
<form action=insertattend.php method=POST>
<?php
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . "<input name='classid[]' type=text value=" . $row['classid'] . " </td>";
echo "<td>" . "<input name='fname[]' type=text value=" . $row['fname'] . " </td>";
echo "<td>" . "<input name='teacherid[]' type=number value=" . $row['teacherid'] . " </td>";
echo "<td>" . "<input type=number name='locid[]' value=" . $row['locid'] . " </td>";
echo "<td>" . "<input name='date[]' type=date value='date'></td>";
echo "<td>" . "<input type=radio id=attend name='attend[]' value='present'>";
?>P
<?php echo "<input type=radio id=attend name='attend[]' value='absent'>"; ?>A
<?php
echo"</td>";
echo "<td><input name='comment[]' type=comment row=3 column=5></td>";
echo "</tr>";
}
?>
</table>
<input type="submit" name= "submit" value="submit">
</form>
</body>
</html>
You have to post the values as array in form fields inside the loop. Then you have to get the array of post values in foreach to insert all the records to the database.
There are two forms.
Input types are in one form and submit button in another one.So avoid that and put together in one form.There is no need for another form with same action value. There are more than one student so write form outside while loop and then array name for input values. Then in php page values in array so use loop to get all values seperately.
<html>
<head>
<title>grade1</title>
</head>
<body>
<form action=insertattend.php method=POST>
<table border="1" cellspacing="1" cellpadding="1" width="200" height="200">
<tr>
<th>classid</th>
<th>studentid</th>
<th>teacherid</th>
<th>locid</th>
<th>date</th>
<th>flag</th>
<th>comments</th>
</tr>
<?php
include 'conn.php';
$query = "(SELECT a.classid, a.fname, b.teacherid, c.locid
FROM class_master c JOIN student_master a
ON c.classid = a.classid JOIN teacher_link b
ON c.classid = b.classid
WHERE c.classid = 'grade1' )";
$result = mysql_query($query);
$i=1;
while( $row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input name=classid[] type=text value=" .$row['classid']." </td>";
echo "<td>" . "<input name=fname[] type=text value=" .$row['fname']." </td>";
echo "<td>" . "<input name=teacherid[] type=number value="
.$row['teacherid']." </td>";
echo "<td>" . "<input type=number name=locid[] value=" .$row['locid']." </td>";
echo "<td>" . "<input name=date[] type=date value='date'></td>";
echo "<td>" . "<input type=radio id=attend name=attend[] value='present'>";?>P
<?php echo "<input type=radio id=attend name=attend[] value='absent'>";?>A
<?php
echo"</td>";
echo "<td><input name=comment[] type=text row=3 column=5></td>";
echo "</tr>";
}
?>
</table>
<input type="submit" value="submit">
</form>
</body>
</html>
And in php page you take size of any of the coming array
For eg:sizeof(classid);using that write for loop to accept values
I'm trying to create a dropdown list with the values being populated from the database. In the following code, I have created a table for viewing the student details and created delete and add buttons to add and delete the information.
The problem is while adding the details for class id, I used a dropdown value from another table to be populate the values, but I was not able to do it.
<html>
<head><title>viewstudent</title>
<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "my_attendance";
$prefix = "";
$con = mysql_connect($host, $user, $pass)
or die ("not connected to db");
mysql_select_db($dbname, $con)
or die("database not selected");
if (isset($_POST['delete'])){
$deleteqry = "DELETE from student_master WHERE ID='$_POST[hidden]'" ;
mysql_query($deleteqry, $con);
};
if (isset($_POST['add'])){
$clid=mysql_real_escape_string($_POST['classid']);
$sid=mysql_real_escape_string($_POST['studentid']);
$fn=mysql_real_escape_string($_POST['fname']);
$ln=mysql_real_escape_string($_POST['lname']);
$dob=mysql_real_escape_string($_POST['dob']);
$em=mysql_real_escape_string($_POST['email']);
$ph=mysql_real_escape_string($_POST['phone']);
$loc=mysql_real_escape_string($_POST['locationid']);
$emer=mysql_real_escape_string($_POST['emergency']);
$addquery = ("INSERT INTO student_master(classid, studentid, fname, lname, dob, phone, email, locationid, emergency)
VALUES('$clid', '$sid', '$fn', '$ln', '$dob', '$ph', '$em', '$loc', '$emer')");
mysql_query($addquery, $con);
};
$query=("SELECT * FROM student_master");
$result = mysql_query($query);
?>
</head>
<body>
<table align="center" width="500" border="1" cellspacing="1" cellpadding="1">
<tr>
<th>Id</th><th>classid</th><th>studentid</th><th>fname</th><th>lname</th><th>dob</th><th>phone</th><th>email</th><th>locationid</th><th>emergency</th>
</tr>
<?php
while( $row = mysql_fetch_array($result,MYSQLI_ASSOC))
{
echo "<form action=viewstudentinfo.php method=POST >";
echo "<tr align='center'>";
echo "<td>" . "<input type=text name=ids value=" .$row['id']." </td>";
echo "<td>" . "<input type=text name=classid value=" .$row['classid']." </td>";
echo "<td>" . "<input type=number name=studentid value=" .$row['studentid']." </td>";
echo "<td>" . "<input type=text name=fname value=" .$row['fname']." </td>";
echo "<td>" . "<input type=text name=lname value=" .$row['lname']." </td>";
echo "<td>" . "<input type=text name=dob value=" .$row['dob']." </td>";
echo "<td>" . "<input type=text name=phone value=" .$row['phone']." </td>";
echo "<td>" . "<input type=text name=email value=" .$row['email']." </td>";
echo "<td>" . "<input type=text name=locationid value=" .$row['locationid']." </td>";
echo "<td>" . "<input type=text name=emergency value=" .$row['emergency']." </td>";
echo "<td>" . "<input type=hidden name=hidden value=" .$row['id']." </td>";
echo "<td>" . "<input type=submit name=delete value=Delete" ." </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=viewstudentinfo.php method= post>";
echo "<tr>";
echo "<td><input type=text name=id></td>";
echo "<td><select type=text name=classid></td>";?>
// populating classid values from another table....
<?php
$query = 'SELECT classid FROM class_master';
$result1 = mysql_query($query, $con) or die(mysql_error($con));
while ($row = mysql_fetch_array($result1))
{
echo '<option value="' . $row["classid"] . '"> ' . $row["classid"] . '</option>';
}
echo "</select>";
?>
<?php
echo "<td><input type=number name=studentid></td>";
echo "<td><input type=text name=fname></td>";
echo "<td><input type=text name=lname></td>";
echo "<td><input type=date name=dob></td>";
echo "<td><input type=number name=phone></td>";
echo "<td><input type=email name=email></td>";
echo "<td><input type=number name=locationid></td>";
echo "<td><input type=number name=emergency></td>";
echo "<td>" . "<input type=submit name=add value=ADD" ." </td>";
echo "</tr>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
You are closing the <td> inside the select:
Replace
echo "<td><select type=text name=classid></td>";?>
With
echo "<td><select type=text name=classid>";?>
And close the '</td>' after the '</select>'
Find out <td><select type=text name=classid></td> in your code and remove the ending tag </td> from it. After all the <option> ... </option> close the </td>