I'm trying to insert date from datepicker but the sql statement is not working imo.
here is the code:
if(isset($_POST['confirm']))
{
$order_finish_time = $_POST['order_finish_time'];
$note = $_POST['note'];
$finish_query = mysqli_query($link, "INSERT INTO orderdetails SET order_finish_time=$order_finish_time AND note=$note WHERE order_id=$order_id");
$order_r = mysqli_fetch_array($finish_query);
if(isset($order_r))
{
$result = mysqli_query($link, "UPDATE orderdetails SET order_status = 'Order_Finished' WHERE order_id=$order_id");
?>
<script>alert('Order confirmed!');</script>
<script>document.location = 'orderdetails.php';</script>
<?php
}
else
{
?>
<script>alert('Something wrong');</script>
<script>document.location = 'orderdetails.php';</script>
<?php
}
}
else part of the code is working and im getting something wrong message. Plese help me out if you can spot error in my code.
The form im using is:
<html>
<head>
</head>
<body>
<form class="form-horizontal" method="post">
<div class="row">
<div class="col-xs-12">
Order will be finished :
<input id="datepicker" class="form-control" name="order_finish_time" required/>
</div>
</div>
<button type="submit" name="confirm" id="confirm" class="btn btn-success btn-lg" style="width: 100%;">
<span class="glyphicon glyphicon-ok-sign"></span>
Confirm
</button>
</form>
<!-- datepicker -->
<script>
$( "#datepicker" ).datepicker({
dateFormat : 'yy-mm-dd'
});
</script>
</body>
</html>
here is the picture of my tables:
enter image description here
Your code seems to be incomplete in many ways. No jQuery and jQuery UI is defined in the head.
Your form is missing the field with the name text.
No database connection is made.
The variable $order_id is not defined in your code.
Use var_dump(mysqli_error_list($link)); after executing statements to see possible errors in your SQL. This is procedural style.
Your query is plain wrong. You cannot insert with a where clause, it is used for select, update and delete.
The correct insert query would be
$finish_query = mysqli_query($link, "INSERT INTO `orderdetails` (`order_finish_time`, `note`) VALUES ('$order_finish_time', '$note')");
Please explain what your program is supposed to do. It does not make sense the way it is written right now. This works, but since I do not know your intentions it may be -logically- wrong.
<?php
if (isset($_POST['confirm'])) {
$order_finish_time = $_POST['order_finish_time'];
$note = $_POST['note'];
$link = mysqli_connect('localhost', 'root', '', 'orderdetails');
$finish_query = mysqli_query($link, "INSERT INTO `orderdetails` (`order_finish_time`, `note`) VALUES ('$order_finish_time', '$note')");
$order_id = mysqli_insert_id($link);
if($finish_query === true)
{
$result = mysqli_query($link, "UPDATE `orderdetails` SET `order_status` = 'Order_Finished' WHERE `id`=$order_id");
?>
<script>alert('Order confirmed!');</script>
<script>document.location = 'orderdetails.php';</script>
<?php
}
else
{
?>
<script>alert('Something wrong');</script>
<script>document.location = 'orderdetails.php';</script>
<?php
}
}
?>
Related
I'm trying to upload image in my code project. In the image field I'm getting
"The Activity image field is required."
The below code is my Activity_controller
public function create_activity(){
//...Upload the image
//... path to upload image
$config['upload_path'] = './assets/images/uploads'; // The path to upload the image
$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf|doc';
$config['max_size'] = '2048';
$config['max_width'] = '500';
$config['max_height'] = '500';
$this->load->library('upload',$config);
if(!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$activity_image = 'noimage.jpg';
} else {
$data = array('upload_data' => $this->upload->data());
$activity_image = $_FILES['userfile']['name'];
}
$this->activity_model->save_activity($activity_image);
redirect('activity_controller');
}
Activity model, insert activity in the database Here is only the function to insert data in the database.
public function save_activity($activity_image){
//... code to insert others data in the database
'activity_image' => $activity_image
);
return $this->db->insert('activity', $data);
}
Below code is create_activities.php file in the view folder:
<?php echo validation_errors(); ?>
<?php echo form_open_multipart('activity_controller/create_activity'); ?>
<div class="form-group">
<label>Upload image</label>
<input name="userfile" type="file">
</div>
I have loaded the model in the autoload.php file.
I also used <input name="activity_image type="file">
"image_name" is the name of the field in the database table
Please someone can help me to solve this problem? I will appreciate your help
Thank you in advance
I think you forgot to add (enctype="multipart/form-data") in your view file in form Tag.
Here, Usen this code. I hope this one is help full to you.
<form action="/action_page_binary.asp" method="post" enctype="multipart/form-data">
First name: <input type="file" name="name"><br>
<input type="submit" value="Submit">
</form>
I've never used code igniter and I'm trying to make a quick admin form that includes an image upload input. My admin form is up and has a route/url that I can reach, but the save function is not working correctly. I'm getting a 404 error when I click my submit button.
I believe the issue is with the line form_open_multipart('dashboard_save/do_upload') in views/admin/Dashboard.php. I don't think the do_upload function is being reached. Any ideas where I'm going wrong?
*new detail: I am able to reach my controller with form_open_multipart('dashboard_save') using an index function... but I'm not able to reach any other function such as form_open_multipart('dashboard_save/upload') using an upload function in controller dashboard_save.
CONTROLLERS
controllers/admin/Dashboard_save.php
<?php
class Dashboard_save extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
// die('got here 2');
$this->load->view('admin/dashboard_view', array('error' => ' ' ));
}
public function do_upload()
{
// die('got here!!');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('dashboard_view', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('dashboard_save', $data);
}
}
}
?>
VIEWS
views/admin/Dashboard.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<!DOCTYPE html>
<body>
<div id="main-wrapper">
<div id="main-container">
<div id="main-body">
<div id='main-form-body'>
<p>Configure front end here.</p>
<div id='admin-form-container'>
<?php echo form_open_multipart('dashboard_save/do_upload');?>
<form id="admin-form" method="" action="">
<div class='form-field'>
<div class='label-wrapper'><label for='main_img'>Main Image</label></div>
<input type = "file" name = "userfile" size = "20" />
</div>
<div class='form-field'>
<button id="submit-search" type="submit" class="button" title="Submit" value = "upload">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
views/admin/Dashboard_save.php
<html>
<head><title>Dashboard Save</title></head>
<body>
<h3>testing dashbord submit</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
Hi first to check your do_upload function has been called or not . if yes then to please check you have loaded in upload library or not . if not so please upload library and than to use below code
<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<!DOCTYPE html>
<body>
<div id="main-wrapper">
<div id="main-container">
<div id="main-body">
<div id='main-form-body'>
<p>Configure front end here.</p>
<div id='admin-form-container'>
<form id="admin-form" method="post" action="<?php echo base_url()?>/dashboard_save/do_upload" enctype="multipart/form-data">
<div class='form-field'>
<div class='label-wrapper'><label for='main_img'>Main Image</label></div>
<input type = "file" name = "userfile" size = "20" />
</div>
<div class='form-field'>
<button id="submit-search" type="submit" class="button" title="Submit" value = "upload">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
You have used two times the form tag. This is why its taking last one and showing error. Remove one FORM tag. Here is your view file code revised
<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<!DOCTYPE html>
<body>
<div id="main-wrapper">
<div id="main-container">
<div id="main-body">
<div id='main-form-body'>
<p>Configure front end here.</p>
<div id='admin-form-container'>
<?php $attributes = array('id' => 'admin-form'); echo form_open_multipart('dashboard_save/do_upload', $attributes);?>
<div class='form-field'>
<div class='label-wrapper'><label for='main_img'>Main Image</label></div>
<input type = "file" name = "userfile" size = "20" />
</div>
<div class='form-field'>
<button id="submit-search" type="submit" class="button" title="Submit" value = "upload">Submit</button>
</div>
<?php echo form_close();?>
</div>
</div>
</div>
</div>
</body>
</html>
No test it. I have removed form open & close tag which you wrote manually, at same time use only form_open_multipart() & form_close() for accomplish same task
How to display related post by categories with next post of current post (don't latest post or random post). I'm use code related post for twentytwelve theme. but now, author in wentytwelve_entry_meta() is repeat.
pls help me :
<div id="related_posts">
<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach((get_the_category()) as $category) {
$id = $category->cat_ID;
}
global $wpdb;
$query = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = $category_ids[0]
AND $wpdb->posts.id < $post->ID
ORDER BY id DESC limit 3
";
$my_query = $wpdb->get_results($query);
if( $my_query) {
echo '<h3>Related Posts</h3><ul>';
foreach($my_query as $key => $post) {
?>
<li>
<div class="entry-header">
<div class="header-l">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
<p class="datetime">
<?php twentytwelve_entry_meta(); ?>
</p>
</div>
<?php the_post_thumbnail(); ?>
</div>
<div class="entry-summary">
<?php
$str_content = wp_trim_words($post->post_content);
$str_content = str_replace('[', '<', $str_content);
$str_content = str_replace(']', '>', $str_content);
echo $str_content;
?>
</div>
</li>
<?php
}
echo '</ul>';
}
}?>
WordPress customary is to use WP_Query class to fetch posts from DB, if you can modify your code to use WP_Query it would be easier.
WP_Query Reference
As you are using Custom Query to load your posts from DB, template tags like the_permalink(), the_title_attribute(), the_title() etc will not work properly that is the reason while theme function twentytwelve_entry_meta() fails.
As per codex reference Displaying Posts Using a Custom Select Query
You should try something like this:
global $wpdb;
$query = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = $category_ids[0]
AND $wpdb->posts.id < $post->ID
ORDER BY id DESC limit 3";
$my_query = $wpdb->get_results($query);
if($my_query) {
global $post;
echo '<h3>Related Posts</h3><ul>';
foreach($my_query as $key => $post) {
//use setup postdata, it works only for variable named $post.
setup_postdata($post);
//you can safely use template tags now.
?>
<li>
<div class="entry-header">
<div class="header-l">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
<p class="datetime">
<?php twentytwelve_entry_meta(); ?>
</p>
</div>
<?php the_post_thumbnail(); ?>
</div>
<div class="entry-summary">
<?php
$str_content = wp_trim_words($post->post_content);
$str_content = str_replace('[', '<', $str_content);
$str_content = str_replace(']', '>', $str_content);
echo $str_content;
?>
</div>
</li>
<?php
}
}
Other interesting post:
What does setup_postdata ($post ) do?
I use Datatables to submit answers of questions from a survey. Each question is in new page, and the next is seen with the "Next" button. I want to have only one submit button and it has to appear in the end of all questions. In my code, it's under the table and is visible all the time. My biggest problem is that this submit button insert in db only the last question, but not all questions.
Please can you help me? :)
My view is:
<html>
<head>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.5/css/jquery.dataTables.min.css" type="text/css" />
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//cdn.datatables.net/1.10.5/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
$('#example').dataTable( {
"pagingType": "simple",
"lengthMenu": [[1], [1]],
"bSort": false,
"language": {
"paginate": {
"previous": true
}
}
} );
} );
</script>
</head>
<body>
<?php
echo form_open('index/survey_fill/' .$survey_id ); ?>
<table id="example" >
<thead>
<tr><th>Question</th></tr>
</thead>
<tbody>
<?php
echo validation_errors();
foreach ($survey as $row)
{
?>
<tr>
<td>
<?php echo "$row->question"; ?><br/>
<?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>
$data=array(
'name' => 'answer['.$row->question_id.']',
'value' => '5'
);
echo form_radio($data);
echo " 5 ";
$data=array(
'name' => 'answer['.$row->question_id.']',
'value' => ' 4'
);
echo form_radio($data);
echo " 4";
</td></tr>
<?php
}
?>
</tbody>
</table>
<?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>
<input type="submit" id="button" name = "submit" value="Submit" class="btn btn-success">
</form>
</div>
</body>
</html>
My model is:
public function survey_fill()
{
$answers=($this->input->post('answer'));
if (null !==($this->input->post('submit'))) {
$date = new DateTime("now");
foreach($answers as $question_id=>$answer)
{
$data = array(
'user_id'=>$this->session->userdata['user_id'],
'question_id'=>$question_id,
'answer'=>$answer,
'created_at'=>$date->format('Y-m-d H:i:s')
);
$this->db->insert('survey_answers', $data);
}
if($this->db->affected_rows() > 0)
{
return true;
}
return false;
}
}
I'm thinking Datatables is quite a lot of overkill for what you're trying to do. Personally, I'd output this in a set of divs and do a simple forward-back type button with jQuery toggle, which is a LOT less overhead. There's also very low-overhead paging solutions out there that could suffice. Remember, datatables is not only reformatting the table, but it has an encyclopedia of API calls that are loaded, plus the data model it holds. That's a LOT for a show/hide type test.
Regardless, the solution is pretty straightforward. Wrap your container (whatever it is, datatable or divs) with a <form> tag. Put the submit button at the end, and show/hide or enable/disable it via whatever means you want. Datatables has an API call that can tell you the current page, and you could key off that to enable your button. Since the button will be before you close the <form> it will automatically submit.
Alternative, you could wrap your container in the form, build a jQuery .on('click') event that serializes the form and submits it via Ajax if that was desired. The key if you go this route is to preventDefault() so the form doesn't auto-submit as is standard for html forms.
I have a list of cities in my database. There is an edit button that brings up a modal with a form where they can change the name of the city and submit. I'm having a hard time getting the city_id to pass from the edit button through the modal and to the controller. I get a "message undefined variable for the city_id on the line that tries to put the city_id in the form open code.
Adding a modal into the mix has made this confusing to me.
View Code
Edit
<!-- Modal for Edit -->
<div class="modal hide" id="editCityDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">x</button>
<h3>Edit City</h3>
</div>
<div class="modal-body">
<?php $this->load->helper('form'); ?>
<?php echo form_open('/cities/edit_city/'. $city_id); ?>
<label class="control-label" for="name">City</label>
<input type="text" name="city_name" id="city_name" value=""/><br />
<input type="submit" class="btn-small btn-primary" value="Edit City" />
<button class="btn-small" data-dismiss="modal">Cancel</button>
<?php echo form_close(); ?>
</div>
<!-- jQuery for Modal -->
<script>
// scripts for modal windows
$(document).on("click", ".edit-modal", function () {
var city_name = $(this).data('name');
var city_id = $(this).data('id');
$(".modal-body #city_name").val( city_name );
$('#editCityDialog').modal('show');
});
</script>
Controller Code
// Edit City
public function edit_city() {
$id = $this->uri->segment(3);
if ($this->input->city('submit')) {
$city_name = $this->input->xss_clean($this->input->city('city_name'));
$this->cities_model->edit_city($city_id, $city_name);
}
}
Model Code
// Edit City
public function edit_city($city_id, $city_name) {
$data = array(
'city_name' => $city_name
);
$this->db->where('city_id', $city_id);
$this->db->update('cities', $data);
}
You can use the city_id you already have and set the action with that:
$(document).on("click", ".edit-modal", function () {
var city_name = $(this).data('name');
var city_id = $(this).data('id');
$(".modal-body #city_name").val( city_name );
//set the forms action to include the city_id
$(".modal-body form").attr('action','/cities/edit_city/'+city_id);
$('#editCityDialog').modal('show');
});
if echoing <?php echo $c['city_id']; ?> works for the edit button, you should do that for the form_open as well like this
<?php echo form_open('/cities/edit_city/'. $c['city_id']); ?>
I would also recommend that if your edit_city() function is only ever called if there is a segment(3), then it is easier to do this:
// Edit City
public function edit_city($id) {
if ($this->input->city('submit')) {
$city_name = $this->input->xss_clean($this->input->city('city_name'));
$this->cities_model->edit_city($city_id, $city_name);
}
}