Post data not being sent by a code igniter form - php

I've got a slight problem with my CI2.0.2 application.
From what I can tell, it is not getting any post data from a form and so is failing to run an update function, though most pages work fine.
The form is as follows:
<?php echo form_open('job/update/', array('id' => 'update', 'name' => 'update')); ?>
<div class="images">
<?php echo img('application/images/updateJob.png');?>
</div>
<h1>Update Job</h1>
<br><br><br><br>
<div class="fieldset">
<?php
if (isset($error))
{
echo '<div id ="error">';
echo '<h2>Error:</h2>';
echo '<p>'.$error.'</p>';
echo '</div>';
}
?>
<input type="hidden" name="c" id='c' value="<?php if(!empty($view[0])) { echo $view[0]; } else { echo "0"; } ?>">
<div class="left">
<h4>Job Details:</h4>
<div>
<label for="job_number">Job No:</label>
<input type="text" name="job_number" id="job_number" value="<?php echo $job['jobno']; ?>" disabled="disabled">
</div>
<div>
<label for="date">Date:</label>
<input type="text" name="date" id="date" value="<?php echo $job['datetime']->format('d-M-Y'); ?>">
</div>
<div>
<label for="council">Council:</label>
<input type="text" name="council" id="council" value="<?php echo htmlspecialchars($job['council']); ?>"> *
</div>
<div>
<label for="dano">DA No:</label>
<input type="text" name="dano" id="dano" value="<?php echo htmlspecialchars($job['dano']); ?>">
</div>
<div>
<label for="client">Client:</label>
<input type="text" name="client" id="client" value="<?php echo $job['clientid']; ?>" disabled="disabled">
</div>
</div>
<div class="right" style="margin-left: 390px;">
<h4>Location:</h4>
<label for="street_no">Street No:</label>
<input type="text" name="street_no" id="street_no" value="<?php echo $address['streetno']; ?>"> *
<br>
<label for="street_name">Street Name:</label>
<input type="text" name="street_name" id="street_name" value="<?php echo $address['streetname']; ?>"> *
<br>
<label for="street_type">Street Type:</label>
<select name="street_type" id="street_type">
<?php
foreach ($street_types as $type)
{
echo '<option';
if ($type == $address['streettype']) echo ' selected="selected"';
echo '>'.$type.'</option>';
}
?>
</select> *
<br>
<label for="suburb">Suburb:</label>
<input type="text" name="suburb" id="suburb" value="<?php echo $address['suburb']; ?>"> *
<br>
<label for="postcode">Postcode:</label>
<input type="text" name="postcode" id="postcode" value="<?php echo $address['postcode']; ?>"> *
<br>
<label for="state">State:</label>
<input type="text" name="state" id="state" value="<?php echo $address['state']; ?>"> *
<br>
<label for="country">Country:</label>
<input type="text" name="country" id="country" value="<?php echo $address['country']; ?>">
<br>
<label for="dp">DP:</label>
<input type="text" name="dp" id="dp" value="<?php echo $address['dp']; ?>">
<br>
<label for="lot_no">Lot No:</label>
<input type="text" name="lot_no" id="lot_no" value="<?php echo $address['lotno']; ?>">
<br>
<label for="po_box">PO Box:</label>
<input type="text" name="po_box" id="po_box" value="<?php echo $address['pobox']; ?>">
</div>
<div>
<input type="submit" id="submit" name="submit" value="Submit" class="button">
<p>* = Required fields</p>
</div>
</div>
<?php echo form_close();?>
And the update section of the controller is as follows:
/**
* Update an existing job.
* #param int $job_number The number of the job to update.
*/
public function update($job_number=0)
{
$this->load->model('Job_model');
$job = array();
$address = array();
// Get post data.
$job['joblocation'] = '';
$job['jobno'] = $this->input->post('job_number');
$job['datetime'] = new DateTime($this->input->post('date'));
$job['dano'] = $this->input->post('dano');
$job['council'] = $this->input->post('council');
echo $job['jobno'];
echo $job['dano'];
echo $job['council'];
$address['streetno'] = $this->input->post('street_no');
$address['streetname'] = $this->input->post('street_name');
$address['suburb'] = $this->input->post('suburb');
$address['country'] = $this->input->post('country');
$address['postcode'] = $this->input->post('postcode');
$address['state'] = $this->input->post('state');
$address['dp'] = $this->input->post('dp');
$address['lotno'] = $this->input->post('lot_no');
$address['pobox'] = $this->input->post('po_box');
$address['streettype'] = $this->input->post('street_type');
echo "here2";
if (isset($_POST['submit']))
{
$this->Job_model->update($job);
echo "here";
redirect('job/');
}
// Otherwise, get the data from the database.
else
{
$job = $this->Job_model->search($job_number);
$job = $job[0];
$job['datetime'] = new DateTime($job['datetime']);
$address = $this->Job_model->get_address($job['joblocation']);
$address = $address[0];
}
// Get the street types.
$street_types = array();
$this->load->model('staff_model');
$streets = $this->staff_model->get_street_types();
foreach ($streets as $street)
{
$street_types[$street['streettype']] = $street['streettype'];
}
// Load the client list.
$clients = array();
$this->load->model('client_model');
$people = $this->client_model->get_client_person_list();
$companies = $this->client_model->get_client_company_list();
// Allocate view data.
$viewdata = array();
$viewdata['job'] = $job;
$viewdata['street_types'] = $street_types;
$viewdata['address'] = $address;
$viewdata['people'] = $people;
$viewdata['companies'] = $companies;
$this->layout->view('job/update', $viewdata);
}
Any ideas why this might be happening?
Thanks in advance,
James

