How to Insert/Edit data from TAG SELECT HTML Form to MySQL Database?

Thread Solved

Join Date: Jul 2009
Posts: 7
Reputation: adindra is an unknown quantity at this point 
Solved Threads: 0
adindra adindra is offline Offline
Newbie Poster

How to Insert/Edit data from TAG SELECT HTML Form to MySQL Database?

 
0
  #1
Jul 9th, 2009
Hello everyone..it's me again..adindra

i have a problem :
i have 1 database name manipulasiphp, with 10 field (id,nama,email,alamat,umur,negara,universitas,jurusan,prodi,no_telp)

i have 1 HTML script and 1 PHP script
they're name are index.html and input.php

here's the script :

index.html
  1. <html>
  2. <head>
  3. <title>Form Calon Peserta Beasiswa Monbugakusho Jepang</title>
  4. </head>
  5. <body>
  6. <form method="post" action="input.php">
  7. <table>
  8. <tr>
  9. <td>Nama</td>
  10. <td>: <input type="text" name="nama"></td>
  11. </tr>
  12. <tr>
  13. <td>Email</td>
  14. <td>: <input type="text" name="email"></td>
  15. </tr>
  16. <tr>
  17. <td>Alamat</td>
  18. <td>: <input type="text" name="alamat"></td>
  19. </tr>
  20. <tr>
  21. <td>Umur</td>
  22. <td>: <input type="text" name="umur" size="2" maxlength="3">tahun</td>
  23. </tr>
  24. <tr>
  25. <td>Negara</td>
  26. <td>: <input type="text" name="negara"></td>
  27. </tr>
  28.  
  29.  
  30.  
  31. <tr>
  32. <td>Universitas</td>
  33. [B][COLOR="Red"]<td>: <select name="universitas[]">
  34. <option value="1">Universitas Gadjah Mada,Yogyakarta</option>
  35. <option value="2">Institut Teknologi Bandung,Bandung</option>
  36. <option value="3">Universitas Indonesia,Jakarta</option>
  37. <option value="4">Institut Teknologi Sepuluh November,Surabaya</option>
  38. <option value="5">Universitas Airlangga,Surabaya</option>
  39. </td>[/COLOR][/B]
  40. </tr>
  41. <tr>
  42. [B][COLOR="Red"]<td>Jurusan :</td>
  43. <td><input type="radio" name="jurusan" value="1">Teknik Fisika
  44. <br><input type="radio" name="jurusan" value="2">Teknik Kimia
  45. <br><input type="radio" name="jurusan" value="3">Teknik Informatika
  46. <br><input type="radio" name="jurusan" value="4">MIPA
  47.  
  48. </td>[/COLOR][/B]
  49. </tr>
  50.  
  51.  
  52.  
  53.  
  54. <tr>
  55. <td>Prodi</td>
  56. <td>: <input type="text" name="prodi"></td>
  57. </tr>
  58. <tr>
  59. <td>No Telpon</td>
  60. <td>: <input type="text" name="notelpon"></td>
  61. </tr>
  62. <tr>
  63. <td><input type="submit" name="submit" value="Daftar Beasiswa"></td>
  64. <td><input type="reset" name="reset" value="Ndak Jadi ahh.."></td>
  65. </tr>
  66. </table>
  67. </form>
  68. </body>
  69. </html>

then input.php

  1. <?php
  2. $host="localhost";
  3. $usnm="root";
  4. $pswd="";
  5.  
  6. $koneksi=mysql_connect("$host","$usnm","$pswd") or die ("Localhost tidak terkoneksi");
  7.  
  8. mysql_select_db("manipulasiphp",$koneksi);
  9.  
  10. $input_nama=$_POST['nama'];
  11. $input_email=$_POST['email'];
  12. $input_alamat=$_POST['alamat'];
  13. $input_umur=$_POST['umur'];
  14. $input_negara=$_POST['negara'];
  15. $input_univ=$_POST['universitas'];
  16. $input_jurusan=$_POST['jurusan'];
  17. $input_prodi=$_POST['prodi'];
  18. $input_no_telp=$_POST['notelpon'];
  19.  
  20. if((!empty($input_nama)) && (!empty($input_email)) && (!empty($input_alamat)) && (!empty($input_umur)) && (!empty($input_negara)) && (!empty($input_univ)) && (!empty($input_jurusan)) && (!empty($input_prodi)) && (!empty($input_no_telp)))
  21. {
  22. $query=mysql_query("INSERT INTO member (nama,email,alamat,umur,negara,universitas,jurusan,prodi,no_telp) VALUES ('$input_nama','$input_email','$input_alamat','$input_umur','$input_negara','$input_univ','$input_jurusan','$input_prodi','$input_no_telp')",$koneksi);
  23. echo "Calon Peserta Beasiswa baru telah terdaftar!!";
  24. echo "<br>";
  25. echo "<br>Untuk melihat peserta lainnya, klik <a href=\"view.php\">disini</a>";
  26. }
  27. else
  28. {
  29. echo "<script>alert('Formulir pendaftaran harus diisi dengan lengkap');javascript:history.go(-1);</script>";
  30. }
  31. ?>

in universitas html form,the html tag is select to show dropdown list


in jurusan html form,the html tag isradio

the question is :
"How to insert data to MySQL Database,from HTML Form with tag SELECT at Universitas field and tag type RADIO at Jurusan field??"

can you give me the right PHP script in my input.php file?

Hope you can help...
Last edited by adindra; Jul 9th, 2009 at 6:53 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 340
Reputation: Josh Connerty is an unknown quantity at this point 
Solved Threads: 26
Josh Connerty's Avatar
Josh Connerty Josh Connerty is offline Offline
Posting Whiz

Re: How to Insert/Edit data from TAG SELECT HTML Form to MySQL Database?

 
0
  #2
Jul 9th, 2009
Selects and Radios should be fine as long as you give them the value in witch you wich to be in the database.
  1. <input type="radio" name="you_field" value="Male" />
If you were to insert the POSTed data from my little form you would insert "Male" into the database, whereas you are using 1, 2, 3 & 4 for your values.

Please make sure that you also mysql_real_escape_string( $_POST['field_name'] ) as this will make sure MySql will not refuse any data given.
Posts should be like mini-skirts, long enough to cover enough, but not too long that you cover too much.

My Liveperson: http://liveperson.com/josh-connerty/
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 7
Reputation: adindra is an unknown quantity at this point 
Solved Threads: 0
adindra adindra is offline Offline
Newbie Poster

Re: How to Insert/Edit data from TAG SELECT HTML Form to MySQL Database?

 
0
  #3
Jul 9th, 2009
Originally Posted by Josh Connerty View Post
Selects and Radios should be fine as long as you give them the value in witch you wich to be in the database.
  1. <input type="radio" name="you_field" value="Male" />
If you were to insert the POSTed data from my little form you would insert "Male" into the database, whereas you are using 1, 2, 3 & 4 for your values.

Please make sure that you also mysql_real_escape_string( $_POST['field_name'] ) as this will make sure MySql will not refuse any data given.
thanks for reply..but, i'm sorry it didn't work..it will work if the value is only one.But it didn't work if there are more than 1 value..

you can try it yourself at your localhost,Josh,all of my script above..

hope you find out the solution..you can just copy-paste your tested script at this message's reply..

i need detail of the index.html and input.php..

Thank you Josh
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 28
Reputation: jcacquiescent27 is an unknown quantity at this point 
Solved Threads: 8
jcacquiescent27 jcacquiescent27 is offline Offline
Light Poster

Re: How to Insert/Edit data from TAG SELECT HTML Form to MySQL Database?

 
0
  #4
