On Wordpress, i want to insert data in my database on a table i created for purpose, i'm using the global $wpdb and insert.
I've got many more data to insert, is that possible to insert a foreach or any loop inside my array?
Thank you in advance,
jean-Charles
global $wpdb;
$name="JC";
$email="jcdarocha#gmail.com";
$website="www.jcdarocha.co.uk";
$country="UK";
$message="hello World";
global $wpdb;
$wpdb->insert( 'my_table',
array(
'$name' => '$name',
'$email' => $email,
'$website' => $website,
'$country' => $country,
'$message' => $message
),
array( '%s', '%d' ) );
Use $wpdb->query and concat all data into a string
$insert = "INSERT INTO my_table (name, email, website, country, message) VALUES ";
foreach( $datas as $data ) {
$insert .= sprintf("('%s','%s','%s','%s','%s'),", $data['name'], $data['email'], $data['website'] , $data['country'], $data['message'] )
}
$wpdb->query( rtrim($insert, ','));
global $wpdb;
$name="JC";
$email="jcdarocha#gmail.com";
$website="www.jcdarocha.co.uk";
$country="UK";
$message="hello World";
global $wpdb;
$wpdb->insert( 'my_table',
array(
'name' => $name,
'email' => $email,
'website' => $website,
'country' => $country,
'message' => $message
) );
Related
These are the values I want to insert into the database:
$name4 = $request['name4'];
$email4 = $request['email4'];
$phone = $request['phone'];
$cat = $request['cat'];
$rname = $request['rname'];
$file = $request['file'];
$ingr = $request['ingr'];
$qty = $request['qty'];
$unit = $request['unit'];
$recp = $request['recp'];
$items = array(
'ingredients' => $ingr,
'quantity' => $qty,
'unit' => $unit
);
And insert query is as follows:
DB::INSERT("insert into tbl_contest (name, email, phone, category, recp_name, file, ingredient, recp_desc)" . "values('$name4','$email4','$phone','$cat','$rname','$file','$items','$recp')");
I wanted to add $ingr, $qty and $unit into the column ingredient.
Can anyone help me?
Thanks in advance
The $items variable can not be an array, you can turn it into a json string.
$items= json_encode(array(
'ingredients' => $ingr,
'quantity' => $qty,
'unit' => $unit
));
I am creating web services for an App, but I am stuck with a insert query. Actually I made a rating system and want to insert value in database with wordpress standard.
Here is my query:
$res = $wpdb->query( $wpdb->prepare(
"INSERT INTO $table_name(rating_postid, rating_posttile, rating_rating, rating_username, rating_userid) VALUES (%d, %s, %d, %s, $d )",
array(
$rating_postid,
$post_title,
$post_rating,
$user_name,
$rating_userid
)
)
);
and here is the other one:
$res = $wpdb->insert(
$table_name,
array(
'rating_postid' => $rating_postid,
'rating_posttile' => $post_title,
'rating_rating' => $post_rating,
'rating_username' => $user_name,
'rating_userid' => $rating_userid
)
);
But no one is working, why?
if($res){
echo 'inserted';
}else{
echo 'not inserted';
}
I am getting else part alwasy
I used these queries very often and they worked me very well, But I am not sure what's wrong with them now... :(
Try this.
$wpdb->insert(
$table_name,
array(
'rating_postid' => $rating_postid,
'rating_posttile' => $post_title,
'rating_rating' => $post_rating,
'rating_username' => $user_name,
'rating_userid' => $rating_userid
),
array(
'%d',
'%s',
'%s',
'%s',
'%d'
)
);
Declaring $wpdb as global and using it to execute an SQL query statement that returns a PHP object
global $wpdb;
$table_name = $wpdb->prefix . "YOUR_TABLE_NAME"; // Enter without prefix
$data = array(
'rating_postid' => $rating_postid,
'rating_posttile' => $post_title,
'rating_rating' => $post_rating,
'rating_username' => $user_name,
'rating_userid' => $rating_userid
);
$result = $wpdb->insert($table_name, $data);
if( $result ){
echo "Inserted..!";
}else{
echo "Something wrong..!";
$wpdb->show_errors();
}
I have used $sql to insert data into table using below code in model
$sql = "INSERT into $database";
$sql .= ( ;
foreach($data as $key => $value)
{
$sql .= "$key, ";
}
$sql = substr($sql, 0);
$sql .= ) VALUES ( ;
foreach($data as $key => $value)
{
$sql .= "'$value', ";
}
$sql = substr($sql, 0);
$sql .=);
my problem is how to update sametable using silex
according to silex documentation you should use doctrine ORM provider:
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(
'driver' => 'pdo_sqlite',
'path' => __DIR__.'/app.db',
),
));
$app['db']->insert('tablename', array(
'key1' => 'val1',
'key2' => 'val2',
'key3' => 'val3'));
My code is like this :
My array of city (echo '<pre>';print_r($city);echo '</pre>';die();) :
Array
(
[0] => Array
(
[CityCode] => 14879
[CityName] => Soldeu
)
[1] => Array
(
[CityCode] => 14881
[CityName] => Ari'nsal
)
[2] => Array
(
[CityCode] => 14882
[CityName] => El Tarter
)
[3] => Array
(
[CityCode] => 14883
[CityName] => Grau Roig
)
[4] => Array
(
[CityCode] => 175198
[CityName] => Llorts
)
)
In city code : 14881, city name : Ari'nsal
It's single quote in string.
I try code like this :
$date = date('Y-m-d H:i:s');
$sql = "INSERT INTO hotel_search_city (nation_code, city_code, city_name, created_at, updated_at) values ";
$valuesArr = array();
foreach($city as $row){
$nation_code = $value->nation_code;
$city_code = $row['CityCode'];
$city_name = mysqli_real_escape_string($row['CityName']);
$created_at = $date;
$updated_at = $date;
$valuesArr[] = "('$nation_code', '$city_code', '$city_name', '$created_at', '$updated_at')";
}
$sql .= implode(',', $valuesArr);
$query = $sql;
$this->db->query($query);
There exist error like this : Message: mysqli_real_escape_string() expects exactly 2 parameters, 1 given....
Any solution to solve my problem?
Thank you very much
$date = date('Y-m-d H:i:s');
$sql = "INSERT INTO hotel_search_city (nation_code, city_code, city_name, created_at, updated_at) values (?, ?, ?, ?, ?)";
foreach ($city as $row) {
$nation_code = $value->nation_code;
$city_code = $row['CityCode'];
$city_name = $row['CityName'];
$created_at = $date;
$updated_at = $date;
$fields = array($nation_code, $city_code, $city_name, $created_at, $updated_at);
$this->db->query($query, $fields);
}
or
$date = date('Y-m-d H:i:s');
foreach ($city as $row) {
$fields = array(
'nation_code' => $value->nation_code,
'city_code' => $row['CityCode'],
'city_name' => $row['CityName'],
'created_at' = $date,
'updated_at' = $date
);
$this->db->insert('hotel_search_city', $fields);
}
I am trying to do a custom sql query to get 2 lots of post_meta keys and there values. Here is my code:
<?php
$queried_post = get_post($post_id);
$title = $term->name ;
$userid = $current_user->user_email;
$result = mysql_query("SELECT * FROM wp_postmeta WHERE meta_key='user_programme_name' AND meta_value='".$title."' AND meta_key='user_email' AND meta_value='".$userid."' ");
$num_rows = mysql_num_rows($result);
if ($num_rows > 0 ) {
?>
<p style="width:430px;">You have already requested a place on this course</p>
<?php } ?>
The first part of the query works when i jsut query the first set of keys and values:
meta_key='user_programme_name' AND meta_value='".$title."'
But as soon as i add the second lot:
AND meta_key='user_email' AND meta_value='".$userid."'
It doesn't work. Is there something I'm doing wrong?
Any help would be greatly appreciated.
Cheers, Dan
Use something like this :
$args = array( 'post_type' => 'myposttype',
'posts_per_page' => …,
'offset' => …,
'meta_query'=>array(
'relation' => 'AND',
array(
'key'=>'_first_key',
'value'=> youvalue,
'type' => 'DATE', // etc.
'compare' => '='
),
array(
'key'=>'_second_key',
'value'=> youvalue,
'type' => 'DATE', // etc.
'compare' => '='
)
)
);
$yourloop = new WP_Query( $args );
Fix your SQL syntax for selecting multiple values on the same column:
<?php
$result = mysql_query("SELECT * FROM wp_postmeta WHERE meta_key IN ('user_programme_name', 'user_email') AND meta_value IN ('.$title.', '.$userid.') ");
?>