im using hook form alter in drupal.
If the fields left empty I want it to get the last value submitted for the field and increase it by .01
Ive tried
function uc_pa_form_submit($form, &$form_state) {
global $user;
$maxbid = db_result(db_query('SELECT MAX(amount) FROM {uc_auction_bids} WHERE nid = %d', $node->nid));
$input01 = (($maxbid) ? $maxbid : 0) + .01;
drupal_write_record('table', $input01);
but it isnt updating with anything, I know $input01 works as I tried it in a different function.
if i change drupal_write_record('table', $input01); to the value submitted it works.
drupal_write_record() expects the second argument to be an object, at the moment you're passing a number. Also, if you're trying to update a record you'll need to provide the table's primary keys as the third argument. Something like this:
$sql = 'SELECT * FROM FROM {uc_auction_bids} WHERE nid = %d ORDER BY amount DESC LIMIT 1';
$obj = db_fetch_object(db_query($sql));
$obj->amount = (($obj->amount) ? $obj->amount: 0) + .01;
drupal_write_record('uc_auction_bids', $obj, array('bid'));
I'm new here and wish I could just comment rather than say I'm answering anything...
Anyway, what is $maxbid? It doesn't exist in the function before the comparison. Is it a global? Or should $input be $maxbid (or vice-versa) in your code sample?
Related
So I was trying to make a "create new user" function in PHP which should basically check in the database which is the lowest possible ID to assign to that new user. But I have now tried around with so many different methods and they all do not work as they should. Here is my current version:
function newUser($connection) {
$notNewId = sqlsrv_query ($connection, "SELECT id FROM users"); //get id from users table
while($notNewId2 = sqlsrv_fetch_array ($notNewId)) {
for ($i = 0; $i <= sizeOf($notNewId2); $i++) {
foreach ($notNewId2 as $key => $value ) {
if ($i != $value) {
break;
}
}
}
$id = $i;
return $id;
}
}
the $connection is a element of type sqlsrv_connect.
as far as I can tell my current version should be able to read the ids and put them in an array, but from there on something went wrong. As well... I may have to sort the array after id, but I have no clue how to do that.
I would really appreciate any help, even if it's no actual code and just the logic explained, thx.
EDIT: Seems like it is not clear enough, what I want. My script should assign a new ID to the new row in the users table, if that is possible automatically somehow with SQL, then please explain to me how. (Right now ID is not a primary key, I will change that as soon as I can)
Taking the highest number and adding one is not enough (like when I have 0, 1, 2, 4 and 5, the new ID should be 3, not 6). But still thanks, I didn't knew about that MAX thing.
Assuming id is your primary key and you're not trying to auto increment it in your users table, you can find the maximum value of it and add 1 in your SQL query. This mitigates the need for your nested for loops. Use ISNULL to check if that value is not null and default to 1 otherwise.
function newUser($connection) {
$query = sqlsrv_query ($connection, "SELECT ISNULL(MAX(id)+1, 1) FROM users");
return sqlsrv_fetch($query);
}
I know that varchar type of columns are not incrementable, but I want to do something like this:
When a duplicated value is inserted, the new one will be tagged with a number at the end.
For example, we have post-name on our column in the database. Another value post-name is entered, so the next is going to be post-name-2, followed by post-name-3.
I have programmed something in php but its not very convenient.
$post_url_ = $post_url." %";
$stmt_check1 = $this->conn->prepare("SELECT * FROM post WHERE post_url LIKE :post_url ");
$stmt_check2 = $this->conn->prepare("SELECT * FROM post WHERE post_url = :post_url ");
$stmt_check1->bindparam(":post_url",$post_url_);
$stmt_check2->bindparam(":post_url",$post_url);
$stmt_check1->execute();
$stmt_check2->execute();
$rows1 = $stmt_check1->rowCount();
$rows2 = $stmt_check2->rowCount();
if($rows1<=0 && $rows2==1) {
$repeat_no = $rows1+1;
$post_url = $post_url."-$repeat_no";
}
if($rows1>0){
$repeat_no = $rows1+1;
$post_url = $post_url."-$repeat_no";
}
Instead of trying to create a complicate process to keep the correct name, just add separated field version
Then for UI proporse just concatenate both
SELECT CONCAT(post , '-', version)
You can use do...while loop to check next numbers. It isn't performance problem, becuase you will use that only on save new record.
Before loop, create counter (initial by 0).
In loop - if counter > 0, add number to end of url
Increase counter
Check if url exists in database (while results from db > 0)
i have an issue gettin' the value from a query:
public static function sa() {
$resul = Yii::app()->db->createCommand()->select('MAX(id)')->from('yii_availability')->execute();
$got = mysql_query($result);
$res = $got['MAX(id)'] + 1;
$rs='SA'.$res;
return "{$rs}";
}
it always return SA1, but i want get the last id and after plus 1 so in this case i have the next autoincremental id from my id column.
For example: i am creating a new registry with the field SA0000005. This number is calculated getting the last autoincremental value plus 1.
thanks for your valuable help
$resul = Yii::app()->db->createCommand()->select('MAX(id)')->from('yii_availability')->execute();
$got = mysql_query($result); // what are you even doing here
Apart from typos, that's not how you are supposed to use the query builder. Have you read the documentation on the query builder?
The probable reason why you always get SA1, is because the $got['MAX(id)'] expression is NULL. You add 1 to that. You want something like this.
// returns false on no-result, MAX('id') otherwise
Yii::app()->db->createCommand()->select('MAX(id)')->from('yii_availability')->queryScalar();
I was wondering if there was a way to echo out the full query with limit and limitstart etc. I can echo out the line $query, but i want to see why the limit isn't working and I can't seem to get it to display the actual query that it's sending to the database.. Here's the code:
$params =& JComponentHelper::getParams('com_news');
$limit = $params->get('x_items', 5);
$limitstart = JRequest::getVar('limitstart', 0);
$query = "SELECT * FROM #__news WHERE published = 1 AND catid = ".$Itemid." ORDER BY date DESC";
$db->setQuery($query, $limitstart, $limit);
$rows = $db->loadObjectList();
$db->getQuery($query, $limitstart, $limit); is only displaying "SELECT * FROM jos_news WHERE published = 1 AND catid = 8 ORDER BY date DESC" which doesnt have the LIMIT params on the end of the query..
Any help would be appreciated :)
The JDatabaseQuery object has a __toString() function that outputs the query so you can do:
echo $db->getQuery();
Or if you want to pass it to a function you can explicitly cast it to a string first:
var_dump((string)$db->getQuery());
var_dump($db);die;
Do that after the loadObjectList() call. Inside the $db variable there must be a _sql attribute that is the last query executed.
Agreed with the previous answers, but... In case you are developing your own components, since I often want to know for sure what exactly is executed too, here's a simple solution:
In your models put:
$db = JFactory::getDBO();
echo $db->getQuery();
Where you want to know the query... Don't put it in (for example) your view, since it might have loaded some other dropdown list by way of execution in the meantime...
For example:
For a list-view put it right before the foreach ($items... in the public function getItems() of the model.
In a form-/item-view put it right before the return $data / return $item in the protected function loadFormData() / public function getItem($pk = null)
Hope this helps...
On new joomla versions, you need to echo __toString() on query object.
echo $query->__toString();
I get this info on this joomla answer.
Hope this helps
Joomla provides $query->dump(). To add convenience, I like to wrap it in enqueueMessage() so that the presented string is at the top of the webpage.
JFactory::getApplication()->enqueueMessage(
$query->dump(),
'notice'
);
IMPORTANT: You should never show the raw SQL string or a raw query error string to the public.
See some of my implementations at Joomla Stack Exchange.
The scipt should subtract 1 from the primary key (auftrag_id) from a table. But somehow it's not working. The variable $preauftrag_id is not getting decreased. The type of the variable is integer. I tried several subtraction-methods, but none is working. Someone have an idea?
$auftrag_id = $db->insert_id;
$preauftrag_id = $auftrag_id;
$preauftrag_id--;
echo "$preauftrag_id"; // Turns out the same value as $preauftrag_id = $auftrag_id;
Just another thought, if $db->insert_id returns an object, PHP variables assigned by reference.
Therefore, you've essentially assigned another way to access $auftrag_id, but you haven't taken its value:
$auftrag_id $preauftrag_id
\ /
------memory------
(sorry for the terrible graphics)
Therefore, decrementing $preauftrag_id would actually be decrementing the same reference as $auftrag_id, which may not be allowed because its a STATIC variable of the DB class?
Assuming you want the id of the second to last row (the one before the one that was just inserted), the only reliable way is to run a query. Try this:
$query = 'SELECT id
FROM table
WHERE id < '.(int) $db->insert_id.'
ORDER BY id DESC
LIMIT 1';
That should fetch the id of the row prior to the one in question.
In case of, try with integer casting :
$auftrag_id = (int) $db->insert_id;
$preauftrag_id = $auftrag_id;
$preauftrag_id--;
echo "$preauftrag_id"; // Turns out the same value as $preauftrag_id = $auftrag_id;