PHP SQL SELECT and then UPDATE the value - php

I have a problem.
$db = JFactory::getDBO();
$res2 = $_data['DateSubmitted'];
//res2 returns 2014-08-31 12:03:02
$res3 = $_data['UserIp'];
//res3 returns 109.173.20.143 for example
$resdate = date('Y-m-d H:i:s', strtotime($res2));
$SubmId = $db->query("SELECT `SubmissionId` FROM `rrr_submissions` WHERE `FormId` = '20' AND `DateSubmitted`='".$resdate."' AND `UserIp`='".$res3."'");
$db->setQuery("UPDATE `rrr_submission_values` SET `FieldValue` = '".$SubmId."' WHERE `FieldName`='7_Status' AND `SubmissionId`='5682'");
$db->query();
In rrr_submissions:
SubmissionId is INT(11) AUTO_INCREMENT
FormId is INT(11)
DateSubmitted is datetime
UserIp is varchar(15)
In rrr_submission_values:
FieldValue is text
FieldName is text
SubmissionId is int(11)
What am I do wrong? In the result I see "1" in the FieldValue.

Try loading the result row and then calling the field value.
$db = JFactory::getDBO();
$res2 = $_data['DateSubmitted'];
//res2 returns 2014-08-31 12:03:02
$res3 = $_data['UserIp'];
//res3 returns 109.173.20.143 for example
$resdate = date('Y-m-d H:i:s', strtotime($_data['DateSubmitted']));
$SubmId = $db->query("SELECT `SubmissionId` FROM `rrr_submissions` WHERE `FormId` = '20' AND `DateSubmitted`='".$resdate."' AND `UserIp`='".$res3."'");
$row = $db->loadRow();
$SubmId = $row['SubmissionId'];
$db->setQuery("UPDATE `rrr_submission_values` SET `FieldValue` = '".$SubmId."' WHERE `FieldName`='7_Status' AND `SubmissionId`='5682'");
$db->query();

Related

Set last login date

I use this code but its don`t work :/
$date = date('Y-m-d');
$update = DB::getInstance();
$update = ("UPDATE `users` SET `last_login` = '$date' WHERE `userid` = '" .$user->data()->id. "'");
You didn't execute your query. And you have a big mistake.
$date = date('Y-m-d');
$update = DB::getInstance(); #You created an instance
$update = ("UPDATE `users` SET `last_login` = '$date' WHERE `userid` = '" .$user->data()->id. "'"); #You assigned Query in the instance, This is the big mistake
You should query like below
$date = date('Y-m-d');
$update = DB::getInstance();
$query = ("UPDATE `users` SET `last_login` = '$date' WHERE `userid` = '" .$user->data()->id. "'");
$update->YOUR_EXECUTE_FUNCTION($query);
Note: Here YOUR_EXECUTE_FUNCTION() is only for example purpose. Change it to your execute function
try this:
$date = date('Y-m-d');
$update = DB::getInstance();
$query = "UPDATE `users` SET `last_login` = '" . $date . "' WHERE `userid` = '" .$user->data()->id. "'";
$update->execute($query);
Try to use like this :
$date = date('Y-m-d');
$update = DB::getInstance();
$query = "UPDATE users SET last_login = $date WHERE userid = $user->data()->id";
$update->yourFunctionToUpdateHere($query);

How to update a row in sql with loop?

I'm trying to figure how to call a query once.
I have 6 different variables for images, title and desc.
In this code, I need to know how to loop for id from 0 to 6.
$date = new DateTime("NOW");
$image1 = 'SSSS';
$title1 = 'AAAA';
$desc1 = 'BBBB';
$image2 = 'RRRR';
$title2 = 'GGGG';
$desc2 = 'VVVV';
/// 4 vars later....
$id = 6;
$get = $this->db->queryRow("UPDATE `featured` SET `image` = '{$image.$id}', `title` = '{$title.$id}', `desc` = '{$desc.$id}', `date` = '{$date->format('Y-m-d H:i:s')}' WHERE id = '{$id}'");
return(object) $get;
To build a collection of Querys use the multi_query function.
Loop to build your Query string to pass to the db and concatenated by a semicolon.
<?php
for($i=0;$i <= $maxquerys;$i++){
$query = "UPDATE `featured` SET `image` = '".$image.$id."', `title` = ".$title.$id."', `desc` = '".$desc.$id."', `date` = '".$date->format('Y-m-d H:i:s')."' WHERE id = '".$id."';"
}
/* execute multi query */
if ($mysqli->multi_query($query)) {
while ($mysqli->next_result());
}
/* close connection */
$mysqli->close();
you may check also the result by
echo $mysqli->affected_rows;
?>
I have tried to use a simple $query model and it works fine.
Create a valid Query string to pass to the db
<?php
$query = "UPDATE `featured` SET `image` = '".$image.$id."', `title` = ".$title.$id."', `desc` = '".$desc.$id}."', `date` = '".$date->format('Y-m-d H:i:s')."' WHERE id = '".$id."';"
$result=$mysqli->query($query);
// Verify results
if(!$result) {
$ErrMessage = "ErrSqlQuery:" . $mysqli->error . "\n";
$mysqli->close();
die($ErrMessage);
}
you can check also the result by
echo $mysqli->affected_rows;
?>
$query_build = "";
foreach($arr as $$image){
$query_build .= "UPDATE `featured` SET `image` = '{$image.$id}', `title` = '{$title.$id}', `desc` = '{$desc.$id}', `date` = '{$date->format('Y-m-d H:i:s')}' WHERE id = '{$id}';";
}
$get = $this->db->queryRow($query_build);
Accumulate all the queries and execute all at once.

Error in your SQL syntax. Update table issue

I really hate that error message, since it is the most useless error message in the history of man.
Anyhow I think I have stared at this VERY simple sql for an hour and still come up blank as to where it believes the problem is. Hope someone can help me or maybe some rubber ducking will do the trick.
The php code:
$sql = "UPDATE events SET titel = '$this->estart',
endTime = '$this->eend',
desc = '$this->desc',
dd = '$this->dDmed',
dato = '$this->dato',
ticketId = '$this->ticket' WHERE id = $this->id";
And the SQL error thrown:
right syntax to use near 'desc = '2222222222222222',
dd = 'shop',
dato = '2015-01-14' at line 3[ UPDATE events SET titel = '08:30:00',
endTime = '09:00:00',
desc = '2222222222222222',
dd = 'shop',
dato = '2015-01-14',
ticketId = '2222222222222' WHERE id = 4]' in ....
the table layout:
id int(11)
titel varchar(200)
navn varchar(200)
email varchar(255)
tlf varchar(20)
domæne varchar(150)
kundNumb int(14)
abnId varchar(15)
startTime time
endTime time
desc text
ticketId varchar(20)
dd varchar(5)
dato date
Hope someone can assist, I am sick and tired of that sql statement.
The problem is with the desc field. DESC is a keyword in the SQL syntax. So you will have to quote desc with back-ticks like this `desc`.
UPDATE events SET
`titel` = '$this->estart',
`endTime` = '$this->eend',
`desc` = '$this->desc',
`dd` = '$this->dDmed',
`dato` = '$this->dato',
`ticketId` = '$this->ticket'
WHERE id = $this->id
You missed a comma:
$sql = "UPDATE events SET titel = '$this->estart',
endTime = '$this->eend',
desc = '$this->desc',
dd = '$this->dDmed',
dato = '$this->dato',
ticketId = '$this->ticket' WHERE id = $this->id";
I believe changing
ticketId = '$this->ticket'
to
ticketId = $this->ticket
should work..
Seems Error is in ticket_ID i think you are using ticket_id as number type or integer type and passing it as string '$this->ticket' remove '' and keep it as $this->ticket
you miss a comma, and some " and '. :
"UPDATE events SET titel = '".$this->estart."',
endTime = '".$this->eend."',
desc = '".$this->desc."',
dd = '".$this->dDmed."',
dato = '".$this->dato."',
ticketId = '".$this->ticket."' WHERE id = ".$this->id.";

MySQL update column only if value not empty where

I have an UPDATE query and using Ajax, I wanted to know if any value is empty can I only update the values that not empty in the database. I don't know if this is possible to have a if statement or something to check to skip the empty values. I know I can just add another form element but just wanted to know if there was another solution.
Only if the data is POST from front end form. If data not POST don't update this Title = '.$title .',
$id = $_POST['id'];
$title = "";
$description = $_POST['Description'];
$date = $_POST['Date'];
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = '.$title .',
`Description` = '.$description.',
`Date` = '.$date =.'
WHERE `id` = '.$id;
$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query: ".$query."<br />Error: (".mysql_errno().") ".mysql_error());
Update: This is what worked for me. Thanks Karim Daraf
$query = " UPDATE user SET
Title = Coalesce($title,Title ) etc...
Try it with Coalesce .
$query = " UPDATE user
SET
`Title` = CASE WHEN `Title`='' or `Title` IS NULL THEN '$title' END,
`Description` = CASE WHEN `Description`='' Or `Description` IS NULL THEN '$description' END,
`Date` = CASE WHEN `Date`='' Or Date` IS NULL THEN '$date' END
WHERE `id` = '".$id."' ";
or :
$query = " UPDATE user
SET
`id` = Coalesce('$id''".$id."' , NULLIF(`id`,'')),
`Title` = Coalesce('$title''".$title."',NULLIF(`Title`,'') ) ,
`Description` = Coalesce('$description''".$description."' , NULLIF(`Description`,'') ) ,
`Date` = Coalesce('$date''".$date."',NULLIF(`Date`,''))
WHERE `id` = '$id''".$id."' ";
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = COALESCE(NULLIF("'.$title.'", ""),`Title`),
`Description` = "'.$description.'",
`Date` = "'.$date.'"
WHERE `id` = "'.$id.'"';
Not sure to understand: you have data and want to update, but only if some fied in the DB are empty?
In the case perfom only a where:
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = '.$title .',
`Description` = '.$description.',
`Date` = '.$date =.'
WHERE `id` = '.$id.' AND Title = '';
for example

Cannot save php datetime in database

I want to save a datetime from PHP into my database, but I cannot. The datetime value in the database is always set as 0000-00-00 00:00:00. What am I doing wrong?
My code:
$today = new DateTime();
$dt = $today->format('Y-m-d H:i:s');
$sql = "UPDATE wp_posts SET post_date = $dt, post_date_gmt = $dt WHERE ID = $id";
try put quotation marks:
$today = new DateTime();
$dt = $today->format('Y-m-d H:i:s');
$sql = "UPDATE wp_posts SET post_date = '$dt', post_date_gmt = '$dt' WHERE ID = $id";
Simply, datetime is string, not integer.
$today = new DateTime();
$dt = $today->format('Y-m-d H:i:s');
$sql = "UPDATE wp_posts SET post_date = '$dt', post_date_gmt = $dt WHERE ID = $id";
will be works better:)
You can just use,
$dt = date("Y-m-d H:i:S");
$sql = "UPDATE wp_posts SET post_date = '$dt', post_date_gmt = '$dt' WHERE ID = $id";
Instead of using date time from PHP you should use mysql inbuit now() function, no need to write extra line of code in php.
$sql = "UPDATE wp_posts SET post_date = now(), post_date_gmt = now() WHERE ID = $id";
n you can alos set Now() in your table field so you dont need to set as query , when record will insert to update it will automatically set the current date time.
CREATE TABLE tablename
(
fiedlId int NOT NULL,
fieldName varchar(50) NOT NULL,
fieldDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (fiedlId)
)
Cheers!!
why don't you use timestamp function of mysql
for new table
$sql = CREATE TABLE newtb (current_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
or
$sql = CREATE TABLE newtb (current_time DATETIME DEFAULT NOW());
to modify Table
$sql = CREATE TABLE newtb MODIFY current_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
or
$sql = CREATE TABLE newtb MODIFY current_time DATETIME DEFAULT NOE();

Categories