Jul 9th, 2009
Without seeing how your database is set up (the design of your various fields), it seems like you may only have the 'universitas' and 'jarusan' fields to accept a different data type or you just have it set up as varchar(1). Either way, it would probably do you good to take real values, like Josh was saying, by putting meaningful text into the value="" attributes.

Also, there are problems with your html.

Here's your 'universitas' select code:
  1. <select name="universitas[]">
  2. <option value="1">Universitas Gadjah Mada,Yogyakarta</option>
  3. <option value="2">Institut Teknologi Bandung,Bandung</option>
  4. <option value="3">Universitas Indonesia,Jakarta</option>
  5. <option value="4">Institut Teknologi Sepuluh November,Surabaya</option>
  6. <option value="5">Universitas Airlangga,Surabaya</option>

Here's what it should look like:
  1. <select name="universitas">
  2. <option value="1">Universitas Gadjah Mada,Yogyakarta</option>
  3. <option value="2">Institut Teknologi Bandung,Bandung</option>
  4. <option value="3">Universitas Indonesia,Jakarta</option>
  5. <option value="4">Institut Teknologi Sepuluh November,Surabaya</option>
  6. <option value="5">Universitas Airlangga,Surabaya</option>
  7. </select>

You were missing the closing </select> tag, and you probably didn't need to have an HTML array, which you specified by saying universitas[] .

Hope this helps.
Last edited by jcacquiescent27; Jul 9th, 2009 at 3:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 7
Reputation: adindra is an unknown quantity at this point 
Solved Threads: 0
adindra adindra is offline Offline
Newbie Poster

Yes!! I've Solved My Own PHP Problem

 
0
  #5
Jul 10th, 2009
Thank you for everyone who have reply my message...it help me although a little..

But now i'm very happy coz this time..i solved the problem by myself!! what a great experience..

Let me tell you my ways to solve this problem :

I added 2 table in my database (database name is manipulasiphp). The name of two added tables are jurusan, and universitas

in jurusan table field contain id, and jurusan
in universitas table field contain id, and universitas

i fill the database directly through my phpMyAdmin, i filled the id and i filled the name of each university and name of each jurusan.

the main purpose here is fill directly the table, so i can output them in my index.php, exactly in my Form

then i edited my index.html become index.php, because i add php script in my HTML form in Universitas field form and Jurusan field form

here's the script :

index.php

  1. <?php
  2. // koneksi ke mysql
  3. mysql_connect("localhost","root","");
  4. mysql_select_db("manipulasiphp");
  5. ?>
  6.  
  7. <form method="post" action="input.php">
  8. <table>
  9. <tr>
  10. <td>Nama</td>
  11. <td>: <input type="text" name="nama"></td>
  12. </tr>
  13. <tr>
  14. <td>Email</td>
  15. <td>: <input type="text" name="email"></td>
  16. </tr>
  17. <tr>
  18. <td>Alamat</td>
  19. <td>: <input type="text" name="alamat"></td>
  20. </tr>
  21. <tr>
  22. <td>Umur</td>
  23. <td>: <input type="text" name="umur" size="2" maxlength="3"> tahun</td>
  24. </tr>
  25. <tr>
  26. <td>Negara</td>
  27. <td>: <input type="text" name="negara"></td>
  28. </tr>
  29. <tr>
  30. <td>Universitas</td>
  31. <td>: <select name="masuk_universitas">
  32. <?php
  33. // query untuk menampilkan semua negara
  34. $query = "SELECT * FROM universitas";
  35. $hasil = mysql_query($query);
  36. while ($data = mysql_fetch_array($hasil))
  37. {
  38. // setiap negara yang dibaca dari tabel disisipkan ke tag <option></option>
  39. echo "<option value='".$data['id']."'>".$data['universitas']."</option>";
  40. }
  41. ?>
  42. </select>
  43. </td>
  44. </tr>
  45. <tr>
  46. <td>Jurusan :</td>
  47. <td>
  48. <?php
  49.  
  50. $query = "SELECT * FROM jurusan";
  51. $hasil = mysql_query($query);
  52. while ($data = mysql_fetch_array($hasil))
  53. {
  54.  
  55. echo "<input type=\"radio\" name=\"masuk_jurusan\" value='".$data['id']."'>".$data['jurusan']." \n";
  56. echo "<BR>";
  57. }
  58. ?>
  59.  
  60. </td>
  61.  
  62. </tr>
  63. <tr>
  64. <td>Prodi</td>
  65. <td>: <input type="text" name="prodi"></td>
  66. </tr>
  67. <tr>
  68. <td>No Telp</td>
  69. <td>: <input type="text" name="notelpon"></td>
  70. </tr>
  71. <tr>
  72. <td><input type="submit" name="submit" value="Daftar Beasiswa" /></td>
  73. <td><input type="reset" name="reset" value="Ndak Jadi ah.." /></td>
  74. </tr>
  75. </table>
  76.  
  77. </form>

