Update multiple rows with same id using foreach loop - mysql

I echo 2 input-fields (2 db columns) foreach row in the database (where id=$id) with each row's data being inside the input field. Admin-user can change data inside input-field and click update.
Currently, when clicking the update-button, it does update the rows (where id=$id) but it updates each row with the input-data of the first input field instead of updating input-data foreach input field. Thank you in advance for help.
view:
<form action='<?= site_url("admin/do_edit_page"); ?>' method='POST'>
<?php foreach($link_data as $row) : ?>
Link:<br />
<input type='text' name='page_link_title' value='<?= $row->link_title; ?>'>
<input type='text' name='page_link_sub_title' value='<?= $row->link; ?>'><br />
<?php endforeach; ?>
<input type='submit' name='update_site' value='Update'>
</form>
controller:
public function do_edit_page(){
$id = $this->input->post('page_id', TRUE);
$this->content_model->update_links($id);
}
model:
public function update_links($id){
foreach($_POST as $update_rows){
$update_rows = array(
array(
'page_id' => $id,
'link_title' => $this->input->post('page_link_title', TRUE),
'link' => $this->input->post('page_link_sub_title', TRUE)
)
);
$this->db->update_batch('content_links', $update_rows, 'page_id');
}
}

you have to move your form tag between the foreach loop so that it can submit the data of single row. something like this
<?php foreach($link_data as $row) : ?>
<form action='<?= site_url("admin/do_edit_page"); ?>' method='POST'>
Link:<br />
<input type='text' name='page_link_title' value='<?= $row->link_title; ?>'>
<input type='text' name='page_link_sub_title' value='<?= $row->link; ?>'><br />
<input type='submit' name='update_site' value='Update'>
</form>
<?php endforeach; ?>

Related

Wordpress. Insert form values to custom table

How can i on click on submit send values to my custom mysql table??
Form html:
<form action="/ekz.php" method="post">
<input type="text" name="jjkk1">
<input type="text" name="jjkk2">
<input type="text" name="jjkk3">
<input type="text" name="jjkk4">
<input type="submit" name="submit">
</form>
Table name is wp_ekz2020:
Try doing like this :
<?php
if(isset($_POST['submit']))
{
global $wpdb;
$a=$_POST['jjkk1'];
$b=$_POST['jjkk2'];
$c=$_POST['jjkk3'];
$d=$_POST['jjkk4'];
$wpdb->insert( 'wp_ekz2020', array( 'num1' => $a, 'num2' => $b,'num3' => $c,
'num4' => $d), array( '%s', '%s','%s', '%s' ) );
}
?>
please try to place both html and php in single file
here is code i have try and it works
<form action="<?php the_permalink(); ?>" method="post">
<input type="text" name="num1">
<input type="text" name="num2">
<input type="text" name="num3">
<input type="text" name="num4">
<input type="submit" name="submit">
</form>
if(isset($_POST['submit']))
{
function insertnumber(){
global $wpdb;
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$num3 = $_POST['num3'];
$num4 = $_POST['num4'];
$table_name = "newtab";
$wpdb->insert( $table_name, array(
'num1' => $num1,
'num2' => $num2,
'num3' => $num3,
'num4' => $num4
) );
}
insertnumber();
}
put <?php the_permalink(); ?> in your form action
i hope above code will help you : )
change table name and also variable
in this case, whenever you submit the form with action attribute then the page gets reloaded, and '$wpdb' returns null. So this will cause error.
To prevent this you have to include 'wp-load.php' in your file or you can use
add_action('init', 'your_function_name')

Show the selected value everytime in the dropdown list

I need to display the selected category value displayed all the time. I have a list of category values and adding it to drop-down list from an array.
<form action="#" method="post">
<select name="dropDown" id="drop_down_id">
<option value=""> Select CATEGORY</option>
<?php
foreach($decoded as $key => $value ){
foreach($value as $key1 => $value1 ){
?> <option value="<?php echo $value1;?>"><?php echo $value1;?></option><?php
}
}
?>
<input type="submit" name="submit" value="Submit"/>
</form>
I see that you use post for form so you can use POST to know last selected:
$dropDown=htmlentities($_POST['dropDown'], ENT_QUOTES, "UTF-8");
And then just simple make if statement and its done:
if($dropDown==$value1){$selected='selected';}else{$selected=null;}
result:
<?php
$dropDown=htmlentities($_POST['dropDown'], ENT_QUOTES, "UTF-8");
?>
<form action="#" method="post">
<select name="dropDown" id="drop_down_id">
<option value=""> Select CATEGORY</option>
<?php
foreach($decoded as $key => $value ){
foreach($value as $key1 => $value1 ){
if($dropDown==$value1){$selected='selected';}else{$selected=null;}
echo '<option value="'.$value1.'" '.$selected.'>'.$value1.'</option>';
}
}
?>
<input type="submit" name="submit" value="Submit"/>
</form>
You can also store it in PHP SESSSION http://php.net/manual/en/function.session-start.php

Posting HTML form to new wp database table

I'm trying to make a HTML form post the input values to a custom table in the WordPress database. I've managed to get something to show up in a new row, but almost all of my values return N; instead of the value from the form.
Here's the code I have in my page template:
<?php
global $wpdb;
global $current_user;
$userID = $current_user->ID;
$brand = serialize($_POST["brand"]);
$url = serialize($_POST["url"]);
$sector = serialize($_POST["sector"]);
$keywords = serialize($_POST["keywords"]);
if (
'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'updateSearch' ) {
$ufDataUpdate = $wpdb->insert( 'wp_wct3', array(
'date' => current_time('mysql'),
'userid' => $userID,
'brand' => $brand,
'url' => $url,
'sector' => $sector,
'keywords' => $keywords ) );
}
?>
<form method="post">
<label for="brand">Brand/Product Name: </label>
<input type="text" id="brand" placeholder="eg: Spidr" class="clearfix" required />
<label for="website">Website address: </label>
<input type="url" id="url" placeholder="eg: www.spidr.co.uk" class="clearfix" required />
<label for="sector">Market sector: </label>
<input type="text" id="sector" placeholder="eg: Internet Marketing Tools" class="clearfix" required />
<label for="keyword">Keywords/Phrases:<br><span class="orange">(comma separated)</span></label>
<textarea cols="0" rows="8" class="light" id="keywords" required></textarea>
<input type="submit" id="submit" name="submit" class="button-65 mobile-button" value="release the spiders!">
<?php wp_nonce_field( 'updateSearch' ); ?>
<input name="action" type="hidden" id="action" value="updateSearch" />
</form>
Where wp_wct3 is the database name and each item in the array is the name of each column in that table.
I'm not sure if my issue lies in this code, or in the set-up of the database itself. I've used the Wordpress custom tables plugin to make the new table. The brand, url and sector simply use the text definition, while the keywords use enum('0','1').
Anyone have any ideas why the values aren't returning and I'm just getting N; ?
this is my custom-form template.. it works for me
<?php
/**
Template Name: Custom-Form
* The template for displaying all pages.
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site will use a
* different template.
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php
if (!empty($_POST)) {
global $wpdb;
$table = wp_achord;
$data = array(
'name' => $_POST['yourname'],
'chord' => $_POST['chord']
);
$format = array(
'%s',
'%s'
);
$success=$wpdb->insert( $table, $data, $format );
if($success){
echo 'data has been save' ;
}
}
else {
?>
<form method="post">
<input type="text" name="yourname">
<textarea name="chord"></textarea>
<input type="submit">
</form>
<?php } ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Figured out the main issue. I'd missed out the name attribute from the form itself, so my PHP wasn't picking up the field values!

Checkbox form sends zeros to mysql, whether boxes are checked or not

I've setup a checkbox form (each question has only one checkbox). The form is submitting, but it only sends zeros to mysql--whether the box has been checked or not. How can I get the correct values (1 or 0) sent to mysql?
Built in codeigniter/mysql.
FORM
<?php echo form_open('addFoo'); ?>
<input type="checkbox" name="foo1" value="" /> //I tried this w/values incl; still zeros
<input type="checkbox" name="foo2" value="" />
<input type="checkbox" name="foo3" value="" />
<input type="checkbox" name="foo4" value="" />
<?php echo form_submit('submit', 'Save Changes'); ?>
<?php echo form_close(); ?>
CONTROLLER
function addFoo()
{
if ($this->input->post('submit')) {
$id = $this->input->post('id');
$foo1 = $this->input->post('foo1');
$foo2 = $this->input->post('foo2');
$foo3 = $this->input->post('foo3');
$foo4 = $this->input->post ('foo4');
$this->load->model('foo_model');
$this->foo_model->addFoo($id, $foo1, $foo2, $foo3, $foo4);
}
}
MODEL
function addFoo($id, $foo1, $foo2, $foo3, $foo4) {
$data = array(
'id' => $id,
'foo1' => $foo1,
'foo2' => $foo2,
'foo3' => $foo3,
'foo4' => $foo4
);
$this->db->insert('foo_table', $data);
}
<input type="checkbox" name="foo2" value="" /> <-- needs a value. Your DB inserts a zero if it's empty.
I think the issue is likely on your DB end, not the form-end.

Passing data from DB to update form using CI CRUD

I'm trying to write a compact update controller for CRUD activity. Here is the basic code:
Controller:
function update($id)
{
$this->form_validation->set_rules('name','Name','required');
$this->form_validation->set_rules('age','Age','required|is_numeric');
$this->form_validation->set_rules('country','Country','');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) {
//Failed validation or first run
$data = $this->my_model->get_record($id);
$this->load->view('myform_view', $data);
} else {
//Validation success, update DB
}
}
View:
<?php
$attributes = array('class' => '', 'id' => '');
echo form_open('my_form', $attributes); ?>
<p>
<label for="name">Name</label>
<?php echo form_error('name'); ?>
<br /><input id="name" type="text" name="name" value="<?php echo set_value('name'); ?>" />
</p>
<p>
<label for="age">Age</label>
<?php echo form_error('age'); ?>
<br /><input id="age" type="text" name="age" value="<?php echo set_value('age'); ?>" />
</p>
<p>
<label for="country">Country</label>
<?php echo form_error('country'); ?>
<br /><input id="country" type="text" name="country" value="<?php echo set_value('country'); ?>" />
</p>
<p>
<?php echo form_submit( 'submit', 'Submit'); ?>
</p>
<?php echo form_close(); ?>
This is the basic structure, however the first time the form is run there is no validated data. Therefore I have to grab this from the DB. Whats the best way to pass this to the view on the first run? And then once the form has been submitted, if validation fails then I want the failed data to show not to reload from the DB again. Whats the best way to do this?
You should have another method for the viewing aspect. Then submit your form against the "update" method. In there, you define the the form_validation as you have now.
I asked a similar question. See this link
grab the data in update controller first for edit such as
$query = $this->db->where('id',$id)->get('table_name');
$data['edit'] = $query->result_array();
and then check it in view file
value="<?php if(isset($edit[0]['age'])){echo $edit[0]['age'];}else{echo set_value('age');}?>"