I have my car models in a edit form which users can edit and delete a car.
When user clicks on edit they could see the car, model, year, price on the edit form and make new changes and submit. I want the uploaded image to show up and edit as well. I know form_input() wont bring the image to show. I tried everything but it wont display in my edit form. Here is my code, thanks!
controller:
public function input($id = 0) {
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('model_users');
/*
if($this->input->post('submit')) {
$this->model_users->entry_insert();
}
$data = $this->books_model->general();
*/
if((int)$id > 0) {
$query = $this->model_users->get($id);
$data['fid']['value'] = $query['id'];
$data['fcar_make']['value'] = $query['car_make'];
$data['fcar_model']['value'] = $query['car_model'];
$data['fcar_year']['value'] = $query['car_year'];
$data['fcar_lease']['value'] = $query['car_lease'];
$data['fcar_payment']['value'] = $query['car_payment'];
$data['fimgpath']['value'] = $query['imgpath'];
}
if($this->session->userdata('is_logged_in')) {
$this->load->view('edit_car',$data);
}
else {
redirect('main/restricted');
}
}
Model:
function get($id) {
$this->db->where(array('id'=>$id));
$query = $this->db->get('cars');
return $query->row_array();
}
view
<tr>
<td>Car Lease:</td>
<td><input type="text" class="textbox" tabindex="6" name = "<?php echo form_input($fcar_lease); ?></td>
<td> </td>
</tr>
<tr>
<td>Car Payment:</td>
<td><input type="text" class="textbox" tabindex="6" name = "<?php echo form_input($fcar_payment); ?></td>
<td> </td>
</tr>
<tr>
<td>Car Image:</td>
<td><input type="file" name=""<?php echo form_input($fimgpath); ?>" size="20" /></td>
<td> </td>
</tr>
You can't show image or image path in file upload field, instead of that just show the image underneath the file upload field (like you display it in the front-end) and if user re uploads the image, delete existing one, upload new one and update the image name in the DB.
Additionally you can give a delete button to delete the image on the same interface (may underneath the current image).
Related
So I have a PHP CRUD system which adds, edits and deletes entries from/to a database. The CMS in question is a drugs table. I can add drugs to this table using the add button and filling in a form, I can also delete the drugs by simply deleting them. But the one of issue is the edit/update part of the system.
The edit feature itself works fine, I'm able to edit an entry and it will post to the database and show up in the table because the user gets redirected back to the table page which shows all the drugs, however, when I remove the text from a field, leaving it empty, and press update, I get an error message which says a text field is empty, however, I also notice the id at the top of the page is no longer there which gives me an Undefined index:error.
I've narrowed the problem down to it being simply because the input type "submit" removes it for some reason.
URL before pressing update with a text field empty:
php_files/DrugEdit.php?Drug_ID=23
URL after pressing update with a text field empty:
eMAR/php_files/DrugEdit.php
How the program works (with code):
Php variable created by getting the Drug_ID from the URL (in this case 23)
$Drug_ID = $_GET['Drug_ID'];
Then gets the field data from the database and assigns them to variables
$result = mysqli_query($conn, "SELECT * FROM drugs WHERE Drug_ID=$Drug_ID");
while($res = mysqli_fetch_array($result))
{
$Drug_Name = $res['Drug_Name'];
$Allergies = $res['Allergies'];
$Side_effects = $res['Side_effects'];
$Type_of_Medication = $res['Type_of_Medication'];
$Dosage = $res['Dosage'];
}
?>
The data is then shown in a form where I can edit the data
<form name="form1" method="post" action="DrugEdit.php">
<table border="0">
<tr>
<td>Drug_Name <?php echo $drug_name_error ?></td>
<td><input type="text" Name="Drug_Name" value="<?php echo $Drug_Name;?>"></td>
</tr>
<tr>
<td>Allergies</td>
<td><input type="text" Name="Allergies" value="<?php echo $Allergies;?>"></td>
</tr>
<tr>
<td>Side_effects</td>
<td><input type="text" Name="Side_effects" value="<?php echo $Side_effects;?>"></td>
</tr>
<tr>
<td>Type_of_Medication</td>
<td><input type="text" Name="Type_of_Medication" value="<?php echo $Type_of_Medication;?>"></td>
</tr>
<tr>
<td>Dosage</td>
<td><input type="text" Name="Dosage" value="<?php echo $Dosage;?>"></td>
</tr>
<tr>
<td><input type="hidden" Name="Drug_ID" value=<?php echo $_GET['Drug_ID'];?>></td>
<td><input type="submit" Name="update" value="Update"> </td>
</tr>
</table>
</form>
If the update button gets pressed, it runs the block of code below:
$drug_name_error = " ";
if(isset($_POST['update']))
{
$Drug_ID = $_POST['Drug_ID'];
$Drug_Name=$_POST['Drug_Name'];
$Allergies=$_POST['Allergies'];
$Side_effects=$_POST['Side_effects'];
$Type_of_Medication=$_POST['Type_of_Medication'];
$Dosage=$_POST['Dosage'];
// checking empty fields
if(empty($Drug_Name) || empty($Allergies) || empty($Side_effects) || empty($Type_of_Medication) || empty($Dosage)) {
if(empty($Drug_Name)) {
$drug_name_error = "<font color='red'>Drug_Name field is empty.</font><br/>";
}
if(empty($Allergies)) {
echo "<font color='red'>Allergies field is empty.</font><br/>";
}
if(empty($Side_effects)) {
echo "<font color='red'>Side_effects field is empty.</font><br/>";
}
if(empty($Type_of_Medication)) {
echo "<font color='red'>Type_of_Medication field is empty.</font><br/>";
}
if(empty($Dosage)) {
echo "<font color='red'>Dosage field is empty.</font><br/>";
}
} else {
//updating the table
$result = mysqli_query($conn, "UPDATE drugs SET Drug_Name='$Drug_Name' ,Allergies='$Allergies' ,Side_effects='$Side_effects' ,Type_of_Medication='$Type_of_Medication',Dosage='$Dosage' WHERE Drug_ID=$Drug_ID");
//redirecting to the display page. In our case, it is index.php
header("Location: ../drug_manager.php");
}
}
Thank you for taking the time to read my problem. Hopefully I've provided enough information for you.
input type "submit" removes it for some reason
Because you're submitting the form, and the URL for the form submit action is defined in the <form> element:
<form name="form1" method="post" action="DrugEdit.php">
Note that there's no Drug_ID value on that URL. However, in the form processing code you're not looking for it in the URL, you're looking for it in the form post:
$Drug_ID = $_POST['Drug_ID'];
So I'm not really sure where you're getting that error. But if somewhere in your form logic you're also looking for $_GET['Drug_ID'], or simply want it to be on the URL for some downstream need, then you can just add it to that URL:
<form name="form1" method="post" action="DrugEdit.php?Drug_ID=<?php echo $_GET['Drug_ID'];?>">
I have a language file application_lang.php in application/language/english directory with content as follows:
$lang['action_delete'] = 'Delete';
$lang['menu_account'] = 'Account';
In codeigniter view I have
<form>
<div>
<table>
<tr>
<td>
<h3>English:</h3>
</td>
<?php
$this->load->helper('language');
$lang_array=$this->lang->load('application','english',true);
if (! empty($lang_array)) {
foreach ($lang_array as $key => $values)
{?>
<tr>
<td> <input type="text" value="<?php echo htmlentities($values);?>"> </td></tr>
<?php
}
}?>
</tr>
</table>
<input type="submit"value="Save">
</div>
</form>
Right now the text boxes will display the values from the application_lang ( Delete and Account in this case). I need to make some changes through this textboxes and click on save button. And this changes should overwrite the existing application_lang.php with new text from the textbox.
eg: $lang['action_delete'] = 'Delete'; should be overwritten as $lang['action_delete'] = 'Deleted file';
How can I do this in codeigniter?
Any help ?
Im trying to make a select which only shows some information (a conditional select).
I mean, this is my table "usuarios":
"usuarios" includes:(id,username,name,lastname,password,type,status,date)
Take a look:
My goal is to make a select that only shows the users with "type" = 1 AND "status" = 1
Take a look:
I have a done a select but it shows every usernames, it does not validates anything :/.
My program shows this, and it is not correct because it does not validate anything :/
Here is my code:
My controller file:
public function edit($ID){
$data['usuarios'] = $this->Crudmodel->get_usuarios();
$data['record']=$this->Crudmodel->get_id_row($ID);
$this->load->view('edit',$data);
}
My model file:
public function get_usuarios() {
$this->db->select('c.*')
->from('usuarios c');
$q = $this->db->get();
return $q->result();
}
My "edit" file:
<h2 align="center">UPDATE</h2>
<form method="post" action='<?php echo site_url('Home/saveupdate'); ?>'>
<tr>
<td><input type="text" hidden name="txtid" value="<?php echo $record->id ?>"/></td>
</tr>
<tr>
<td>
<select name="txtcarr">
<?php foreach($usuarios as $item):?>
<option value="<?php echo $item->id;?>"><?php echo $item->username;?></option>
<?php endforeach;?>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Save"/></td>
</tr>
Dont know whatto do :/
Why not only select the rows you want? Will it work as you expect if you put this in your model?
public function get_usuarios() {
$this->db->select('id, username')
->from('usuarios')
->where(array('status'=>1, 'type'=>1));
$q = $this->db->get();
return $q->result();
}
I'm having some difficulty with php forms.
I have created a page called 'post_details.php' (this simply displays a photo of a product & description). Each product has a unique id
Within posts_details.php, I have used to include command to include a form. This form allows users to send me feedback regarding the product.
For some reason the form is not workin. Everytime the submit button is clicked, the alert box warns me that I need to complete the form (even if the form is complete)
The last part of line one doesn't seem to work. It's not picking up the post_id
Can anyone please help ??
post a comment
<form method="post" action="post_details.php?post= <?php echo $post_id; ?>">
<table width "600">
<tr>
<td>Your email:</td>
<td><input type="text" name="comment_email"/></td>
</tr>
<tr>
<td>Your Comment:</td>
<td><textarea name="comment" cols="35" rows="16"/></textarea></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="postcom"/></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['comment'] )) {
$comment_email = $POST['comment_email'];
$comment = $POST['comment'];
if( $comment_email=='' OR $comment=='') {
echo "<script>alert('Please complete form')</script>";
echo "<script>window.open('post_details.php?post=post_id')</script>";
exit();
}
else {
echo "complete";
}
}
?>
You have error here
if(isset($_POST['comment'] )) {
$comment_email = $POST['comment_email'];
^
$comment = $POST['comment'];
^
....
Instead of $POST it must be $_POST['comment_email'] and $_POST['comment']
I'm having trouble with this form. I want the user to be able to edit an item in the datebase. They chose which item they want to edit on one page and get sent with a GET to the editing page. The GET has the id of the item they need to edit.
The editing page loads with the details of the item inserted into the user form (apart from the name of the file) this field is left blank. I am trying to do some logic that checks if the user has chosen a file.
If they haven't then this field should be ignored as I will presume the user is happy with the file that is already uploaded.
If they chose a file then this means they want this to be the new picture. Only then do I want to run the logic to upload the picture and insert its name into the database.
I'm getting my POST details by saying:
$clean_pic = $_POST['pic'];
I am then saying if it's blank do nothing otherwise run the upload:
if($clean_pic = ''){}
else{
It's not working. Any ideas how I should find out if its blank? Cut down code:
if (isset($_POST['add']))
{
// validate 'pic': must consist of alphanumeric characters only.
$_POST['pic'] = isset($_POST['pic']) ? $_POST['pic'] : '';
//if(preg_match('/\.(jpg|gif|jpeg)$/i',$_POST['pic']))
//{
$clean_pic = $_POST['pic'];
//}
//else
//{$error++; $errmsg .= 'Invalid pic. ';}
}
if (isset($_POST['add']) && ($error==0))
{
if (!isset($_POST['pic'])) { echo"test1";}
else {echo"test222";}
/*tied this too but it didnt work (it will always display result 1):
if (!isset($_POST['pic'])) { echo"test1";}
if (isset($_POST['pic'])) {echo"test222";}*/
}
else //output error messages
{
/////////render form
?>
<form enctype="multipart/form-data" action="" method="post" id="save"><fieldset>
<table id="site-form">
<tr>
<td class="one_of_three"><label>Item Name: </label></td>
<td class="two_of_three"><input type="text" name="fileName" id="fileName" value="<?php echo"$db_name";?>"/></td>
<td><label class="errors" id="fileNameError"> </label></td>
</tr>
<tr>
<td class="one_of_three"><label>Picture: </label></td>
<td class="two_of_three"><input type="file" name="userfile[]" id="pic"/></td>
<td><label class="errors" id="picError"> </label></td>
</tr>
<tr>
<td class="one_of_three"> </td>
<td class="two_of_three"><input name="add" id="save_button" type="submit" value="Update"/> Cancel.</td>
<td> </td>
</tr>
</table>
</fieldset></form>
<?php }?>
What about if (!isset($_POST['pic']))?
You cant access the file using 'id' attribute ('pic'). You have to use the value of 'name' attribute ('userfile'). You can check whether the file is uploaded or not using $_FILES array in php.
if(isset($_FILES) && isset($_FILES['userfile']) && (trim($_FILES['userfile']['name']) != '') ) {
//Your upload code here
}
else
{
//No file uploaded
}
You need to check to things :
one is $_POST['pic'] is an array
second one is in the form you have to use enctype="multipart/form-data" attribute.