HI I NEED YOUR HELP..
I have this VIEW which show all items according to its company
<?php foreach($item_list as $item2):?>
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="<?php echo $item2->item_id ?>" ></td>
<td><?php echo $item2->item_desc?></td>
<td><?php echo $item2->serial_num ?></td>
<td><input type = "checkbox" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "checkbox" name = "JobStatus[]" value = "job order" ></td>
</tr>
<?php endforeach; ?>
---------------------------------------- html output ---------------------------------------
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="146" ></td>
<td>sample item 01</td>
<td>123</td>
<td><input type = "radio" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "radio" name = "JobStatus[]" value = "job order" ></td>
</tr>
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="147" ></td>
<td>sample item 02</td>
<td>21344</td>
<td><input type = "radio" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "radio" name = "JobStatus[]" value = "job order" ></td>
</tr>
My problem is that I want to insert this to new value (status, item_id, item_desc, serial_num, JobStatus) to quotation table in mysql. If Click I on radio item_id 146 row it will insert all values under item_id 146 likewise to item_id 147 it will insert all item values under 147 item_id
So you want the radio button to be checked according to it's job status?????
If so, try something along these lines ( i don't know how your your database is set up so you will have to alter the following):
<?php foreach($query as $item):?>
<td><input type = "text" name = "item_id" value ="<?php echo $item->item_id ?>" ></td>
<td><?php echo $item->item_desc?></td>
<td><?php echo $item->serial_num ?></td>
<td><input type="radio" name="JobStatus" value="quotation" <?php $item->job_status == 'quotation' ? echo "checked" : null; ?> ></td>
<td><input type="radio" name="JobStatus" value="job order" <?php $item->job_status == 'job order' ? echo "checked" : null; ?> ></td>
</tr>
<?php endforeach; ?>
Enclose the whole loop inside a form tag with action="index.php/controller/method"
In your controller,
public function method() {
$items = $this->input->post('item_id', TRUE);
$job_status = $this->input->post('JobStatus', TRUE);
$stati = $this->input->post('status', TRUE);
foreach($items as $key => $row) {
$this->your_model->insert_new($row, $job_status[$key], $stati[$key]);
}
}
In your_model, write the database query to insert these data wherever you need. Of course you need to change the names - method, controller and your_model to suit your application.
Related
I currently Have this code for inserting into my receipt table,
$query = "insert into receipt(petname,receipttype)
values('$petname','$receipttype')";
in which i obtain the autogenerated id
$receiptid=mysqli_insert_id($db);
and send it to the next page. however when the record is inserted, another is also generated (Two of the same content but another auto-generated ID, is entered into the database)
http://prntscr.com/kpjff6 [example of duplicate fields with unique ids]
if (isset($_POST['cmdedit'])){
$petname = mysqli_real_escape_string($db,$_POST['petname']);
$receipttype = mysqli_real_escape_string($db,$_POST['receipttype']);
$query = "insert into receipt(petname,receipttype)
values('$petname','$receipttype')";
$result = mysqli_query($db,$query);
if (mysqli_query($db,$query)){
$receiptid=mysqli_insert_id($db);
if ($result) {
echo "<font color = 'green' > Receipt sucessfully obtained! Page will auto-redirect to order confirmation page in 5 seconds! </font>";
header( "refresh:5; url=addorderhomepage.php?animalid=".$animalid."&receiptid=".$receiptid);
}else{
echo "<p>Something went wrong! </p>" . mysqli_error($db);
}
}
}
?>
<div class = "topbar">
<h2> Order Receipt </h2>
</div>
<?php
$query = "select *
from catalogue
where animalid= " . $animalid;
$result = mysqli_query($db,$query);
if ($result){
while ($rows = mysqli_fetch_array($result))
{
?>
<form method = "post" action = "" >
<table>
<tr>
<th> Animal ID</th>
<td bgcolor="#FFFFFF"> <input type ="text" name = "txtanimalid" value = "<?php echo $rows[0]; ?>" readonly /> </td>
</tr>
<tr>
<th> Animal Name </th>
<td> <input type ="text" name = "petname" value = "<?php echo $rows[1]; ?>" readonly /> </td>
</tr>
<tr>
<th> Animal Type </th>
<td> <input type ="text" name = "petname" value = "<?php echo $rows[3]; ?>" readonly /> </td>
</tr>
<tr>
<th> Animal Species </th>
<td> <input type ="text" name = "petname" value = "<?php echo $rows[2]; ?>" readonly /> </td>
</tr>
<tr>
<th> Animal Description </th>
<td> <input type ="text" name = "petname" value = "<?php echo $rows[4]; ?>" readonly /> </td>
</tr>
<tr>
<th> Receipt type </th>
<td> <input type ="text" name = "receipttype" value = "printable" readonly /> </td>
</tr>
<tr>
<th> Receipt Date </th>
<td> <input type ="date" name = "orderdate" value="<?php echo date('Y-m-j'); ?>" readonly="readonly" </td>
</tr>
<tr>
<th> <br/><br/> </th>
<td> </td>
</tr>
<tr>
<th> </th>
<td> <input type ="submit" value = "Obtain receipt" name = "cmdedit" /> </td>
</tr>
The duplicate row are probably happen because you are calling 2 time the mysqli_query with the same data, the first time when storing the response in the $result var and the second time inside the condition in the if statement.
// ... code
$result = mysqli_query($db,$query);
- if (mysqli_query($db,$query)) {
+ if ($result) {
$receiptid = mysqli_insert_id($db);
if ($result) {
// .. rest of the code
I'm trying to create a form where you can type in information and choose several options. Based on what is chosen i would like to take id and create record in database.
<form method = "post" action = "http://localhost:81/servisai/knygynas/insert_book.php">
<input type = "submit" value = "Pridėti knygą">
<tbody>
<?php while($row = $resultb->fetch_assoc()) : ?>
<tr>
<td style="display:none"><input type = "text" name = "Autoriaus_id"></td>
<td><input type = "text" name = "Autoriaus_vardas" class="form-control" value = "<?php echo $row['Vardas']; ?>"></td>
<td><input type = "text" name = "Autoriaus_pavarde" class="form-control" value = "<?php echo $row['Pavarde']; ?>"></td>
<td><input type = "checkbox" name = "myCheckbox" ></td>
</tr>
<?php endwhile ?>
</tbody>
</table>
</form>
I'm not sure how to choose which records to send to insert_book.php upon submitting
I think it's supposed to do something with if (isset($_POST['myCheckbox'])) but dont know where to go from here
You can do it by following
<input type = "hidden" name = "Autoriaus_id[]" value="<?php echo $row['id']?>">
foreach($_POST['Autoriaus_id'] as $id){
//do your stuffs here
}
I have a page with with the following php code:
<?php
include ('dbconnect.php');
// get values from the form
function getPosts()
{
$posts = array();
$posts[0] = $_POST['bookingid'];
$posts[1] = $_POST['bookingname'];
$posts[2] = $_POST['cabintype'];
$posts[3] = $_POST['lengthofstay'];
$posts[4] = $_POST['guests_description'];
$posts[5] = $_POST['startdate'];
$posts[6] = $_POST['enddate'];
$posts[7] = $_POST['other_information'];
return $posts;
}
if (isset($_POST['bookingdetails'])) {
$data = getPosts();
$insert_Query = "insert into bookings(Bookingid,Bookingname,Cabin_Type,
lengthofstay,Details_about_guests,Booking_start_date,Booking_end_date,
Other_relavant_information)
values ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]',
'$data[6]','$data[7]')";
$result = mysqli_query($db, $insert_Query);
if ($result){
echo "<p>Total price is \$$total</p>\n";
Booked</p> </font>";
}else{
echo "<p> Something went Wrong! Contact Your Administrator </p>" .
mysqli_error ($db);
}
}
$Luxury_Cabin= 1200;
$Contemporary_Cabin= 1000;
$Original_Cabin= 800;
$Total= "";
if(isset($_POST['bookingdetails']) && $_POST['luxurycabin']=='1')
{
$num=(int)$_POST['lengthofstay'];
if($num>=0)
{
$Total=($Luxury_Cabin*$num);
}
}
if(isset($_POST['bookingdetails']) && $_POST['contemporarycabin']=='2')
{
$num=(int)$_POST['lengthofstay'];
if($num>=0)
{
$Total=($Contemporary_Cabin*$num);
}
}
if(isset($_POST['bookingdetails']) && $_POST['originalcabin']=='4')
{
$num=(int)$_POST['lengthofstay'];
if($num>=0)
{
$Total=($Original_Cabin*$num);
}
}
echo "<p>Total price is \$$total</p>\n";
?>
And the HTML Code is like the following:
<form method = "post" action = "" >
<tr>
<th>Bookingid </th>
<td><input type = "text" name = "bookingid" /> </td>
</tr>
<tr>
<th>Booking Name </th>
<td><input type = "text" name = "bookingname" /> </td>
</tr>
<tr>
<th>Cabin Type </th>
<td><input type = "text" name = "cabintype" /> </td>
</tr>
<tr>
<th>Luxury Cabin</th>
<td><input type="radio" name="luxurycabin" value="1" />Yes</td>
<td><input type="radio" name="luxurycabin" value="0"
checked="checked"/>No</td>
</tr>
<tr>
<th>Contemporary Cabin</th>
<td><input type="radio" name="contemporarycabin" value="2" />Yes</td>
<td><input type="radio" name="contemporarycabin" value="3"
checked="checked"/>No</td>
</tr>
<tr>
<th>Original Cabin</th>
<td><input type="radio" name="originalcabin" value="4" />Yes</td>
<td><input type="radio" name="originalcabin" value="5"
checked="checked"/>No</td>
</tr>
<tr>
<th>Length of Stay </th>
<td><input type = "text" name = "lengthofstay" id="lengthofstay"/> </td>
</tr>
<tr>
<th>Details About Guests </th>
<td><textarea cols = "30" rows = "5" type = "text" name
="guests_description" /></textarea> </td>
</tr>
<tr>
<th>Booking Start Date </th>
<td><input type = "text" name = "startdate" /> </td>
</tr>
<tr>
<th>Booking End Date </th>
<td><input type = "text" name = "enddate" /> </td>
</tr>
<tr>
<th>Other Relavant Information </th>
<td><textarea cols = "30" rows = "5" type = "text" name
="other_information" /></textarea> </td>
</tr>
<tr>
<th> </th>
<td> <input type = "submit" value = "Create Booking!" name =
"bookingdetails" /> </td>
</tr>
</table>
</form>
What occurs here is that when the "bookingdetails" button is pressed all of the data entered into the form are sent to the database. What is also done is a multiplication calculation which uses the radio buttons and the number entered into the lenghtofstay field. The result is then echoed on the page itself using:
<?php echo "<p>Total price is \£$total</p>\n"; ?>
So what I need your assistance with is how do I get the dynamically generated calculation result into my database table with a column called Booking_Price?
Just swap your code around so all the calculation stuff happens before the database insert and then you have your value to insert in $total. Modify your insert query to include it.
I have searched for this and keep getting errors no matter what I try.
The main error I get is:
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, string given
I have created a page showing all records. The user then selects one record to edit and the id is passed on to this page which would show all the fields in a form/table. a few of the fields are readonly so can not be modified.
What I am trying to achieve is to show the results in a table so that they can be edited and then sent to the database and updated. I was thinking of sending it to another file to update and then send them back to view the results page.
<form action = 'modify2.php' method = 'post'>
<table border="1">
<tr style="border-bottom:1px solid #000000;">
<strong>User ID</strong>
<strong>Username</strong>
<strong>Password</strong>
<strong>Email</strong>
<strong>Name</strong>
<strong>Submitted By</strong>
<strong>Memeber Since</strong>
</tr>
<?PHP
//error check to see what results are obtained
echo '<br />sql code = '.$sql.'<br/>'; // this works bot the $result
shows nothing
//die();
//fetch object array and put into a row
//while($row = mysqli_fetch_assoc($result))
while($row = mysqli_fetch_assoc($sql))
{
?>
<tr><td><label>User ID :</label></td>
<td><input type = 'text' name = 'id' id = 'id' value='<?PHP echo $row["id"];?>' readonly/></input><br /></td></tr>
<tr><td><strong>Username</strong></td>
<td><input type = 'text' name = 'username' id = 'username' value='<?PHP echo $row["username"];?>'/></input><br /></td></tr>
<tr><td><label>password :</label></td>
<td><input type = 'text' name = 'password' id = 'password' value='<?PHP echo $row["password"];?>'/></input><br /></td></tr>
<tr><td><label>email :</label></td>
<td><input type = 'text' name = 'email' id = 'email' value='<?PHP echo $row["email"];?>'/></input><br /></td></tr>
<tr><td><label>Real Name :</label></td>
<td><input type = 'text' name = 'Name' id = 'Name' value='<?PHP echo $row["Name"];?>'/></input><br /></td></tr>
<tr><td><label>submitted by :</label></td>
<td><input type = 'text' name = 'submittedby' id = 'submittedby' value='<?PHP echo $row["submittedby"];?>' readonly/></input><br /></td></tr>
<tr><td><label>trn_date :</label></td>
<td><input type = 'text' name = 'trn_date' id = 'trn_date' value='<?PHP echo $row["trn_date"];?>' readonly/></input></td></tr>
<?PHP
}
//close table here
?>
<tr><td colspan='2'><div align='center'><input type = 'submit' value ='Modify Record' name = 'submit'/></td></tr>
</form></div>
</table>
You need to put $result=mysqli_query($con,$sql); first to you can use
// Associative array
$row=mysqli_fetch_assoc($result);
I want to have the same drop down Hardware_ID in Adding new record form WHERE I get the data from another table for my Update form.
This is the example for my Update (Edit) form.
Update Form
And this is my Adding new record form.
Adding new record Form
this is my code for Update Form. Im using the same code like my adding new record but I got an error(The error is in the Update Form).
<?php
//get the data
$Asset_ID = $_GET['Asset_ID'];
$Hardware_ID = $_GET['Hardware_ID'];
$Vendor_ID = $_GET['Vendor_ID'];
$PO_ID = $_GET['PO_ID'];
?>
<form action = 'Update_Asset2_Process.php' method = 'POST'>
<table border = '1' align = 'center' cellspacing='0' cellpadding='10'
bgcolor = "White">
<tr>
<th colspan = '2'>ASSET UPDATE FORM</th>
</tr>
<tr>
<td align='center'>Asset ID :</td>
<td><input type = "varchar" name = "Asset_ID" value = "<?php echo
$Asset_ID; ?>" readonly></td>
</tr>
<tr>
<td align='center'> Hardware ID: </td>
<td><input type = "varchar" name = "Hardware_ID" value = "<?php echo
$Hardware_ID?>">
<?php
$query = "SELECT * FROM hardware2 ORDER BY Hardware_ID";
$result = mysql_query($query);
if(mysql_num_rows($result))
{
while ($id = mysql_fetch_row($result))
{
echo "<option value='" . $id[0] . "'>" . $id[0] . " : " .
$id[1] . " </option>";
}
}
?>
</select>
</tr>
<tr>
<td align='center'>Vendor ID :</td>
<td><input type = "varchar" name = "Vendor_ID" value = "<?php echo
$Vendor_ID; ?>"></td>
</tr>
<tr>
<td align='center'>PO ID :</td>
<td><input type = "varchar" name = "PO_ID" value = "<?php echo $PO_ID; ?
>"></td>
</td>
</tr>
<tr>
<td colspan = '2' align = 'right'>
<input type = 'submit' name = 'submit' value = 'UPDATE'>
</td>
</tr>
Back to Asset
</table>
</form>
thanks for your help, Regards
Wrong select box syntax
<?php
//get the data
$Asset_ID = isset($_GET['Asset_ID']) ? $_GET['Asset_ID'] : 0;
$Hardware_ID = isset($_GET['Hardware_ID']) ? $_GET['Hardware_ID'] : 0;
$Vendor_ID = isset($_GET['Vendor_ID']) ? $_GET['Vendor_ID'] : 0;
$PO_ID = isset($_GET['PO_ID']) ? $_GET['PO_ID'] : 0;
?>
<form action = 'Update_Asset2_Process.php' method = 'POST'>
<table border = '1' align = 'center' cellspacing='0' cellpadding='10' bgcolor = "White">
<tr>
<th colspan = '2'>ASSET UPDATE FORM</th>
</tr>
<tr>
<td align='center'>Asset ID :</td>
<td><input type = "varchar" name = "Asset_ID" value = "<?php echo $Asset_ID; ?>" readonly></td>
</tr>
<tr>
<td align='center'> Hardware ID: </td>
<td><input type = "varchar" name = "Hardware_ID" value = "<?php echo $Hardware_ID?>">
<select id="Your_id" name="Your_name">
<?php
$query = "SELECT * FROM hardware2 ORDER BY Hardware_ID";
$result = mysql_query($query);
if(mysql_num_rows($result))
{
while ($id = mysql_fetch_row($result))
{
echo "<option value='".$id[0]."'>".$id[0]." : ".$id[1]."</option>";
}
}
?>
</select>
</tr>
<tr>
<td align='center'>Vendor ID :</td>
<td><input type = "varchar" name = "Vendor_ID" value = "<?php echo $Vendor_ID; ?>" /></td>
</tr>
<tr>
<td align='center'>PO ID :</td>
<td><input type = "varchar" name = "PO_ID" value = "<?php echo $PO_ID; ?>" /></td>
</td>
</tr>
<tr>
<td colspan = '2' align = 'right'>
<input type = 'submit' name = 'submit' value = 'UPDATE'>
</td>
</tr>
Back to Asset
</table>
</form>
Maybe no need the select box in this form
<?php
//get the data
$Asset_ID = isset($_GET['Asset_ID']) ? $_GET['Asset_ID'] : 0;
$Hardware_ID = isset($_GET['Hardware_ID']) ? $_GET['Hardware_ID'] : 0;
$Vendor_ID = isset($_GET['Vendor_ID']) ? $_GET['Vendor_ID'] : 0;
$PO_ID = isset($_GET['PO_ID']) ? $_GET['PO_ID'] : 0;
?>
<form action = 'Update_Asset2_Process.php' method = 'POST'>
<table border = '1' align = 'center' cellspacing='0' cellpadding='10' bgcolor = "White">
<tr>
<th colspan = '2'>ASSET UPDATE FORM</th>
</tr>
<tr>
<td align='center'>Asset ID :</td>
<td><input type = "varchar" name = "Asset_ID" value = "<?php echo $Asset_ID; ?>" readonly></td>
</tr>
<tr>
<td align='center'> Hardware ID: </td>
<td><input type = "varchar" name = "Hardware_ID" value = "<?php echo $Hardware_ID?>">
</tr>
<tr>
<td align='center'>Vendor ID :</td>
<td><input type = "varchar" name = "Vendor_ID" value = "<?php echo $Vendor_ID; ?>" /></td>
</tr>
<tr>
<td align='center'>PO ID :</td>
<td><input type = "varchar" name = "PO_ID" value = "<?php echo $PO_ID; ?>" /></td>
</td>
</tr>
<tr>
<td colspan = '2' align = 'right'>
<input type = 'submit' name = 'submit' value = 'UPDATE'>
</td>
</tr>
Back to Asset
</table>
</form>