Unknown system variable 'post_content' - php

Why Error - Unknown system variable 'post_content'
foreach( $posts as $post ) {
$post_content = $this->add_image_dimensions( $post->post_content );
if( $post_content != $post->post_content ) {
$query = "UPDATE " . $wpdb->prefix . "posts";
$query = " SET post_content = '" . $post_content . "' WHERE ID = " . $post->ID;
$wpdb->query( $query );
}
Sorry for my English

#Strawberry is right. You may want to do something like this:
$query = "UPDATE " . $wpdb->prefix . "posts";
$query = $query + " SET post_content = '" . $post_content . "' WHERE ID = " . $post->ID;

Related

How to update item in sql if a variable isn't empty?

I'm getting variable from parser.
If variable $desc is empty - my request just stops, but I need just pass it.
It's my part of code.
$desc = $description->plaintext;
if(!empty($desc)) {
$query = "UPDATE snowcore_parser_products SET description = 'blabla' WHERE remote_id = '" . $dataId . "';";
} else {
$query = "UPDATE snowcore_parser_products SET description = 'EMPTY' WHERE remote_id = '" . $dataId . "';";
}
$sql = mysqli_query($db, $query);
But if(!empty($desc)) doesn't work
Try this
$desc = $description->plaintext;
if(count($desc)>0) {
$query = "UPDATE snowcore_parser_products SET description = 'blabla' WHERE remote_id = '" . $dataId . "';";
} else {
$query = "UPDATE snowcore_parser_products SET description = 'EMPTY' WHERE remote_id = '" . $dataId . "';";
}
$sql = mysqli_query($db, $query);
Are you sure $desc is empty, mind you a space or newline character will not make it empty. Check the value by debugging or by var_dump(). If there is space or newline you can use trim().
$desc = trim($description->plaintext);
if(!empty($desc)) {
$query = "UPDATE snowcore_parser_products SET description = 'blabla' WHERE remote_id = '" . $dataId . "';";
} else {
$query = "UPDATE snowcore_parser_products SET description = 'EMPTY' WHERE remote_id = '" . $dataId . "';";
}
$sql = mysqli_query($db, $query);

Wordpress custom database query

I am trying to get the result from the database with the mysql LIKE but in wordpress its not working here is the code of what i am trying
//this is what i am putting in where clause.
$state = $_POST['state'];
//table name.
$table_name = $wpdb->prefix . 'userprofile';
//trying but this is returning empty
$q = 'SELECT * FROM ' . $table_name . 'WHERE state LIKE \'%' . esc_sql( like_escape( $state ) ) . '%\'';
echo $q;
$result = $wpdb->get_results($q);
if (empty($result)) {
echo "the result is empty";
}
//returns empty array.
print_r($result);
You are missing a space:
$q = 'SELECT * FROM ' . $table_name . 'WHERE[..snip..]
^---here
which means you're producing
SELECT * FROM whateveruserprofileWHERE
which is invalid SQL.
Like Marc B. said, there are some missing quotes and unnecessary quotations.. change your query line, to this:
$q = "SELECT * FROM $table_name
WHERE state LIKE '%". esc_sql( like_escape( $state ) ) . "%'
AND WHERE city LIKE '%". esc_sql( like_escape( $city ) ) . "%'
AND WHERE session LIKE '%". esc_sql( like_escape( $session ) ) . "%'
OR WHERE another LIKE '%". esc_sql( like_escape( $another ) ) . "%' ";
and your POST line to this:
$state = $_POST['state'];
do this way
$state = esc_sql( $state );
$state = like_escape( $state );
$state = '%' . $state . '%';
$q = 'SELECT * FROM ' . $table_name . 'WHERE state LIKE '$state';

$wpdb->get_results not working

i write following code to copy values from one table to another table but $wpdb->get_results not returning anything.
function rating_convert() {
global $wpdb;
$likes = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'up_down_comment_vote_totals', ARRAY_N);
foreach ($likes as $like) {
$wpdb->query('UPDATE ' . $wpdb->prefix . 'comment_rating SET ck_rating_up = ' . $like['vote_count_up'] . ' WHERE ck_comment_id = ' . $like['comment_id']);
$wpdb->query('UPDATE ' . $wpdb->prefix . 'comment_rating SET ck_rating_down = ' . $like['vote_count_down'] . ' WHERE ck_comment_id = ' . $like['comment_id']);
}
}
i have tested every line with echo function.foreach line not running and i think $wpdb->get_results is wrong.
please help me.
Sorry for my bad english.
i solve this.
my table have 147,308 rows.
problem is table rows number.
i add LIMIT code for only get 1000 rows and remove every row after copy to another table.thanks.
function comment_rating() {
global $wpdb;
$likes = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'up_down_comment_vote_totals LIMIT 0, 1000', ARRAY_A);
foreach ($likes as $like) {
echo "id " . $like['comment_id'] . "<br>";
$wpdb->query('UPDATE ' . $wpdb->prefix . 'comment_rating SET ck_rating_up = ' . $like['vote_count_up'] . ' WHERE ck_comment_id = ' . $like['comment_id']);
$wpdb->query('UPDATE ' . $wpdb->prefix . 'comment_rating SET ck_rating_down = ' . $like['vote_count_down'] . ' WHERE ck_comment_id = ' . $like['comment_id']);
$wpdb->query('DELETE FROM ' . $wpdb->prefix . 'up_down_comment_vote_totals WHERE comment_id = ' . $like['comment_id']);
}
}

php new hosting with double quote error

I have just change my hosting, before all my PHP scripts worked fine
but now i get many mysql error like this:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near column = \'value\'
it seems that there is a double quote in some script
there is a way to resolve without update all my PHP scripts?
EDIT: example of PHP code
function test( $table,$column, $where ){
if( get_magic_quotes_gpc() ) { $where = strip_tags( trim( $where ) ); }
else{ $where = strip_tags( mysql_real_escape_string( trim( $where ) ) ); }
$where = "AND id = '" . $where . "' ";
$query = "SELECT " . $column . " FROM " . $table . " WHERE 1 " . $where . " LIMIT 1";
//...
You have to either pass the $table variable or declare it as global, if defined outside.
function test( $column, $where ){
global $table;
if( get_magic_quotes_gpc() ) { $where = strip_tags( trim( $where ) ); }
else{ $where = strip_tags( mysql_real_escape_string( trim( $where ) ) ); }
$where = "AND id = '" . $where . "' ";
$query = "SELECT " . $column . " FROM " . $table . " WHERE 1 " . $where . " LIMIT 1";
What happens if your function looks like this?
function test( $table,$column, $where ){
$where=stripslashes($where);
$where = strip_tags(mysql_real_escape_string(trim( $where )));
$where = "AND id = '" . $where . "' ";
$query = "SELECT " . $column . " FROM " . $table . " WHERE 1 " . $where . " LIMIT 1";
}

session having different session ids on different pages of single domain, non secure pages!

I'm pulling my hair out over this one. I have tried to make a simple script to store sessions so when a user closes there browser, they can come back later and their shopping basket will still be in tact. This all seemed to be going fine until i noticed that on some items the basket was containing same items as the previous. After some checks I noticed the session id was different on these odd pages! Here's my code which sits at the top of my framework.
<?php
session_start();
function sessions(){
if( ! isset( $_COOKIE['PHPSESSID'] ) ) {
setcookie( "PHPSESSID", session_id(), strtotime('+ 30 days') );
}else{
$con = Database::getInstance();
if( session_id() != $_COOKIE['PHPSESSID'] ) {
$re = $con->query( "SELECT * FROM `" . TABLE_PREFIX . "_tbl_sessions` WHERE session_id = '" . $_COOKIE['PHPSESSID'] . "'" );
if( $re->num_rows != 0 ) {
$ar = $re->fetch_assoc();
$id = $ar['id'];
$_SESSION['basket'] = unserialize( stripslashes( $ar['basket'] ) );
$con->query("UPDATE `" . TABLE_PREFIX . "_tbl_sessions` SET session_id = '" . session_id() . "' WHERE id = '$id' " );
}
unset($_COOKIE['PHPSESSID']);
setcookie( "PHPSESSID", session_id(), strtotime('+ 30 days') );
header('Location: ' . get_base_url() );
}else{
$re = $con->query( "SELECT * FROM `" . TABLE_PREFIX . "_tbl_sessions` WHERE session_id = '" . $_COOKIE['PHPSESSID'] . "'" );
if( $re->num_rows != 0 ) {
$ar = $re->fetch_assoc();
$id = $ar['id'];
if( ! empty( $_SESSION['basket'] ) ) {
$con->query("UPDATE `" . TABLE_PREFIX . "_tbl_sessions` SET session_id = '" . $_COOKIE['PHPSESSID'] . "', data = '" . addslashes( serialize( $_SESSION['basket'] ) ) . "' WHERE id = '$id'" );
}else{
$con->query( "DELETE FROM `" . TABLE_PREFIX . "_tbl_sessions` WHERE id = '$id'" );
}
}else{
if( ! empty( $_SESSION['basket'] ) ) {
$con->query( "INSERT INTO `" . TABLE_PREFIX . "_tbl_sessions` ( `session_id`, `stamp`, `data`) VALUES ( '" . $_COOKIE['PHPSESSID'] . "', NOW(), '" . addslashes( serialize( $_SESSION['basket'] ) ) . "' )" );
}
}
}
}
echo 'cookie: ' . $_COOKIE['PHPSESSID'] . ' : session(): ' . session_id();
}
?>
any help on this matter is much appreciated.
** EDIT **
i've tried to make it more simpler but still same problem
<?php
function sessions(){
$con = Database::getInstance();
if( session_id() == '' ) {
if( isset( $_COOKIE['session_id'] ) ) {
session_start();
$re = $con->query( "SELECT * FROM `" . TABLE_PREFIX . "_tbl_sessions` WHERE session_id = '" . $_COOKIE['session_id'] . "'" );
if( $re->num_rows != 0 ) {
$ar = $re->fetch_assoc();
$id = $ar['id'];
if( session_id() != $_COOKIE['session_id'] ) {
$_COOKIE['session_id'] = session_id();
$con->query("UPDATE `" . TABLE_PREFIX . "_tbl_sessions` SET session_id = '" . session_id() . "' WHERE id = '$id' " );
$_SESSION['basket'] = unserialize( stripslashes( $ar['data'] ) );
}else{
if( isset( $_SESSION['basket'] ) ) {
$con->query("UPDATE `" . TABLE_PREFIX . "_tbl_sessions` SET data = '" . addslashes( serialize( $_SESSION['basket'] ) ) . "' WHERE id = '$id' " );
}
}
}else{
$con->query( "INSERT INTO `" . TABLE_PREFIX . "_tbl_sessions` ( `session_id`, `stamp`, `data`) VALUES ( '" . $_COOKIE['session_id'] . "', NOW(), '' )" );
}
}else{
session_start();
setcookie( "session_id", session_id(), strtotime('+ 30 days') );
$_COOKIE['session_id'] = session_id();
}
}else{
die('session has previously been created');
}
echo 'cookie: ' . $_COOKIE['session_id'] . ' : session(): ' . session_id();
}
?>
<?php
function sessions(){
$con = Database::getInstance();
if( session_id() == '' ) {
session_start();
if( isset( $_COOKIE['session_id'] ) ) {
$re = $con->query( "SELECT * FROM `" . TABLE_PREFIX . "_tbl_sessions` WHERE session_id = '" . $_COOKIE['session_id'] . "'" );
display_error( $con );
if( $re->num_rows != 0 ) {
$ar = $re->fetch_assoc();
$id = $ar['id'];
if( session_id() != $_COOKIE['session_id'] ) {
setcookie( "session_id", '', strtotime('- 30 days'), '/', 'localhost' );
setcookie( "session_id", session_id(), strtotime('+ 30 days'), '/', 'localhost' );
$con->query("UPDATE `" . TABLE_PREFIX . "_tbl_sessions` SET session_id = '" . session_id() . "' WHERE id = '$id' " );
display_error( $con );
$_SESSION['basket'] = unserialize( stripslashes( $ar['data'] ) );
}else{
if( isset( $_SESSION['basket'] ) ) {
$con->query("UPDATE `" . TABLE_PREFIX . "_tbl_sessions` SET data = '" . addslashes( serialize( $_SESSION['basket'] ) ) . "' WHERE id = '$id' " );
display_error( $con );
}
}
}else{
$con->query( "INSERT INTO `" . TABLE_PREFIX . "_tbl_sessions` ( `session_id`, `stamp`, `data`) VALUES ( '" . $_COOKIE['session_id'] . "', NOW(), '' )" );
display_error( $con );
}
}else{
setcookie( "session_id", session_id(), strtotime('+ 30 days'), '/', 'localhost' );
$_COOKIE['session_id'] = session_id();
}
}else{
die('session has previously been created');
}
echo 'cookie: ' . $_COOKIE['session_id'] . ' : session(): ' . session_id();
}
function display_error( $con ) {
if( isset( $con->error ) && $con->error != '' ) {
die( $con->error );
}
}
?>
The Above works!
$_COOKIES['foo'] = 'bar' *does not* re-value the cookie in the browser only during the script.
The other problem was needing to set the path and domain of the cookie to stop multipul cookies being created. Works and runs smoothly!

Categories