Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Zapis do bazy formularza.
pawel06281990
post 18.01.2022, 20:50:48
Post #1





Grupa: Zarejestrowani
Postów: 281
Pomógł: 0
Dołączył: 10.01.2014

Ostrzeżenie: (0%)
-----


Witam,

W jaki sposób zapisać taki rząd formularza do bazy.



Zacząłem robić pętlę w array ale nie zapisuje mi tak jak ja chce


  1. $sektor = esc_sql($_POST['img_sektort']);
  2. $rzad = esc_sql($_POST['img_rzad']);
  3. $coords = esc_sql($_POST['img_coords']);
  4. $url = esc_sql($_POST['img_href']);
  5. $alt = esc_sql($_POST['img_alt']);
  6. $shape = esc_sql($_POST['img_shape']);
  7. $wartość = [
  8. 'sektor'=> $sektor,
  9. 'rzad'=>$rzad,
  10. 'coords'=>$coords,
  11. 'url'=>$url,
  12. 'alt' =>$alt,
  13. 'shape' =>$shape];
  14. foreach ($wartość as $data ){
  15.  
  16.  
  17. $wpdb->insert($wpdb->prefix.'generator_map', array('sektor'=> $data['sektor'], 'rzad'=> $data['rzad'], 'coords'=> $data['coords'], 'url'=> $data['url'], 'alt' =>$data['alt'], 'shape' => $data['shape'] ));
  18. }


I nie mogę zrobić żeby mi się zapisywało wedle tego co podaje przez $_POST.
Go to the top of the page
+Quote Post
nospor
post 19.01.2022, 10:16:10
Post #2





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6290
Dołączył: 27.12.2004




A po grzyba ta petla tam?

Czemu poprostu nie
$wpdb->insert($wpdb->prefix.'generator_map', array('sektor'=> $sektor, 'rzad'=> $rzad, 'coords'=> $coords, 'url'=> $url, 'alt' =>$alt, 'shape' => $shape));
?


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
pawel06281990
post 19.01.2022, 11:39:53
Post #3





Grupa: Zarejestrowani
Postów: 281
Pomógł: 0
Dołączył: 10.01.2014

Ostrzeżenie: (0%)
-----


Cytat(nospor @ 19.01.2022, 10:16:10 ) *
A po grzyba ta petla tam?

Czemu poprostu nie
$wpdb->insert($wpdb->prefix.'generator_map', array('sektor'=> $sektor, 'rzad'=> $rzad, 'coords'=> $coords, 'url'=> $url, 'alt' =>$alt, 'shape' => $shape));
?


Bo mam w tedy jeden rząd zapisu formularza do bazy.

A ja musze zapisać kilka formularzy do bazy jednoczesnie.
Go to the top of the page
+Quote Post
nospor
post 19.01.2022, 12:59:28
Post #4





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6290
Dołączył: 27.12.2004




To pokaz dokladnie kod formularzy a nie jakis obrazek ktory nic nie mowi. Ciezko wyczaic co ty tam masz

ps: jesli ty faktycznie i DOSLOWNIE masz na mysli, ze wysylasz kilka formularzy to nie, formularz jest wysylany ZAWSZE tylko jeden.
Wiec zamiast kilku formularzy, poprostu zrob wiersze w jednym formularzu z tablicowa nazwa pol


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
pawel06281990
post 19.01.2022, 18:13:59
Post #5





Grupa: Zarejestrowani
Postów: 281
Pomógł: 0
Dołączył: 10.01.2014

Ostrzeżenie: (0%)
-----


Tak wygląda formularz w js
[JAVASCRIPT] pobierz, plaintext
  1. function gui_addArea(id) {
  2. //var id = props.length;
  3. //id = 1;
  4. props[id] = document.createElement('DIV');
  5. document.getElementById('form_container').appendChild(props[id]);
  6.  
  7. props[id].id = 'img_area_' + id;
  8. props[id].aid = id;
  9. props[id].className = 'img_area';
  10. //hook ROW event handlers
  11. myimgmap.addEvent(props[id], 'mouseover', gui_row_mouseover);
  12. myimgmap.addEvent(props[id], 'mouseout', gui_row_mouseout);
  13. myimgmap.addEvent(props[id], 'click', gui_row_click);
  14. var temp = '<input type="text" name="img_id" class="img_id" value="' + id + '" readonly="1"/>';
  15. //temp+= '<input type="checkbox" name="img_active" class="img_active" id="img_active_'+id+'" value="'+id+'">';
  16. //could be checkbox in the future
  17. temp+= '<input type="radio" name="img_active" class="img_active" id="img_active_'+id+'" value="'+id+'">';
  18. temp+= '<select name="img_shape" class="img_shape">';
  19. temp+= '<option value="rect">rectangle</option>';
  20. if (document.getElementById('dd_output').value != 'css') {
  21. temp+= '<option value="circle">circle</option>';
  22. temp+= '<option value="poly">polygon</option>';
  23. temp+= '<option value="bezier1">bezier</option>';
  24. }
  25. temp+= '</select>';
  26. temp+= 'Współrzędne: <input type="text" name="img_coords" class="img_coords" value="">';
  27. temp+= 'URL: <input type="text" name="img_href" class="img_href" value="#">';
  28. temp+= 'Alt: <input type="text" name="img_alt" class="img_alt" value="">';
  29. temp+= 'Sektor: <input type="text" name="img_sektor" class="img_sektor" value="">';
  30. temp+= 'Rząd <input type="text" name="img_rzad" class="img_rzad" value="">';
  31.  
  32. //temp+= 'Sektor <select name="img_sektor" class="img_target">';
  33.  
  34. //temp+= '<option value="">&lt;not set&gt;</option>';
  35.  
  36. //temp+= '<option value="_self" >this window</option>';
  37. //temp+= '<option value="_blank" >new window</option>';
  38. //temp+= '<option value="_top" >top window</option>';
  39. //temp+= '</select>';
  40. props[id].innerHTML = temp;
  41. //hook more event handlers to individual inputs
  42.  
  43. myimgmap.addEvent(props[id].getElementsByTagName('input')[1], 'keydown', gui_cb_keydown);
  44. myimgmap.addEvent(props[id].getElementsByTagName('input')[2], 'keydown', gui_coords_keydown);
  45. myimgmap.addEvent(props[id].getElementsByTagName('input')[2], 'change', gui_input_change);
  46. myimgmap.addEvent(props[id].getElementsByTagName('input')[3], 'change', gui_input_change);
  47. myimgmap.addEvent(props[id].getElementsByTagName('input')[4], 'change', gui_input_change);
  48. myimgmap.addEvent(props[id].getElementsByTagName('select')[0], 'change', gui_input_change);
  49. //myimgmap.addEvent(props[id].getElementsByTagName('select')[1], 'change', gui_input_change);
  50. if (myimgmap.isSafari) {
  51. //need these for safari
  52. myimgmap.addEvent(props[id].getElementsByTagName('select')[0], 'change', gui_row_click);
  53. //myimgmap.addEvent(props[id].getElementsByTagName('select')[1], 'change', gui_row_click);
  54. }
  55. //set shape as nextshape if set
  56. if (myimgmap.nextShape) {props[id].getElementsByTagName('select')[0].value = myimgmap.nextShape;}
  57. //alert(this.props[id].parentNode.innerHTML);
  58. gui_row_select(id, true);
  59. }
[JAVASCRIPT] pobierz, plaintext


I jak wypełnię pierwsze pola w input to dodają się kolejne, bo to generator jest do map
Go to the top of the page
+Quote Post
nospor
post 19.01.2022, 18:26:24
Post #6





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6290
Dołączył: 27.12.2004




Zakladajac ze to wszystko dodajesz do jednego forma, to tak jak mowilem wczesniej, pola maja miec nazwe tablicowa czyli nie
name="img_href"

a
name="img_href[]"

Podobnie reszta. Jak to zrobisz to sie przyjrzyc co ci idzie postem a moze zrozumiesz czemu tak mialo byc

print_r($_POST);


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
pawel06281990
post 19.01.2022, 20:20:58
Post #7





Grupa: Zarejestrowani
Postów: 281
Pomógł: 0
Dołączył: 10.01.2014

Ostrzeżenie: (0%)
-----


Dostałem taki wynik dodająć print_r($_POST);

  1. Array ( [img_id] => Array ( [0] => 0 [1] => 1 )
  2. [img_shape] => Array ( [0] => rect [1] => rect )
  3. [img_coords] => Array ( [0] => 185,112,219,123 [1] => 223,111,260,122 )
  4. [img_href] => Array ( [0] => # [1] => # )
  5. [img_alt] => Array ( [0] => A_I_2 [1] => A_I_45 )
  6. [img_sektor] => Array ( [0] => A [1] => A )
  7. [img_rzad] => Array ( [0] => I [1] => I )
  8. [img_active] => Array ( [0] => 1 )
  9. [action] => add_generator_map )


Teraz rozumiem.

Ale teraz zapis bo post podaje jako array wszystkie pola i muszę przetworzyć to w zapis.

Zalazłem rozwiązanie mojego problemu, ale nawiną się następny problem

Jesli zrobię tak
  1. foreach ($_POST as $val) {
  2. for( $x = 0; $x < 5; $x++ ) {
  3. print_r($val[$x]);
  4. }
  5.  
  6. }


Dostaje wszystkie wyniki w tej formie

01rectrect185,112,219,123223,111,260,122##A_I_2A_I_45AAII

I moje pytanie brzmi, jak mam to posegregować w odpowiednie tabele w bazie danych przy zapisie

Tak to powinno być przy zapisie
  1. $wpdb->insert($wpdb->prefix.'generator_map', array('sektor'=> A , 'rzad'=> I, 'coords'=> 223,111,260,122, 'url'=># , 'alt' =>A_I_45, 'shape' =>rect ));
  2.  



Cytat(pawel06281990 @ 19.01.2022, 19:44:51 ) *
Dostałem taki wynik dodająć print_r($_POST);

  1. Array ( [img_id] => Array ( [0] => 0 [1] => 1 )
  2. [img_shape] => Array ( [0] => rect [1] => rect )
  3. [img_coords] => Array ( [0] => 185,112,219,123 [1] => 223,111,260,122 )
  4. [img_href] => Array ( [0] => # [1] => # )
  5. [img_alt] => Array ( [0] => A_I_2 [1] => A_I_45 )
  6. [img_sektor] => Array ( [0] => A [1] => A )
  7. [img_rzad] => Array ( [0] => I [1] => I )
  8. [img_active] => Array ( [0] => 1 )
  9. [action] => add_generator_map )


Teraz rozumiem.

Ale teraz zapis bo post podaje jako array wszystkie pola i muszę przetworzyć to w zapis.

Zalazłem rozwiązanie mojego problemu, ale nawiną się następny problem

Jesli zrobię tak
  1. foreach ($_POST as $val) {
  2. for( $x = 0; $x < 5; $x++ ) {
  3. print_r($val[$x]);
  4. }
  5.  
  6. }


Dostaje wszystkie wyniki w tej formie

01rectrect185,112,219,123223,111,260,122##A_I_2A_I_45AAII

I moje pytanie brzmi, jak mam to posegregować w odpowiednie tabele w bazie danych przy zapisie

Tak to powinno być przy zapisie
  1. $wpdb->insert($wpdb->prefix.'generator_map', array('sektor'=> A , 'rzad'=> I, 'coords'=> 223,111,260,122, 'url'=># , 'alt' =>A_I_45, 'shape' =>rect ));
  2.  



Ok już zrobiłem tak jak powinno być dodając to w ten sposób

  1. for( $x = 0; $x < 5; $x++ ) {
  2.  
  3. $wpdb->insert($wpdb->prefix.'generator_map', array('sektor'=> $_POST['img_sektor'][$x], 'rzad'=> $_POST['img_rzad'][$x], 'coords'=> $_POST['img_coords'][$x], 'url'=> $_POST['img_href'][$x], 'alt' =>$_POST['img_alt'][$x], 'shape' => $_POST['img_shape'][$x] ));
  4.  
  5. }


I dział zapisuje mi tak jak chciałem.

Ten post edytował pawel06281990 19.01.2022, 19:09:07
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 23:25