wpdb update query not working - mysql

I've made an email script that should update as soon as wp_mail has result. For some reason my value won't update. Have I missed something? I am receiving the mail so the wp_mail works.
Cheers!
$email_result = wp_mail( $to, $subject, $message, $headers );
if( $email_result ){//wp_mail() processed the request successfully
global $wpdb;
$table_name = $wpdb->prefix . "wpsc_coupon_codes";
$coupon_id = $ereminder->ID;
$ereminders = $wpdb->query( $wpdb->prepare("
UPDATE *
FROM $table_name
SET reminder = 1
WHERE ID = $coupon_id
") );
}

Try This:
$wpdb->update( $table_name, array( 'reminder' => 1),array('ID'=>$coupon_id));

try this
UPDATE $table_name
SET reminer = 1
WHERE ID = $coupon_id

We could use $wpdb->update, like this:
global $wpdb;
$table_name = $wpdb->prefix.'your_table_name';
$data_update = array('row_name_1' => $row_data_1 ,'row_name_2' => $row_data_2);
$data_where = array('row_name' => $row_data);
$wpdb->update($table_name , $data_update, $data_where);

You may change instead of (UPDATE * FROM)
$ereminders = $wpdb->query($wpdb->prepare("UPDATE $table_name SET reminer='1' WHERE ID=$coupon_id"));
and use no break.
Thank you.

Example of mine that is working:
$result = $wpdb->update(
$wpdb->prefix .'sae_calendar',
array(
'year' => $year,
'quarter' => $quarter,
'start_date' => $start_date,
'end_date' => $end_date,
'reservation_start_date' => $reservation_start_date,
'reservation_end_date' => $reservation_end_date
),
array(
"id" => $id
)
);

<?php
global $wpdb;
if(isset($_POST['progress'])){
$table=t_test;
$data=array('client_development'=>$_POST['Progress']);
$where=array('p_id'=>$_SESSION['id']);
$format=("%d");
$whereFormat=("%d");
$result4=$wpdb->UPDATE($table,$data,$where,$format,$whereFormat);
}
?>
<form method="post">
<input type="text" name="Progress">
<input type="submit" name="progress" value="Prog%">
</form>
<?php
if(isset($_POST['progress'])){
$table=t_test;
$data=array('client_development'=>$_POST['Progress']);
$where=array('p_id'=>$_SESSION['id']);
$format=("%d");
$whereFormat=("%d");
$result4=$wpdb->UPDATE($table,$data,$where,$format,$whereFormat);
}
?>
<form method="post">
<input type="text" name="Progress">
<input type="submit" name="progress" value="Prog%">
</form>

Related

How can i solve my codeigniter batch update problem?

Hello guys I just want to ask in my project there are three tables product, color and product_color.I insert database using insert_batch then it work fine, when i update product_color table using update_batch then face some problems.Here's my sample code:
Database:
product:id,name,sku...
color:id,color_name
product_color:id,pro_id,color_id
Input Form:
<?php foreach($colors as $color): ?>
<input type="checkbox" class="form-check-input" name="color[]" value="<?php echo $color->color_id; ?>" <?php foreach ($productcolor as $key => $value){ $array[] = $value->color_id;} if(in_array($color->color_id,$array)) echo 'checked'; else ''; ?>>
<label class="form-check-label">
<?php echo $color->color_name; ?>
</label>
<?php endforeach; ?>
Actually i want to pass primary id from product_color table.Here i pass color_id.Have any way to pass primary id from input form;
Here Is my Controller:
$colorBatch = array();
foreach ($color as $colorvalue) {
$colorBatch[] = array(
'id'=>$id
'pro_id' =>$pid,
'color_id' => $colorvalue
);
}
$this->db->update_batch('product_color', $colorBatch,'pro_id');
Where $pid contains product_id;
Is it possible to pass product_color table primary id from input form or Have any better solution to solve this.Sorry for bad english.
Thanks
Please check below code as your array structure is wrong;
$colorBatch = array();
foreach ($color as $key => $colorvalue) {
$colorBatch[$key] = array(
'id'=>$id
'pro_id' =>$pid,
'color_id' => $colorvalue
);
}
$this->db->update_batch('product_color', $colorBatch,'pro_id');

how to insert these data into mysql in wampserver

here are is the code.no matter what i did couldnt connect to mysql database in phpadmin in the wampserver all attempts has failed please help to solve
if (isset($_POST['continue'])) {
$j=0;
while ($j < $passengers)
{
$register_data2 = array(
'first_name' => $_POST["fname"][$j],
'last_name' => $_POST["lname"][$j],
'passport' => $_POST["passport"][$j],
'visa' => $_POST["visa"][$j],
'address1' => $_POST["address1"][$j],
'address2' => $_POST["address2"][$j],
'email' => $_POST["email"][$j],
'contact' => $_POST["contact"][$j],
'pin' => $_POST["pin"][$j],
'leaving_from' => $pieces[0],
'going_to' => $pieces[2],
'depart_date' => $pieces[7],
'depart_time' => $pieces[12],
'arrival_time' => $pieces[17],
'grand_fare' => $pieces[22],
'returning_from' => $pieces1[0],
'returning_to' => $pieces1[2],
'returning_date' => $pieces1[7],
'returning_time' => $pieces1[11],
'reaching_time' => $pieces1[16],
'fare' => $pieces1[21]
);
session_start();
$_SESSION['ticket'][] = $_POST["fname"][$j];
$_SESSION['ticket'][] = $_POST["lname"][$j];
$_SESSION['ticket'][] = $_POST["passport"][$j];
$_SESSION['ticket'][] = $_POST["visa"][$j];
$_SESSION['ticket'][] = $_POST["pin"][$j];
register_passenger($register_data2);
$j = $j+1;
}
$_SESSION['ticket1'] = $pieces[0];
$_SESSION['ticket2'] = $pieces[2];
$_SESSION['ticket3'] = $pieces[7];
$_SESSION['ticket4'] = $pieces[12];
$_SESSION['ticket5'] = $pieces[17];
$_SESSION['ticket6'] = $pieces[22];
$_SESSION['ticket11'] = $pieces1[0];
$_SESSION['ticket22'] = $pieces1[2];
$_SESSION['ticket33'] = $pieces1[7];
$_SESSION['ticket44'] = $pieces1[11];
$_SESSION['ticket55'] = $pieces1[16];
$_SESSION['ticket66'] = $pieces1[21];
}
?>
<?php
if (isset($_POST['pay'])){
if ($_POST['cash'] != $grand_total) {
echo "*Pay the given amount!"."<br>";
}
else{
header ('Location: ticket.php');
}
}
?>
<h2> Select payment method </h2>
<form action="payment.php" method="post">
<input type="radio" name="payment" id="cash" checked="checked" value="cash">
<label for="cash">Cash</label>
<input type="number" id="cash" name="cash" size="8"><br><br>
<input type="radio" name="payment" id="card" value="card">
<label for="card">Card</label>
<select>
<option>Debit card</option>
<option>Credit card</option>
</select>
<br>
<img src="Credit.jpg">
<br>
<input type="submit" name="pay" value="Make payment">
</form>
<?php
if(isset($_POST["Continue"])){
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$passport = $_POST['passport'];
$visa = $_POST['visa'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$pin = $_POST['pin'];
mysql_query( "INSERT INTO passengers (first_name,last_name,passport,visa,address1,address2,email,contact,pin) VALUES('$lastname','$passport','passport','$visa','$address1','$address2','$email','$contact','$pin')");
}
?>
i couldnt connect the data to mysql database in the phpadmin in the wampserver. it shows up nothing. as you can see below one of my many attempt to correct to make it work it up as fail. non of the method that i tried didnt work. please help to sove this problem
It seems you messed up the values list:
mysql_query( "INSERT INTO passengers
(first_name,last_name,passport,visa,address1,address2,email,contact,pin)
VALUES
('$lastname','$passport','passport','$visa','$address1','$address2','$email','$contact','$pin')");
First name is missing, 'passport' should not be there, etc. It should be:
('$firstname','$lastname','$passport','$visa','$address1','$address2','$email','$contact','$pin')");

How to create permalink URL for API in WordPress?

I use WordPress and list data from my API:
$url = "http://example.com/all-list";
$response = wp_remote_get( $url );
$data_body = json_decode( wp_remote_retrieve_body( $response ), true );
<?php foreach ( $data_body as $real_data ): ?>
<h1><?php echo $real_data["title"]; ?></h1>
<?php end foreach; ?>
But in I don't know how to give url.
Example: I'd like to give <a href="/real-data?id=<?php $real_data['id'] ?>"> then return new view template. It like get_permalink(); to view single page post.
How could I do this in WordPress?
Check this examples below.
// This would output '/client/?s=word&foo=bar'
echo esc_url( add_query_arg( 'foo', 'bar' ) );
// This would output '/client/?s=word&foo=bar&baz=tiny'
$arr_params = array( 'foo' => 'bar', 'baz' => 'tiny' );
echo esc_url( add_query_arg( $arr_params ) );
https://developer.wordpress.org/reference/functions/add_query_arg/

Insert data inner join Cakephp 3.0

I'm new to CakePHP, I just finished the blog/article tutorial and now I am trying to add the comment system to it.
In my comments table I have, Id, article_Id, name, email, text and created. In my articles table I have Id, user_id, title, text, created.
I'm trying to add the comment according to the article ID by using Insert Inner join Query. So let's say a user views a specific article like /view/1, before he add comments, the article ID is already passed so he can comment accordingly to that specific article ID he is viewing. How can I do that? I hope I'm clear.
This is my add function for my comment:
public function leavemessage()
{
$comment = $this->Comments->newEntity();
$articles = $this->loadModel('Articles');
$leavemessage = $articles->find()
->hydrate(false)
->join([
'table' => 'comments',
'alias' => 'c',
'type' => 'LEFT',
'conditions' => 'c.article_id = articles.id',
])
->insert(['id'])
->where(['c.article_id = articles.id'])
->execute();
if ($this->request->is('post')) {
$comment = $this->Comments->patchEntity($comment, $this->request->data);
if ($this->Comments->save($comment)) {
$this->Flash->success('The comment has been saved.');
return $this->redirect([
'controller' => 'Articles',
'action' => 'index',
'c.article_id'
]);
} else {
$this->Flash->error('The comment could not be saved. Please, try again.');
}
}
$articles = $this->Comments->Articles->find('list', ['limit' => 200]);
$this->set(compact('comment', 'articles'));
$this->set('_serialize', ['comment']);
}
My add comments view
<div class="comments form large-10 medium-9 columns">
<?= $this->Form->create($comment); ?>
<fieldset>
<legend><?= __('Add Comment') ?></legend>
<?php
echo $this->Form->input('article_id', ['value' => $articleId]);
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('text');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

Yii2 preselect radiobutton value

I know I can use
<?= $form ->field($model, 'subject')
->textInput(array('value' => 'VALUE'))
->label('Titel'); ?>
to prefill a Textfield, but how can I do it for a radioList?
<?= $form ->field($model, 'locations')
->radioList($regionen)
->label('Regionen');
I could use ->textInput again but this transforms the whole List into a single Textfield
Alternativly: Is there a better way to modify a database record? Currently I'm trying to set all values into a new form.
Put the value that you want selected in the locations attribute of your $model and after rendering, it will be pre-selected in the radio list. That is:
$model->locations = ....
I assume that locations is a foreign key to some other table (or maybe a fixed list of strings).
Referring the documentation :
Pass the second parameter, options[] to radioList()
$options = [
'item' => function($index, $label, $name, $checked, $value) {
// check if the radio button is already selected
$checked = ($checked) ? 'checked' : '';
$return = '<label class="radio-inline">';
$return .= '<input type="radio" name="' . $name . '" value="' . $value . '" ' . $checked . '>';
$return .= $label;
$return .= '</label>';
return $return;
}
]
<?= $form->field($model, 'locations')
->radioList($regionen, $options)
->label('Regionen');
Hope this helps..