1,105,288 Community Members

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

Member Avatar
adindra
Newbie Poster
7 posts since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

<html>
<head>
<title>Form Calon Peserta Beasiswa Monbugakusho Jepang</title>
</head>
<body>
<form method="post" action="input.php">
<table>
<tr>
<td>Nama</td>
<td>: <input type="text" name="nama"></td>
</tr>
<tr>
<td>Email</td>
<td>: <input type="text" name="email"></td>
</tr>
<tr>
<td>Alamat</td>
<td>: <input type="text" name="alamat"></td>
</tr>
<tr>
<td>Umur</td>
<td>: <input type="text" name="umur" size="2" maxlength="3">tahun</td>
</tr>
<tr>
<td>Negara</td>
<td>: <input type="text" name="negara"></td>
</tr>



<tr>
<td>Universitas</td>
[B]<td>: <select name="universitas[]">
      <option value="1">Universitas Gadjah Mada,Yogyakarta</option>
	  <option value="2">Institut Teknologi Bandung,Bandung</option>
	  <option value="3">Universitas Indonesia,Jakarta</option>
	  <option value="4">Institut Teknologi Sepuluh November,Surabaya</option>
	  <option value="5">Universitas Airlangga,Surabaya</option>
</td>[/B]
</tr>
<tr>
[B]<td>Jurusan :</td>
<td><input type="radio" name="jurusan" value="1">Teknik Fisika
      <br><input type="radio" name="jurusan" value="2">Teknik Kimia
	  <br><input type="radio" name="jurusan" value="3">Teknik Informatika
	  <br><input type="radio" name="jurusan" value="4">MIPA
      
</td>[/B]
</tr>




<tr>
<td>Prodi</td>
<td>: <input type="text" name="prodi"></td>
</tr>
<tr>
<td>No Telpon</td>
<td>: <input type="text" name="notelpon"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Daftar Beasiswa"></td>
<td><input type="reset" name="reset" value="Ndak Jadi ahh.."></td>
</tr>
</table>
</form>
</body>
</html>

then input.php

<?php
$host="localhost";
$usnm="root";
$pswd="";

$koneksi=mysql_connect("$host","$usnm","$pswd") or die ("Localhost tidak terkoneksi");

mysql_select_db("manipulasiphp",$koneksi);

$input_nama=$_POST['nama'];
$input_email=$_POST['email'];
$input_alamat=$_POST['alamat'];
$input_umur=$_POST['umur'];
$input_negara=$_POST['negara'];
$input_univ=$_POST['universitas'];
$input_jurusan=$_POST['jurusan'];
$input_prodi=$_POST['prodi'];
$input_no_telp=$_POST['notelpon'];

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)))
{
$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);
echo "Calon Peserta Beasiswa baru telah terdaftar!!";
echo "<br>";
echo "<br>Untuk melihat peserta lainnya, klik <a href=\"view.php\">disini</a>";
}
else
{
echo "<script>alert('Formulir pendaftaran harus diisi dengan lengkap');javascript:history.go(-1);</script>";
}
?>

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...

Member Avatar
Josh Connerty
Posting Whiz
358 posts since Apr 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 29 [?]
Skill Endorsements: 1 [?]
 
0
 

Selects and Radios should be fine as long as you give them the value in witch you wich to be in the database.

<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.

Member Avatar
adindra
Newbie Poster
7 posts since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Selects and Radios should be fine as long as you give them the value in witch you wich to be in the database.

<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

Member Avatar
jcacquiescent27
Light Poster
28 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

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:

<select name="universitas[]">
      <option value="1">Universitas Gadjah Mada,Yogyakarta</option>
	  <option value="2">Institut Teknologi Bandung,Bandung</option>
	  <option value="3">Universitas Indonesia,Jakarta</option>
	  <option value="4">Institut Teknologi Sepuluh November,Surabaya</option>
	  <option value="5">Universitas Airlangga,Surabaya</option>

Here's what it should look like:

<select name="universitas">
      <option value="1">Universitas Gadjah Mada,Yogyakarta</option>
	  <option value="2">Institut Teknologi Bandung,Bandung</option>
	  <option value="3">Universitas Indonesia,Jakarta</option>
	  <option value="4">Institut Teknologi Sepuluh November,Surabaya</option>
	  <option value="5">Universitas Airlangga,Surabaya</option>
</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.

Member Avatar
adindra
Newbie Poster
7 posts since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

<?php
// koneksi ke mysql
mysql_connect("localhost","root","");
mysql_select_db("manipulasiphp");
?>
 
<form method="post" action="input.php">
<table>
<tr>
<td>Nama</td>
<td>: <input type="text" name="nama"></td>
</tr>
<tr>
<td>Email</td>
<td>: <input type="text" name="email"></td>
</tr>
<tr>
<td>Alamat</td>
<td>: <input type="text" name="alamat"></td>
</tr>
<tr>
<td>Umur</td>
<td>: <input type="text" name="umur" size="2" maxlength="3"> tahun</td>
</tr>
<tr>
<td>Negara</td>
<td>: <input type="text" name="negara"></td>
</tr>
<tr>
<td>Universitas</td>
<td>: <select name="masuk_universitas">
<?php
   // query untuk menampilkan semua negara
   $query = "SELECT * FROM universitas";
   $hasil = mysql_query($query);
   while ($data = mysql_fetch_array($hasil))
   {
      // setiap negara yang dibaca dari tabel disisipkan ke tag <option></option>
      echo "<option value='".$data['id']."'>".$data['universitas']."</option>";
   }
?> 
</select>
</td>
</tr>
<tr>
<td>Jurusan :</td>
<td>
<?php
   
   $query = "SELECT * FROM jurusan";
   $hasil = mysql_query($query);
   while ($data = mysql_fetch_array($hasil))
   {
      
      echo "<input type=\"radio\" name=\"masuk_jurusan\" value='".$data['id']."'>".$data['jurusan']." \n";
	  echo "<BR>";
   }
?> 

</td>

</tr>
<tr>
<td>Prodi</td>
<td>: <input type="text" name="prodi"></td>
</tr>
<tr>
<td>No Telp</td>
<td>: <input type="text" name="notelpon"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Daftar Beasiswa" /></td>
<td><input type="reset" name="reset" value="Ndak Jadi ah.." /></td>
</tr>
</table>

</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

<?php
$host="localhost";
$usnm="root";
$pswd="";

$koneksi=mysql_connect("$host","$usnm","$pswd") or die ("Localhost tidak terkoneksi");

mysql_select_db("manipulasiphp",$koneksi);

$input_nama=$_POST['nama'];
$input_email=$_POST['email'];
$input_alamat=$_POST['alamat'];
$input_umur=$_POST['umur'];
$input_negara=$_POST['negara'];
$input_univ=$_POST['masuk_universitas'];
$input_jurusan=$_POST['masuk_jurusan'];
$input_prodi=$_POST['prodi'];
$input_no_telp=$_POST['notelpon'];

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)))
{
$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);
echo "Calon Peserta Beasiswa baru telah terdaftar!!";
echo "<br>";
echo "<br>Untuk melihat peserta lainnya, klik <a href=\"view.php\">disini</a>";
}
else
{
echo "<script>alert('Formulir pendaftaran harus diisi dengan lengkap');javascript:history.go(-1);</script>";
}
?>

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

Thank you everyone..

PROBLEM SOLVED by adindra

Question Answered as of 4 Years Ago by jcacquiescent27 and Josh Connerty
Member Avatar
Josh Connerty
Posting Whiz
358 posts since Apr 2009
Reputation Points: 20 [?]
Q&As Helped to Solve: 29 [?]
Skill Endorsements: 1 [?]
 
0
 

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. ;-)

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article