Trouble with some SQL core code

Events happening in the community are now at Drupal community events on www.drupal.org.
damodarko's picture

Hi Guys/Gals,

I'm a student in DCU struggling to get the last parts of my 3rd year project in place. Here is a link to the test area of my site that I am having problems with (http://www.redbrick.dcu.ie/~dano/drupal/?q=examples/test_form). You shouldn't need a login for now. The problem is in my .inc file, I cant seem to use the "insert into" command properly with the AHAH submit button I have. The query instead inserts a row of blank values because of what I can only assume is me not referencing the textfield values properly. I'm sure its something simple that I'm just not seeing at this late stage in code, I would really appreciate any help as my submission deadline is late tomorrow. Here is a pastebin of my code. (http://pastebin.com/Z9KQ5aqk).

Thanks you for your time :)

Comments

Does the submit button have

stella's picture

Does the submit button have to be ahah too or just the 'add more' button? It'd be better to use a non-ahah submit button and use a submit form handler instead. That way you could also avail of the form validation handlers too if you wanted.

Ignoring that, you are getting empty values in your table because you are inserting a variable ($node->Iname) which doesn't exist. Not sure where you got $node from but it's not a node form by the looks of it (difficult to tell without seeing full code, but that's the incorrect definition of it if it is) and it's not a variable passed into or set in that function either.

You're already checking the correct place for the value of the submit button ($form_state['values']['op']) so you know how to access the form values already - $form_state['values'] array.

Hope that helps.

Thanks

damodarko's picture

Hey Stella,
Thanks so much, completely switched on the idea bulb there and have it working in full swing! :D
Damien

Trouble in Insert Paradise again

damodarko's picture

Hi, sorry to be clogging the forums with my questions. Below are two snippets of code, the first one works, however the second one is the one I am interested in. The second allows a user to create multiple boxes to insert an input into but I'm having trouble referencing the multiple inputs.

EXAMPLE ONE

$form['Rname'] = array(
    '#type' => 'textfield',
    '#title' => t('Recipe Name'),
    '#size' => 36,
    '#default_value' => $form_state['values']['Rname'],
);

after submit is clicked

$dish = $form_state['values']['Rname'];
db_query("INSERT INTO {recipe} (Rname) VALUES ('%s')", $dish);

EXAMPLE TWO

    for ($i = 1; $i <= $quantity; $i++) {

// The element name needs to be different for each textfield, otherwise we will only get one value after the form is submitted.
$form['Iname']['Iname'. $i] = array(
'#type' => 'textfield',
'#title' => t('Ingredient'),
'#size' => 36,
'#default_value' => $form_state['values']['Iname']['Iname'. $i],
);

after submit is clicked

    $Ingred = $form_state['values']['Iname']['Iname.$i'];              //This seems to be the problem
    db_query("INSERT INTO {ingredient} (Iname) VALUES ('%s')", $ingred);

When I make $Ingred = $form_state['values']['Iname'], the insert works but only gives me the first item in the array. The addition of the 3rd array that expands with the addition of boxes should act as a marker to each new textfield but its not, it returns blanks rows (I've tried a for loop around the insert as well).

I am also wondering is there a way to manually assign and NID, VID and UID without creating a node?

again sorry for being a pain.

Trouble in Insert Paradise again

damodarko's picture

Hi, sorry to be clogging the forums with my questions. Below are two snippets of code, the first one works, however the second one is the one I am interested in. The second allows a user to create multiple boxes to insert an input into but I'm having trouble referencing the multiple inputs.

EXAMPLE ONE

$form['Rname'] = array(
    '#type' => 'textfield',
    '#title' => t('Recipe Name'),
    '#size' => 36,
    '#default_value' => $form_state['values']['Rname'],
);

after submit is clicked

$dish = $form_state['values']['Rname'];
db_query("INSERT INTO {recipe} (Rname) VALUES ('%s')", $dish);

EXAMPLE TWO

    for ($i = 1; $i <= $quantity; $i++) {

// The element name needs to be different for each textfield, otherwise we will only get one value after the form is submitted.
$form['Iname']['Iname'. $i] = array(
'#type' => 'textfield',
'#title' => t('Ingredient'),
'#size' => 36,
'#default_value' => $form_state['values']['Iname']['Iname'. $i],
);

after submit is clicked

    $Ingred = $form_state['values']['Iname']['Iname.$i'];              //This seems to be the problem
    db_query("INSERT INTO {ingredient} (Iname) VALUES ('%s')", $ingred);

When I make $Ingred = $form_state['values']['Iname'], the insert works but only gives me the first item in the array. The addition of the 3rd array that expands with the addition of boxes should act as a marker to each new textfield but its not, it returns blanks rows (I've tried a for loop around the insert as well).

I am also wondering is there a way to manually assign and NID, VID and UID without creating a node?

again sorry for being a pain.

Solved Main Problem

damodarko's picture

Apologies again, the multiple textfield problem was solved. I would still like to know if it is possible to set NID and the like manually.

No, you really shouldn't be

stella's picture

No, you really shouldn't be setting the nid or vid manually. You can alter the uid by changing the user in the 'authored by' textfield which you see in the 'authoring information' collapsed fieldset if you have the 'administer nodes' permission (this permission is only for trusted users!)

Ireland

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: