Wordpress Insert Query not working - mysql

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();
}

Related

how to update table in silex (PHP micro-framework)?

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'));

Wordpress mysql query not showing results?

The query should return thumbnail, title, name, price etc (all the fields)
<?php
query_posts('meta_key=cp_job&meta_value=Sell');
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
the_content();
//
} // end while
} // end if
?>
It should return results where meta_key=cp_job and meta_value=Sell.. I've tried all sorts of queries and this has taken up several hours, as i've yet to find a solution.
I'm working with a theme and the only time i've gotten a result is with this query
$metakey = 'cp_job';
$job = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
foreach ($job as $value) {
echo $value";
}
This query doesn't do what i want to accomplish.
I simply want "SELECT * FROM table WHERE cp_job='Sell'"; but WordPress makes everything so complicated. I don't even know the table which hold my results!...
Any help please..
You may try this:
$args = array(
'post_type' => 'post', // Or custom post type if it's a CPT
'meta_key' => 'cp_job',
'meta_query' => array(
array(
'key' => 'cp_job',
'value' => 'Sell'
)
)
);
Then run the query and loop the $query same way:
$query = new WP_Query( $args );

insert data on wordpress database with a foreach

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
) );

wordpress query 2 sets of meta keys

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.') ");
?>

FB:registration - array doesnt save any data

I did the Facebook Registration Plugin like in the official Facebook tutorial and with this code there it shows the total Array on screen:
if ($_REQUEST) {
echo '<p>signed_request contents:</p>';
$response = parse_signed_request($_REQUEST['signed_request'],
FACEBOOK_SECRET);
echo '<pre>';
print_r($response);
echo '</pre>';
} else {
echo '$_REQUEST is empty';
}
this brings me the following array:
signed_request contents:
Array
(
[algorithm] => HMAC-SHA256
[expires] => 1324xxxx400
[issued_at] => 132446xxx80
[oauth_token] => AAADRjT73VhwBALl6Gb3EVarvyGU7xxxxxxxxxxxxxxxxxxxSAUuoZAGlydkX2pH3
[registration] => Array
(
[name] => Philipp Mail
[email] => p.mail#xxxxde
[location] => Array
(
[name] => Munich, Germany
[id] => 1.1604xxxxxx286E+14
)
[birthday] => xx/xx/19x7
)
[registration_metadata] => Array
(
[fields] => [{'name':'name'}, {'name':'email'}, {'name':'location'}, {'name':'birthday'}]
)
[user] => Array
(
[country] => de
[locale] => de_DE
)
[user_id] => 10xxxxxxx5426
)
Now I changed it for storing several data to mysql:
if ($_REQUEST) {
echo '<p>signed_request contents:</p>';
$response = parse_signed_request($_REQUEST['signed_request'],
FACEBOOK_SECRET);
$name_arr = explode(' ',$name,2);
$vname = $name_arr[0];
$zname = isset($name_arr[1])?$name_arr[1]:'';
$email = $response["registration"]["email"];
$ort = $response["registration"]["location"]["name"];
$anrede = $response["registration"]["gender"];
$geburtstag = $response["registration"]["birthday"];
// Connecting to database
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
// Inserting into users table
$result = mysql_query("INSERT INTO REKRU_mem (mem_id, vname, zname, ort, email, userpass, chili, regdatum, geburtstag, fbuid)
VALUES
(NULL, '$vname', '$zname', '$ort', '$email', MD5('".$gesamtpass."'),'$chili', '0000-00-00 00:00:00', '$geburtstag', '$user_fbid')");
if($result){
// GOT RESULTS
}
else
{
// Error in storing
}
}
else
{
echo '$_REQUEST is empty';
}
When I look in mysql after a registration there is a new line but it saves only the actual registration time. Can anyone of U see what is my mistake?
do not use this code in a plublic Area! You can make a SQL-Injection (could also be the problem why you cannot insert).
Try this instead of the mysql_query:
$vname = mysql_real_escape_string($vname);
$zname = mysql_real_escape_string($zname);
$ort = mysql_real_escape_string($ort);
$email = mysql_real_escape_string($email);
$gesamtpass = mysql_real_escape_string($gesamtpass);
$chili = mysql_real_escape_string($chili);
$geburtstag = mysql_real_escape_string($geburtstag);
$user_fbid = mysql_real_escape_string($user_fbid);
$result = mysql_query("INSERT INTO REKRU_mem (mem_id, vname, zname, ort, email, userpass, chili, regdatum, geburtstag, fbuid)
VALUES (NULL, '".$vname."', '".$zname."', '".$ort."', '".$email."', MD5('".$gesamtpass."'),'".$chili."', '0000-00-00 00:00:00', '".$geburtstag."', '".$user_fbid."')");