Try changing the quotes you're using on this line:
<input type="hidden" name="c" id='c' value="<?php if(!empty($view[0])) { echo $view[0]; } else { echo '0'; } ?>" />
The way you had it with the double quotes around the 0 you were breaking that php because the first one would be closing the opening value quote.

Related

How do I display the record before deleting that record?

I wish to display the record to the user that I am deleting in PHP?
I am trying to display the record in a form that I want to delete but only the input type = text values are being displayed and the radio button values are not being displayed?
<?php
require('/home/s3022041/sqlC/dbConnect.php');
if(isset($_POST['search']))
{
$search = mysqli_real_escape_string($connection, $_POST['id']);
$id = $_POST['id'];
$stp1 = preg_replace("/[^a-zA-Z0-9]/", "", $id); //grab only the alphanumerics
$stp2 = strtoupper($stp1); //Make all alphabets uppercase
$stp3 = preg_replace('/\d+/', '',$stp2); //extract the alphabets part
$newsearchid = str_replace($stp3,"-".$stp3."-",$stp2); //put hyphens before and after the alphabet part
$query = "SELECT * FROM cars WHERE Registration_Number = '$newsearchid' ";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run))
{
?>
<form action="delete_process.php" method="POST" class="form">
<h2>Car Registration Form</h2>
<div class="mb-3">
<input type="radio" name="Car" id="Toyota" value=" <?php echo $row['make']; ?>" />
<label for="Toyota" class="form-label">Toyota</label>
<select name="Models[Toyota]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="Camri">Camri</option>
<option value="Corolla">Corolla</option>
<option value="Estima">Estima</option>
</select><br>
<input type="radio" name="Car" id="Nissan" value=" <?php echo $row['make']; ?>"/>
<label for="Nissan" class="form-label">Nissan</label>
<select name="Models[Nissan]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="Micra">Micra</option>
<option value="roller">roller</option>
<option value="fushe">fushe</option>
</select><br>
<input type="radio" name="Car" id="Mercedez" value=" <?php echo $row['make']; ?>"/>
<label for="Mercedez" class="form-label">Mercedez</label>
<select name="Models[Mercedez]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="5series">5series</option>
<option value="x7">x7</option>
<option value="x5">x5</option>
</select><br>
<label for="vehicleidentificationnumber" class="form-label">vehicle identification
number(VIN)</label>
<input type="text" disabled="disabled" name="vehicleidentificationnumber" class="form-control" maxlength=20 value=" <?php echo $row['VIN']; ?>" required >
<br>
<label for="ManufacturingYear" class="form-label">Manufacturing Year</label>
<input type="text" name="Manufacture_Year" class="form-control" maxlength=20 value=" <?php echo $row['Manufacture_Year']; ?>" required >
<label for="Enginesize" class="form-label">Engine size</label>
<input type="text" name="Engine_Size" class="form-control" maxlength=20 value=" <?php echo $row['Engine_Size']; ?>" required>
<label for="TransmissionType" class="form-label">TransmissionType</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="Automatic" class="form-label">Automatic</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="Manual" class="form-label">Manual</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="SemiAutomatic" class="form-label">Semi-Automatic</label><br>
<label for="NoofSeats" class="form-label">No. of Seats</label>
<input type="number" name="NoOfSeats" class="form-control" value="<?php echo $row['NoOfSeats']; ?>" maxlength=20 required>
<label for="Noofdoors" class="form-label">No. of doors</label>
<input type="number" name="NoOfDoors" class="form-control" maxlength=20 value="<?php echo $row['NoOfDoors']; ?>" required>
<label for="Fueltype" class="form-label">Fuel type</label>
<input type="text" name="Fuel_Type" class="form-control" maxlength=20 value=" <?php echo $row['Fuel_Type']; ?>" required>
<label for="Colour" class="form-label">Colour</label>
<input type="text" name="Colour" class="form-control" maxlength=20 value=" <?php echo $row['Colour']; ?>" required>
<label for="RegistrationNumber" class="form-label">Registration Number (use Dublin
registration) </label>
<input type="hidden" disabled="disabled" name="Registration_Number" class="form-control" maxlength=20 value=" <?php echo $row['Registration_Number']; ?>" required>
<label for="Dateoffirstregistration" class="form-label">Date of first registration</label>
<input type="date" name="DateOfRegestration" class="form-control" maxlength=20 value=" <?php echo $row['DateOfRegestration']; ?>" required>
</div>
<button type="submit" name="search" class="btn btn-primary">Delete</button>
</form>
<?php
}
}
else{
echo "<h1> No records found </h1>";
echo "<a href='index.php'>home</a>";
}
?>
</div>
<div class="modal-footer">
</form>
Here I am trying to delete the above record that is displayed but it says deleted successfully but when I go and check the records it is not deleted it's still there?
<?php
require('/home/s3022041/sqlC/dbConnect.php');
if(isset($_POST['search']))
{
$search = mysqli_real_escape_string($connection, $_POST['search']);
$id = $_POST['Registration_Number'];
$query = "DELETE FROM `cars` WHERE Registration_Number='$id' ";
$query_run = mysqli_query($connection, $query) or die ("not done");
if($query_run)
{
echo "<h1> deleted successfully</h1>";
echo "<a href='index.php'>home</a>";
}
else
{
echo "<h1> not deleted </h1>";
echo "<a href='index.php'>home</a>";
echo 'Error! ' . mysqli_error($connection);
}
}
Change
1. Form
<input type="radio" name="Car" id="Toyota" value="<?=$row['make']; ?>" />
to
<input type="radio" name="Car" id="Toyota" <?=($row['make'] == 'Toyota' ? 'value="'.$row['make'].'" checked':NULL); ?> />
<!-- or better yet -->
<input type="radio" name="Car" id="Toyota" value="Toyota" <?=($row['make'] == 'Toyota' ? 'checked':NULL); ?> />
2. Database
File: delete_process.php
// Change
if($query_run) // << This only tell the script the sql statement run with no errors
// To
if(mysql_affected_rows()<=1) // << This confirms that one or more rows where changed by the sql statement
Explained
PHP Shorthand If/Else Using Ternary Operators (?:)
/* basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
Reason for error
If a radio button are not being displayed, it most-likely an 'Undefined variable' when getting $row['make'].
Other thing, remove the white spacing from. As this may play a part when using a MYSQL Statement.
<!-- From -->
value=" <?php echo $row['Registration_Number']; ?>"
<!-- To -->
value="<?=$row['Registration_Number']; ?>"
*Shorthand one line echo/print <?=

undeclared php variable in contact form

I'm having problems with a simple contact form.
The error I'm getting is:
Notice: Undefined index: terms in contact.php on line 29
Attention! You have to check the Privacy Policy box to accept our terms.
Code:
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$terms = $_POST['terms']; // <---- line 29
if(trim($terms) == '') {
echo '<div class="alert error"><div class="msg">Attention! You have to check
the Privacy Policy box to accept our terms.</div></div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="alert error"><div class="msg">Attention! Please enter your
message.</div></div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
The template file is below:
<form action="<?php echo $this->config->get('config_url').'ajax/contact.php' ?>" method="post" id="contact_form" enctype="multipart/form-data">
<table>
<tr>
<td>
<label for="name"><?php echo $entry_name; ?></label>
<input type="text" name="name" id="name" style="margin-right:20px" value="<?php echo $name; ?>" />
<?php if ($error_name) { ?>
<span class="error"><?php echo $error_name; ?></span>
<?php } ?>
</td>
<td>
<label for="email"><?php echo $entry_email; ?></label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
<?php if ($error_email) { ?>
<span class="error"><?php echo $error_email; ?></span>
<?php } ?>
</td>
</tr>
</table>
<label for="enquiry"><?php echo $entry_enquiry; ?></label>
<textarea name="enquiry" id="enquiry" cols="30" rows="5"><?php echo $enquiry; ?></textarea>
<?php if ($error_enquiry) { ?>
<span class="error"><?php echo $error_enquiry; ?></span>
<?php } ?>
<input type="checkbox" name="terms" value="<?php echo $terms; ?>" />
<label for="terms">Tick this box to confirm you comply with our Privacy Terms</label>
<input type="submit" id="submit" value="<?php echo $button_continue; ?>" class="button dark-bt" />
</form>
Try this:
$terms = isset($_POST['terms']) ? $_POST['terms'] : '';
You will get the checkbox field only if it is checked by the user in the form.
So, always check like this for checkbox and radio buttons.

Why is the value of id getting randomized?

My code is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SomuFinance - Personal Finance Manager</title>
<link rel="stylesheet" type="text/css" href="indexStyle.css">
<script src="scripts/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="scripts/jquery.validate.min.js"></script>
<style type="text/css">
#addItemContainer {
background-color: rgba(204,207,232,1);
}
</style>
<script type="text/javascript">
var flag=0;
</script>
</head>
<body>
<form id="list" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="container">
<input type="submit" class="button" name="edit" id="edit" value="Edit" />
<input type="button" class="button" name="delete" value="Delete" />
<input type="hidden" id="action" name="action">
<table id="listDB">
<tr>
<th>Select</th>
<th>ID</th>
<th>Category ID</th>
<th>Shop</th>
<th>Item</th>
<th>Quantity</th>
<th>Unit</th>
<th>Price Based On</th>
<th>MRP</th>
<th>Seller's Price</th>
<th>Last Updated On</th>
</tr>
<?php
$dbc = mysqli_connect('localhost','root','atlantis2016','itemDB')
or die("Error Connecting to Database");
if(isset($_POST['confirmDelete']))
{
if($_POST['action']=='confirmDelete')
{
foreach ($_POST['selected'] as $delete_id)
{
$query = "DELETE FROM grocery WHERE id = $delete_id";
mysqli_query($dbc, $query)
or die('Error querying database.');
}
}
}
$query1 = "SELECT DISTINCT category FROM grocery";
$result1 = mysqli_query($dbc, $query1)
or die("Error Querying Database");
while($row = mysqli_fetch_array($result1))
{
$category = $row['category'];
$query2 = "SELECT * FROM grocery WHERE category='$category' ORDER BY item ASC";
$result2 = mysqli_query($dbc, $query2)
or die("Error Querying Database");
echo '<tr>';
echo '<td class="catHead" colspan=11>'.$category.'</td>';
echo '</tr>';
$catCount=1;
while($inRow = mysqli_fetch_array($result2))
{
$id = $inRow['id'];
$shop = $inRow['shop'];
$item = $inRow['item'];
$qnty = $inRow['quantity'];
$unit = $inRow['unit'];
$price_based_on = $inRow['price_based_on'];
$mrp = $inRow['MRP'];
$sellers_price = $inRow['sellers_price'];
$last_updated_on = $inRow['last_updated_on'];
echo '<tr>';
echo '<td><input type="checkbox" id="selected" value="' . $id . '" name="selected[]" /></td>';
echo '<td>'.$id.'</td>';
echo '<td>'.$catCount.'</td>';
echo '<td>'.$shop.'</td>';
echo '<td class="leftAligned">'.$item.'</td>';
echo '<td>'.$qnty.'</td>';
echo '<td>'.$unit.'</td>';
echo '<td>'.$price_based_on.'</td>';
echo '<td class="pri">₹'.$mrp.'</td>';
echo '<td class="pri">₹'.$sellers_price.'</td>';
echo '<td>'.$last_updated_on.'</td>';
echo '</tr>';
$catCount++;
}
}
?>
</table>
</div>
<div class="dialogBG">
<div id="deleteConfirmDialog" class="dialog">
<div class="closeDialog"></div>
<p>Sure you want to delete the selected Data?</p>
<input type="submit" id="confirmDelete" class="dialogButton" name="confirmDelete" value="Delete" />
<input type="button" id="cancelDelete" class="dialogButton cancelButton" name="cancelDelete" value="Cancel" />
</div>
</div>
</form>
<div class="dialogBG">
<div id="addItemContainer" class="dialog">
<div class="closeDialog"></div>
<h1>Edit Item</h1>
<form id="data" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php
if(isset($_POST['action']))
{
if($_POST['action']=='edit')
{
echo '<script>flag=1;</script>';
foreach ($_POST['selected'] as $edit_id)
{
$query = "SELECT * FROM grocery WHERE id = $edit_id";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
break;
}
$inRow = mysqli_fetch_array($result);
$id = $inRow['id'];
$shop = $inRow['shop'];
$category = $inRow['category'];
$item = $inRow['item'];
$qnty = $inRow['quantity'];
$unit = $inRow['unit'];
$price_based_on = $inRow['price_based_on'];
$mrp = $inRow['MRP'];
$sellers_price = $inRow['sellers_price'];
$last_updated_on = $inRow['last_updated_on'];
}
}
?>
<div class="leftAligned">
<div class="inp">
<label for="shop">ID : </label>
<input type="text" id="id" name="id" value="<?php echo $id; ?>" required disabled>
</div> <br>
<div class="inp">
<label for="shop">Shop : </label>
<input type="text" id="shop" name="shop" value="<?php echo $shop; ?>" required>
</div> <br>
<div class="inp">
<label for="category">Category : </label>
<input type="text" id="category" name="category" value="<?php echo $category; ?>" required>
</div> <br>
<div class="inp">
<label for="item">Item : </label>
<input type="text" id="item" name="item" value="<?php echo $item; ?>" required>
</div> <br>
<div class="inp">
<label for="qnty">Quantity : </label>
<input type="text" id="qnty" name="qnty" value="<?php echo $qnty; ?>" required>
</div> <br>
<div class="inp">
<label for="unit">Unit : </label>
<input type="text" id="unit" name="unit" value="<?php echo $unit; ?>" required>
</div> <br>
<div class="inp">
<label for="price_based_on">Price based on : </label>
<select name="price_based_on" id="price_based_on">
<option value="kilos">Kilos</option>
<option value="packet">Packet</option>
<option value="bottle">Bottle</option>
<option value="box">Box</option>
<option value="piece">Piece</option>
</select>
</div> <br>
<div class="inp">
<label for="mrp">MRP (₹) : </label>
<input type="text" id="mrp" name="mrp" value="<?php echo $mrp; ?>" required>
</div> <br>
<div class="inp">
<label for="sellers_price">Seller's Price (₹) : </label>
<input type="text" id="sellers_price" value="<?php echo $sellers_price; ?>" name="sellers_price" required>
</div> <br>
<div class="inp">
<label for="last_updated_on">Last Updated on : </label>
<input type="date" id="last_updated_on" name="last_updated_on" value="<?php echo $last_updated_on; ?>" required>
</div>
</div>
<div class="inp">
<input id="insertButton" type="submit" name="submit" value="Insert">
</div>
<div id="message">
<?php
if(isset($_POST['submit']))
{
echo "<script> alert('$id'); </script>";
$shop = $_POST['shop'];
$category = $_POST['category'];
$item = $_POST['item'];
$qnty = $_POST['qnty'];
$unit = $_POST['unit'];
$price_based_on = $_POST['price_based_on'];
$mrp = $_POST['mrp'];
$sellers_price = $_POST['sellers_price'];
$last_updated_on = $_POST['last_updated_on'];
$result=null;
$query = "UPDATE grocery SET shop='$shop', category='$category', item='$item', quantity='$qnty', unit='$unit', price_based_on='$price_based_on', mrp='$mrp', sellers_price='$sellers_price', last_updated_on='$last_updated_on' WHERE id='$id'";
if(!empty($shop)&&!empty($category)&&!empty($item)&&is_numeric($qnty)&&!empty($unit)&&is_numeric($mrp)&&is_numeric($sellers_price)&&!empty($last_updated_on))
{
$result = mysqli_query($dbc, $query)
or die(mysqli_error($dbc));
}
if($result)
{
echo '<span class="success">Item Edited Successfully!</span>';
}
else
{
echo '<span class="failure">Failed to insert Item.</span>';
}
//header("Refresh:2");
}
?>
<script>
$(document).ready(function(){
$( "#data" ).validate({
rules: {
qnty: {
number: true
},
mrp: {
number: true
},
sellers_price: {
number: true
}
},
messages: {
qnty : {
number: '<br> <span class="failure err">Enter a valid quantity</span>'
},
mrp : {
number: '<br> <span class="failure err">Enter a valid MRP</span>'
},
sellers_price : {
number: '<br> <span class="failure err">Enter a valid Price</span>'
},
}
});
});
</script>
</div>
</form>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('.button').click(function(event){
if($(this).val()=="Delete")
{
$("#deleteConfirmDialog").show(200).parent(".dialogBG").fadeIn(200);
$("#action").val('confirmDelete');
}
else if($(this).val()=="Edit")
{
event.preventDefault();
$("#action").val('edit');
$("#list").submit();
}
});
if(flag===1)
{
console.log("This shouldn't be there if the page reloads!");
$("#addItemContainer").show(200).parent(".dialogBG").fadeIn(200);
}
$('#confirmDelete').click(function(){
$(".closeDialog").trigger("click");
});
$('#cancelDelete').click(function(){
$("input:checkbox[name='selected[]']").prop('checked', false);
});
$(".closeDialog").click(function (e){
$(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
});
$(".cancelButton").click(function (e){
$(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
});
});
</script>
<?php
mysqli_close($dbc);
?>
</body>
</html>
Notice that I obtain the id of the first selected element by using the lines:
foreach ($_POST['selected'] as $edit_id)
{
$query = "SELECT * FROM grocery WHERE id = $edit_id";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
break;
}
$inRow = mysqli_fetch_array($result);
$id = $inRow['id'];
Now, I use the id(which is exactly the same as edit_id) thus obtained to access the record in the DB, and show it in the popup.
<div class="leftAligned">
<div class="inp">
<label for="shop">ID : </label>
<input type="text" id="id" name="id" value="<?php echo $id; ?>" required disabled>
</div> <br>
<div class="inp">
<label for="shop">Shop : </label>
<input type="text" id="shop" name="shop" value="<?php echo $shop; ?>" required>
</div> <br>
<div class="inp">
<label for="category">Category : </label>
<input type="text" id="category" name="category" value="<?php echo $category; ?>" required>
</div> <br>
<div class="inp">
<label for="item">Item : </label>
<input type="text" id="item" name="item" value="<?php echo $item; ?>" required>
</div> <br>
<div class="inp">
<label for="qnty">Quantity : </label>
<input type="text" id="qnty" name="qnty" value="<?php echo $qnty; ?>" required>
</div> <br>
<div class="inp">
<label for="unit">Unit : </label>
<input type="text" id="unit" name="unit" value="<?php echo $unit; ?>" required>
</div> <br>
<div class="inp">
<label for="price_based_on">Price based on : </label>
<select name="price_based_on" id="price_based_on">
<option value="kilos">Kilos</option>
<option value="packet">Packet</option>
<option value="bottle">Bottle</option>
<option value="box">Box</option>
<option value="piece">Piece</option>
</select>
</div> <br>
<div class="inp">
<label for="mrp">MRP (₹) : </label>
<input type="text" id="mrp" name="mrp" value="<?php echo $mrp; ?>" required>
</div> <br>
<div class="inp">
<label for="sellers_price">Seller's Price (₹) : </label>
<input type="text" id="sellers_price" value="<?php echo $sellers_price; ?>" name="sellers_price" required>
</div> <br>
<div class="inp">
<label for="last_updated_on">Last Updated on : </label>
<input type="date" id="last_updated_on" name="last_updated_on" value="<?php echo $last_updated_on; ?>" required>
</div>
</div>
The ID and all the relevant details thus far are exactly as they should be.
However, further down, when I try to use the ID to update the record, using the statement
UPDATE grocery SET shop='$shop', category='$category', item='$item',
quantity='$qnty', unit='$unit', price_based_on='$price_based_on',
mrp='$mrp', sellers_price='$sellers_price',
last_updated_on='$last_updated_on'
WHERE id='$id'
somehow the id has changed to the last value of id in the table. So, instead of updating the table's correct record with the updated values, since the id "magically" changes at this point (after the sumbit button has been pressed), the last value of the id appears. I want the id to stay the same as the original id.
Why is this happening and how can I solve this?

view data from codeigniter join table

I want to view data from database.there are three table to join.in two table have same field name. when i try to view data as follows.but there was an error called
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$project_name1
Filename: views/boq_doc.php
Line Number: 12
Controller
class Project_list extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('project_list_model');
}
function show_project_id() {
$id = $this->uri->segment(3);
$data['projects'] = $this->project_list_model->show_projects();
$data['single_project'] = $this->project_list_model->show_project_id($id);
$this->load->view('boq_doc', $data);
}
}
Model
class Project_list_model extends CI_Model {
// Function To Fetch All Students Record
function show_projects(){
$this->db->select("project.project_name AS project_name1 , project.id AS id, client.firstname AS firstname1, client.lastname AS lastname1,staff.firstname AS firstname2, staff.lastname AS lastname2, project.location,project.category, project.start_date, project.end_date");
$this->db->from('project');
$this->db->join('client', 'project.client_id = client.client_id');
$this->db->join('staff', 'staff.id = project.staff_id');
$query = $this->db->get();
return $query->result();
}
// Function To Fetch Selected Student Record
function show_project_id($data){
$this->db->select('*');
$this->db->from('project');
$this->db->where('id', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
}
}
View
<?php foreach ($projects as $project): ?>
<li><?php echo $project->project_name1; ?></li>
<?php endforeach; ?>
</ol>
</div>
<div id="detail">
<!-- Fetching All Details of Selected Student From Database And Showing In a Form -->
<?php foreach ($single_project as $project): ?>
<form method="post" action="<?php echo base_url() . "index.php/update_ctrl/update_project_id1"?>">
<label id="hide">Id :</label>
<?php echo $project->project_name1; ?>
<label>Name :</label>
<input type="text" name="dname" value="<?php echo $project->location; ?>">
<label>Email :</label>
<input type="text" name="demail" value="<?php echo $project->start_date; ?>">
<label>Mobile :</label>
<input type="text" name="dmobile" value="<?php echo $project->end_date; ?>">
<label>Address :</label>
<input type="text" name="dmobile" value="<?php // echo $project->firstname1; ?>">
<input type="text" name="daddress" value="<?php // echo $project->project_address; ?>">
<input type="submit" id="submit" name="dsubmit" value="Update">
</form>
<?php endforeach; ?>
</div>
Your foreach loop syntax seems wrong try this instead
<?
foreach ($projects as $project)
{
?>
<li><?php echo $project->project_name1; ?></li>
<?
}
?>
</ol>
</div>
<div id="detail">
<!-- Fetching All Details of Selected Student From Database And Showing In a Form -->
<?php foreach ($single_project as $project){ ?>
<form method="post" action="<?php echo base_url() . "index.php/update_ctrl/update_project_id1"?>">
<label id="hide">Id :</label>
<?php echo $project->project_name1; ?>
<label>Name :</label>
<input type="text" name="dname" value="<?php echo $project->location; ?>">
<label>Email :</label>
<input type="text" name="demail" value="<?php echo $project->start_date; ?>">
<label>Mobile :</label>
<input type="text" name="dmobile" value="<?php echo $project->end_date; ?>">
<label>Address :</label>
<input type="text" name="dmobile" value="<?php // echo $project->firstname1; ?>">
<input type="text" name="daddress" value="<?php // echo $project->project_address; ?>">
<input type="submit" id="submit" name="dsubmit" value="Update">
</form>
<?
}
?>
</div>

Filling html forms with mysql data using php coming up null

I am trying to fill a html form with data being received out of my mysql database. However I cannot set the forms to display on-load the variables being extracted from the database. I would like the form on-load to hold the data last entered into the forms which have been added to the database previously.
$query = "SELECT FROM character_tbl WHERE character_player
='".$_SESSION["user"]."' character_tbl";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$name = $row['character_name'];
$race = $row['character_race'];
$class = $row['character_class'];
$alignment = $row['character_alignment'];
$hp = $row['character_hp'];
$str = $row['character_str'];
$dex = $row['character_dex'];
$con = $row['character_con'];
$int = $row['character_int'];
$wis = $row['character_wis'];
$cha = $row['character_cha'];
$ac = $row['character_ac'];
$touch = $row['character_touch'];
$flat = $row['character_flat'];
$fort = $row['character_fort'];
$ref = $row['character_ref'];
$will = $row['character_will'];
}
echo $will;
mysql_close();
?>
<!DOCTYPE html>
<html>
<body>
<div id="nav">
<form action="user.php">
<input type="submit" value="Back">
</form>
</div>
<div id="section">
<form action="update.php" method="POST">
Character Name:<br>
<input type="text" name="name" value="<?php echo $name;?>">
<br>
Race<br>
<input type="text" name="race" value="<?php echo $race;?>">
<br>
Class<br>
<input type="text" name="class" value="<?php echo $class;?>">
<br>
Alignment<br>
<input type="text" name="alignment" value="<?php echo $alignment;?>">
<br>
HP<br>
<input type="text" name="hp" value="<?php echo $hp;?>">
<br>
STR<br>
<input type="number" name="str" value="<?php echo $str;?>">
<br>
DEX<br>
<input type="number" name="dex" value="<?php echo $dex;?>">
<br>
CON<br>
<input type="text" name="con" value="<?php echo $con;?>">
<br>
INT<br>
<input type="text" name="int" value="<?php echo $int;?>">
<br>
WIS<br>
<input type="text" name="wis" value="<?php echo $wis;?>">
<br>
CHA<br>
<input type="text" name="cha" value="<?php echo $cha;?>">
<br>
AC<br>
<input type="text" name="ac" value="<?php echo $ac;?>">
<br>
Touch AC<br>
<input type="text" name="touch" value="<?php echo $touch;?>">
<br>
Flat-Footed AC<br>
<input type="text" name="flat" value="<?php echo $flat;?>">
<br>
Fortitude<br>
<input type="text" name="fort" value="<?php echo $fort;?>">
<br>
Reflex<br>
<input type="text" name="ref" value="<?php echo $ref;?>">
<br>
Will<br>
<input type="text" name="will" value="<?php echo $will;?>">
</br>
<input type="submit" value="Update">
</form>
I think the SQL has error:
SELECT FROM character_tbl WHERE character_player
try:
SELECT * FROM character_tbl WHERE character_player
You have syntax error in your mysql query. You have not place field or columns name or (*) for all columns to extract.
Try like this..
$query = "SELECT * FROM character_tbl WHERE character_player ='".$_SESSION['user']."'";

Categories