you see that there are php script in Universitas form and Jurusan form, because i want to output the universitas table and jurusan table.

then after filled the form, i will go to input.php

here's the script :

input.php
  1. <?php
  2. $host="localhost";
  3. $usnm="root";
  4. $pswd="";
  5.  
  6. $koneksi=mysql_connect("$host","$usnm","$pswd") or die ("Localhost tidak terkoneksi");
  7.  
  8. mysql_select_db("manipulasiphp",$koneksi);
  9.  
  10. $input_nama=$_POST['nama'];
  11. $input_email=$_POST['email'];
  12. $input_alamat=$_POST['alamat'];
  13. $input_umur=$_POST['umur'];
  14. $input_negara=$_POST['negara'];
  15. $input_univ=$_POST['masuk_universitas'];
  16. $input_jurusan=$_POST['masuk_jurusan'];
  17. $input_prodi=$_POST['prodi'];
  18. $input_no_telp=$_POST['notelpon'];
  19.  
  20. if((!empty($input_nama)) && (!empty($input_email)) && (!empty($input_alamat)) && (!empty($input_umur)) && (!empty($input_negara)) && (!empty($input_univ)) && (!empty($input_jurusan)) && (!empty($input_prodi)) && (!empty($input_no_telp)))
  21. {
  22. $query=mysql_query("INSERT INTO member (nama,email,alamat,umur,negara,universitas,jurusan,prodi,no_telp) VALUES ('$input_nama','$input_email','$input_alamat','$input_umur','$input_negara','$input_univ','$input_jurusan','$input_prodi','$input_no_telp')",$koneksi);
  23. echo "Calon Peserta Beasiswa baru telah terdaftar!!";
  24. echo "<br>";
  25. echo "<br>Untuk melihat peserta lainnya, klik <a href=\"view.php\">disini</a>";
  26. }
  27. else
  28. {
  29. echo "<script>alert('Formulir pendaftaran harus diisi dengan lengkap');javascript:history.go(-1);</script>";
  30. }
  31. ?>

it works!! and it also success added my member table in my manipulasiphp database

Thank you everyone..

PROBLEM SOLVED by adindra
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 340
Reputation: Josh Connerty is an unknown quantity at this point 
Solved Threads: 26
Josh Connerty's Avatar
Josh Connerty Josh Connerty is offline Offline
Posting Whiz

Re: How to Insert/Edit data from TAG SELECT HTML Form to MySQL Database?

 
0
  #6
Jul 10th, 2009
Yes but the original problem still stands, it was most liekly because MySql was triming the universities because the field will only accept one character.

For instance varchar(1) will make this happen. You could have just changed it to varchar(100) .

But I'm glad you solved the problem. ;-)
Posts should be like mini-skirts, long enough to cover enough, but not too long that you cover too much.

My Liveperson: http://liveperson.com/josh-connerty/
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC