I have the following code:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query = "UPDATE #__cspartners_partners SET estado = '".TpoEstadoDocumentacion::Revisar."' WHERE id='" .$id. "'";
$db->setQuery($query);
$resultado = $db->query();
if(!$resultado) return 0;
$query returns this:
string(60) "UPDATE #__cspartners_partners SET estado = '3' WHERE id='1'"
And $resultado returns this:
bool(true)
If I execute this query directly in phpMyAdmin it works fine but its not working in my code. What am I missing?
Please refer to the Documentation for writing database queries so that you are aware of the latest coding standards.
For the query use this:
$value = TpoEstadoDocumentacion::Revisar;
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$fields = array(
$db->quoteName('estado') . ' = ' . $db->quote($value)
);
$conditions = array(
$db->quoteName('id') . ' = ' . (int)$id
);
$query->update($db->quoteName('#__cspartners_partners'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
Related
I want to delete a string from a database (of Wordpress posts) that starts with <noindex> and ends with </noindex>. The string is not always the same between those strings.
I found a PHP script that seems to old and doesn't work, it requires some fixes (like msqli) and I have no idea, I'm just trying to clean viruses from a database:
<?php
$co = mysql_connect("localhost", "MY_USER", "MY_PASS");
mysql_set_charset('utf8');
mysql_select_db("MY_DB", $co);
$selectQuery = "SELECT ID, post_content FROM wp_posts";
$res = mysql_query($selectQuery, $co) or exit(mysql_error());
while($row = mysql_fetch_assoc($res))
{
$updateQuery = "UPDATE wp_posts SET post_content = '" . addslashes(preg_replace("<noindex>(.*)</noindex>", '', $row['post_content'])) . "' WHERE ID = " . $row['ID'];
mysql_query($updateQuery) or exit(mysql_error());
}
mysql_close($co);
?>
Can you please help me? Thank you.
Try this code:-
<?php
$co = mysqli_connect("localhost", "MY_USER", "MY_PASS");
mysqli_set_charset($co,'utf8');
mysqli_select_db("MY_DB", $co);
$selectQuery = "SELECT ID, post_content FROM wp_posts";
$res = mysqli_query($co,$selectQuery) or exit(mysqli_error());
while($row = mysqli_fetch_assoc($res))
{
$updateQuery = "UPDATE wp_posts SET post_content = '" . addslashes(preg_replace("<noindex>(.*)</noindex>", '', $row['post_content'])) . "' WHERE ID = " . $row['ID'];
mysqli_query($co,$updateQuery) or exit(mysqli_error());
}
mysqli_close($co);
?>
I've got below snippet where $filter_xx values are extracted from a dropdown basis user choice.
I'm trying to query the mySQL database with what the user chose to query the database with via dropdown selection.
You will see that there are 4 $filter_xx variables and how many of them are set in a given instance is completely random.
The issue is when I use && in the query it checks if all four parameters are true and then throws and output. (Well I know && is suppose to work that way!). I tried replacing all && operators with || and had no luck.
How do I search the database with only options selected by the user?
if(isset($filter_brand) || isset($filter_year) || isset($filter_month) || isset($filter_status))
{
$query = "SELECT * FROM targets WHERE brand='$filter_brand' && startyear='$filter_year' && startmonth='$filter_month' && status='$filter_status' ORDER BY createdon DESC";
} else {
$query = "SELECT * FROM targets ORDER BY createdon DESC";
}
When you have several values that must work in a similar manner, use an array together with loop. I am supposing, you are using mysqli, change quoting for PDO if needed.
$mysqli = new mysqli("localhost", "user", "pass", "test");
//...
//SQL attr name => name of POST parameter
$filter = array('brand' => 'brand', 'startyear' => 'year',
'startmonth' => 'month', 'status' => 'status');
//here we'll store SQL conditions
$sql_filter = array();
foreach($filter as $key => $value)
{
if (isset($_POST[$value]))
{
//use your library function to quote the variable before using it in SQL
$sql_filter[] = $key . '="'. $mysqli->escape_string($_POST[$value]) . '"';
}
}
$query = "SELECT * FROM targets ";
if(isset($sql_filter[0]))
{
$query .= 'WHERE ' . implode(' AND ', $sql_filter) . ' ';
}
$query .= 'ORDER BY createdon DESC';
Try By This
$join = "";
//TAKE ONE BLANK VARIBLE THAT JOIN IF VALUE IS SET
if(isset($filter_brand)){
//IF VALUE ISSET THAN IT ADDED TO QUERY
$join .= " AND brand='$filter_brand'";
}
if(isset($filter_year){
$join .= " AND startyear='$filter_year'";
}
$query = "SELECT * FROM targets WHERE id != '' $join ORDER BY createdon DESC";
You can do something like this:
$query = 'SELECT * FROM targets';
$flag = 0;
if(isset($filter_brand) )
{
$query = "SELECT * FROM targets WHERE brand='$filter_brand'";
$flag = 1;
}
if(isset($filter_year)) {
if($flag==1)
$query .= " &&";
$query .= " startyear='$filter_year'";
$flag = 1;
}
if(isset($filter_month)) {
if($flag==1)
$query .= " &&";
$query = " startmonth='$filter_month'";
$flag = 1;
}
if(isset($filter_status)){
if($flag==1)
$query .= " &&";
$query = " status='$filter_status'";
$flag = 1;
}
if($flag == 1){
$query .= " ORDER BY createdon DESC";
} else {
$query = "SELECT * FROM targets ORDER BY createdon DESC";
}
Try this:
$query = "SELECT * FROM targets WHERE 1 ";
$query = isset($filter_brand) ? $query . " AND brand = '".$filter_brand."'" : $query;
$query = isset($filter_year) ? $query . " AND startyear = '".$filter_year."'" : $query;
$query = isset($filter_month) ? $query . " AND startmonth = '".$filter_month."'" : $query;
$query = isset($filter_status) ? $query . " AND status = '".$filter_status."'" : $query;
$query .= " ORDER BY createdon DESC";
I'm calling AJAX through below code on single table query and everything works fine and the results are printed:
public function getUnitsHTML() {
$jinput = JFactory::getApplication()->input;
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$sdate = date_create($jinput->get ('dsvalue'));
$mystartdate = date_format($sdate, "Y-m-d");
$edate = date_create($jinput->get ('devalue'));
$myenddate = date_format($edate, "Y-m-d");
$query->select($db->quoteName(array('id', 'unit', 'start_date', 'end_date', 'state')));
$query->from($db->quoteName('#__p_reservations'));
$query->where($db->quoteName('state')." = ".$db->quote('1'),'AND')
->where($db->quoteName('start_date')." >= ".$db->quote($mystartdate),'AND')
->where($db->quoteName('end_date')." <= ".$db->quote($myenddate));
$query->order('ordering ASC');
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as $result)
{
echo '<option value="' . $result->id . '" > ' . $result->unit.'</option>';
}
exit;
}
But when trying to make it more complex by joining with other tables I'm getting 500 Error:
public function getUnitsHTML() {
$jinput = JFactory::getApplication()->input;
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$sdate = date_create($jinput->get ('dsvalue'));
$mystartdate = date_format($sdate, "Y-m-d");
$edate = date_create($jinput->get ('devalue'));
$myenddate = date_format($edate, "Y-m-d");
$query
->select(array('a.unit','a.start_date','a.end_date','a.state','b.id','b.unit_number'))
->from($db->quoteName('#__p_reservations','a'))
->join('INNER',$db->quoteName('#__p_units','b') . ' ON (' . $db->quoteName('a.unit') . ' = ' . $db->quoteName('b.id') . ')')
->where($db->quoteName('a.state')." = ".$db->quote('1'),'AND')
->where($db->quoteName('a.start_date')." >= ".$db->quote($mystartdate),'AND')
->where($db->quoteName('a.end_date')." <= ".$db->quote($myenddate))
->order('ordering ASC');
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as $result)
{
echo '<option value="' . $result->id . '" > ' . $result->unit_number.'</option>';
}
exit;
}
your error is connected to mysql. It looks like your sql query is correct, but check back through the MYSQL command line. If the error goes on, this may be due to joomla JDbase.
I had an error with entering data in the database, but it had nothing to do with my code but with JDbase.
To make sure that the error is from joomla, put the joomla error console to work. It will tell you which file and line the error encounters.
Sorry for my bad English but it's not my mother tongue.
i did mistake on method of using order
->order('ordering ASC');
should change to:
->order($db->quoteName('a.unit'). 'ASC');
I am having a pretty weird problem with my PHP MySQL Query, I am trying to return rows that have the correct rname, rcity, and rstate values.
$sql = "SELECT * FROM `images` WHERE rname = '" . $rname . "' AND rcity = '" . $rcity . "' AND rstate = '" . $rstate . "'";
When I run that query, it only returns 0 results. However after some playing around with it, If I only use rname and rstate in the WHERE Clause it returns results.
$sql = "SELECT * FROM `images` WHERE rname = '" . $rname . "' AND rstate = '" . $rstate . "';
That works perfect. So when i tried just useing rcity in the WHERE Clause.
$sql = "SELECT * FROM `images` WHERE rcity = '" . $rcity . "'";
0 results return. So something is wrong with the rcity portion of the query. If I hard write the value into the query instead of the $rcity variable, it pulls up results. I doubled checked to make sure $rcity was declared, it has the correct value, etc.
I also created another test table in the database to check to see if it was a problem on the db side. Which the problem still existed.
Here is the full code of the getQuery() Function
private function getQuery($data){
// Takes raw data and creats image(s) query to search for listing resort...
$listing = $data['listing'];
$rname = $data['rname'];
$rcity = $data['rcity'];
$rstate = $data['rstate'];
$query = "SELECT * FROM `test` WHERE rname = '" . $rname . "' AND rcity = '" . $rcity . "' AND rstate = '" . $rstate ."'";
return $query;
}
And Here is my database class
class db {
public function __construct(){
$this->server = DB_SERVER;
$this->user = DB_USER;
$this->Pass = DB_PASS;
$this->Database = DB_Database;
}
protected function connect(){
return mysqli_connect($this->server, $this->user, $this->Pass, $this->Database);
}
public function query($sql){
$conn = $this->connect();
$query = $conn->query($sql);
if($query == false) {
throw new Exception("Query failed:".PHP_EOL.$conn->error.PHP_EOL.$sql);
}
if($query->num_rows == 0) {
// need E_NOTICE errors enabled to see this,
// on screen if display_errors is on, else in PHP error log
trigger_error("Query returned 0 rows:".PHP_EOL.$sql);
}
$result = array();
while ($row = $query->fetch_assoc()){
$result[] = $row;
}
return $result;
}
}
I call the query in a class __construct function like so
$con = new db;
$sql = $this->getQuery($data);
$result = $con->query($sql);
I think problem can be with syntax or mysql screening. Try to use PDO with bindParam method
$sql = "SELECT * FROM `images` WHERE rname = '$rname' AND rcity = '$rcity' AND rstate = '$rstate'";
Try to implement this i.e. using variable directly between ' (apostrophe), It should work perfect
use query for access the $current_rank this value want to access in different query but this value can not access any where in different query so how to access $current_rank......
$query = "select * from menu_master where menu_id =
$row_id and hotel_id='" . $_REQUEST['hotel_id'] . "'";
$result = mysql_query($query)."<br/>";
while($row=mysql_fetch_array($result))
{
$rank = $row['set_rank'];
}
$current_rank = $rank;
//echo $current_id = $row_id."<br/>";
//echo $new_rank =$_REQUEST['set_rank']."<br/>";
$sql = "select * from menu_master where set_rank = '$new_rank ' and hotel_id='".$_REQUEST['hotel_id']."'" ;
// echo $sql."<br/>";
$rs = mysql_query($sql)."<br/>";
while($row = mysql_fetch_array($rs))
{
$menu_id = $row['menu_id'];
$sql="update menu_master
set set_rank=$current_rank where menu_id= $menu_id and hotel_id='".$_REQUEST['hotel_id']."'";
//echo $sql."<br/>";
mysql_query($sql)."<br/>";
}
$sql="update menu_master set menu_name = '" . mysql_real_escape_string($_REQUEST['menu_name']) . "',
menu_name_ar = '" . mysql_real_escape_string($_REQUEST['menu_name_ar']) . "',
is_active = '" . $is_active . "',
set_rank = $new_rank where menu_id = '$current_id' and hotel_id='".$_REQUEST['hotel_id']."'";
//echo $sql."<br/>";
//exit;
mysql_query($sql);
Your current_rank seems to be an array. If you have single value in current_rank, then do not use while loop for it.
Just use $row=mysql_fetch_array($result);
$current_rank = $row['set_rank'];
Also you have commented out this line.
//echo $new_rank =$_REQUEST['set_rank']."";
So you have no value for $new_rank