3

The code I wrote that uses db_insert() and db_merge() works great. I can populate the database table well (also using a post http). Now I want to get a success/fail message.

How do I show a message after executing a query?

closed as too broad by kiamlaluno Apr 19 '18 at 16:17

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I've tried to count rows before and after insert (merge) and compare if difference but there's a problem somewhere (it always shows : user exists (even if it's not true) and the insert works after that message (or before) !!) – Mitch Jul 30 '15 at 8:32
3

You show messages using drupal_set_message():

Sets a message to display to the user.

Messages are stored in a session variable and displayed in page.tpl.php via the $messages theme variable.

  • the thing is I know how to show messages, I want just to show them in a condition (if insert or not) – Mitch Jul 30 '15 at 8:43
  • 4
    Use a try...catch around the db_insert, that's the best way – Clive Jul 30 '15 at 8:50
3

You can use drupal_set_message() to display a message in the browser, this can be marked as a status', 'warning' or 'error' message.

If you want to debug your code, the Devel module is recommended and provides the dpm() function. Besides being able to print a simple message, dpm() also allows to display an array or object quite easy.

2

If you want to know the status of the insert, perhaps a more suitable function for your use case is drupal_write_record

Return value

If the record insert or update failed, returns FALSE. If it succeeded, returns SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

To update a record, you need to have the 'primary key field name' => 'value' in the $record (2nd) parameter passed to the function. As well as that, you need to pass the $primary_keys (3rd) parameter which is a simple array of your primary key field names without the values. If it's just a single primary key (id for example), then you can pass it as a string as well.

If you omit the 3rd parameter $primary_keys then it's going to perform a db_insert otherwise it does a db_update.

In fact, as you follow the function along, it shows you exactly how to evaluate whether the db_insert or db_update were a success or not. So I won't be covering it here.

  • You mean i've te replace the db_insert and db_merge by drupal_write_record ? – Mitch Jul 30 '15 at 9:37
  • I've just replace db_insert by drupal_write_record, but it insert even if the actual record exists on database (duplicating problem) did you know how to avoid this ? any condition ? – Mitch Jul 30 '15 at 10:16
  • @Mitch but it insert even if the actual record exists on database means that you haven't got your primary keys quite right. If you had, you would simply receive an exception for trying to insert into a duplicate into a column with a unique constraint – Clive Jul 30 '15 at 10:57
  • The primary key i used is a serial key so it's not a good key to make difference between two records (even if it's the same record) because this pk is auto increment so those records are considered as "different" just because id is different (but in fact it's the same record) how to put another key to be "the difference maker" ?? (like key for db_merge) – Mitch Jul 30 '15 at 11:50
  • @Mitch check the edit, hope I understood you correctly, and it's clear. – Beebee Jul 30 '15 at 12:35
0

by the way I've solved my problem by try catch as @clive adviced me on comment,

Thedrupal_write_record works as well so thank you Beebee

If it can helps, here is the part of code with the try catch :

try{
$uid_courant = db_query("SELECT (MAX(uid))+1 as max_id FROM {users}")->fetchCol();
$create_user = db_merge('users')
    ->key(array('name'=> $data["username_client"],'uid'=>'uid'))
    ->fields(array(
        'uid'=> $uid_courant,
        'name'=>$data["username_client"],
        'pass'=>$data['password_cl'],
        'mail'=>$data["mail_cl"],
        'created'=>time()
        ))
->execute();
}
catch(Exception $e){
$user_existe = array('code'=>'1','error'=>'1');
}

Here the code insert a new user only if there's no user with same username on table "users", if not it returns an error. and in the same time i'm inserting the same user on anotre custom table i've created this table is called "client" and do same verifications.

Thank you all.

Not the answer you're looking for? Browse other questions tagged or ask your own question.