Jump to content

Recommended Posts

Hi all,i need help please..I got 2 php files,i'm intending to call a insert function from fns.inc.php to make a insert into myql database query..But it doesn't work..Here are the codes..Thanks a lot...

 

output_fns.php

function display_login_form()
{
$Error = array("email" => "","pwd" =>"","pwd1"=>"");
if(isset($_POST['submit']))
{
$email=mysql_real_escape_string($_POST['email']);
if(empty($email)){
$Error['email'] = "Email tidak boleh kosong";
}
else
if (!strchr($email, '@')) {
$Error['email'] = "Email yang anda masukkan tidak valid";
}  
$pwd1=mysql_real_escape_string($_POST['pwd1']);
if(empty($pwd1))
{
$Error['pwd1'] = "Password 1 tidak boleh kosong";
}
$pwd2=mysql_real_escape_string($_POST['pwd2']);
if(empty($pwd2))
{
$Error['pwd'] = "Password 2 tidak boleh kosong";
}
else
if($pwd1 != $pwd2)
{
$Error['pwd'] = "Password harus cocok";
}
$nama=mysql_real_escape_string($_POST['nama']);
$tmpt=mysql_real_escape_string($_POST['tempat_lahir']);
$tgl=mysql_real_escape_string($_POST['tgl']);
$bln=mysql_real_escape_string($_POST['bln']);
$thn=mysql_real_escape_string($_POST['thn']);
$tgl_lahir=$tgl.'-'.$bln.'-'.$thn;
$jekel=mysql_real_escape_string($_POST['jekel']);
$alamat=mysql_real_escape_string($_POST['alamat']);
$kota=mysql_real_escape_string($_POST['kota']);
$propinsi=mysql_real_escape_string($_POST['propinsi']);
if(empty($Error))
{
//this is the function that i want to call from fns.inc.php
insert($email,$pwd,$nama,$tgl_lahir,$jekel,$tmpt,$alamat,$kota,$propinsi);
}
}
else
{
$email="";
$pwd1="";
$pwd2="";
$nama="";
$tmpt="";
$tgl="";
$bln="";
$thn="";
$tgl_lahir=$tgl.'-'.$bln.'-'.$thn;
$jekel="";
$alamat="";
$kota="";
$propinsi="";
}
?>
<div id="stylized" class="myform">
  <form id="form1" name="form1" method="post" action="register.php">
    <fieldset>
    <legend >Data keanggotaan</legend>
    <br>
    <label>Email anda</label>
    <input type="text" name="email" id="email" value="<?php echo $email; ?>"/>
    <span class="error"> <?php echo $Error['email']; ?></span>
    <div class="spacer"></div>
    <label>Password
        <span class="small">6-12 karakter</span>
    </label>
    <input type="password" name="pwd1" id="pwd1" ?>
    <span class="error"> <?php echo $Error['pwd1']; ?></span>
    <div class="spacer"></div>
    <label>Ulangi Password
    </label>
    <input type="password" name="pwd2" id="pwd2" />
    <span class="error"> <?php echo $Error['pwd']; ?></span>
    <div class="spacer"></div>
    </fieldset>
    <fieldset>
    <legend>Data pribadi</legend>
    <br>
    <h1>   * Mohon diisi dengan data yang sebenarnya</h1>
    <div class="spacer"></div>
    <br>
    <label>Nama lengkap anda *
    <span class="small">Mohon nama asli anda</span>
    </label>
    <input type="text" name="nama" id="nama" value="<?php echo $nama; ?>"/>
    <div class="spacer"></div>
    <label>Tempat lahir anda *
    <span class="small"></span>
    </label>
    <input type="text" name="tempat_lahir" id="tempat_lahir"/>
    <div class="spacer"></div>
    <label for="tgl">Tanggal lahir *
    <span class="small">dd/mm/yyyy</span>
    </label>
    <select name="tgl" id="tgl">
        <option value="1" selected="selected">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
        <option value="16">16</option>
        <option value="17">17</option>
        <option value="18">18</option>
        <option value="19">19</option>
        <option value="20">20</option>
        <option value="21">21</option>
        <option value="22">22</option>
        <option value="23">23</option>
        <option value="24">24</option>
        <option value="25">25</option>
        <option value="26">26</option>
        <option value="27">27</option>
        <option value="28">28</option>
        <option value="29">29</option>
        <option value="30">30</option>
        <option value="31">31</option>
      </select>
        <label for="bln"></label><select name="bln" id="bln">
        <option value="1" selected="selected">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        </select>
        <label for="thn"></label>
<input type="text" name="thn" id="thn"/>
        <div class="spacer"></div>
        <label>Jenis Kelamin
<span class="small">Jenis kelamin anda</span>
</label>
        <select name="jekel">
 <option value="Pilih" selected="selected">Pilih salah satu... </option>
 <option value="pria" id="jekel">Pria</option>
 <option value="wanita" id="jekel">Wanita</option>

        </select>
        <div class="spacer"></div>
        <label>Alamat 
        <span class="small">Alamat lengkap anda</span>
        </label>
        <input type="text" name="alamat" id="alamat"/>
        <div class="spacer"></div>
        <label>Kota 
        <span class="small">Kota anda</span>
        </label>
        <input type="text" name="kota" id="kota"/>
        <div class="spacer"></div>
        <label>Propinsi 
        <span class="small">Propinsi anda</span>
        </label>
        <select name="propinsi">
        <option value="Pilih" selected="selected">Pilih salah satu....</option>
 <option>Nanggroe Aceh Darussalam</option>
<option>Sumatera Utara</option>
<option>Sumatera Barat</option>
<option>Bengkulu</option>
<option>Riau</option>
<option>Kepulauan Riau</option>
<option>Jambi</option>
<option>Sumatera Selatan</option>
<option>Lampung</option>
<option>Kepulauan Bangka Belitung</option>
<option>DKI Jakarta</option>
<option>Jawa Barat</option>
<option>Banten</option>
<option>Jawa Tengah</option>
<option>DI Yogyakarta</option>
<option>Jawa Timur</option>
<option>Kalimantan Barat</option>
<option>Kalimantan Tengah</option>
<option>Kalimantan Selatan</option>
<option>Kalimantan Timur</option>
<option>Bali</option>
<option>Nusa Tenggara Barat</option>
<option>Nusa Tenggara Timur</option>
<option>Sulawesi Barat</option>
<option>Sulawesi Utara</option>
<option>Sulawesi Tengah</option>
<option>Sulawesi Selatan</option>
<option>Sulawesi Tenggara</option>
<option>Gorontalo</option>
<option>Maluku</option>
<option>Maluku Utara</option>
<option>Papua Barat</option>
<option>Papua</option>
        </select>

        </fieldset>
    <br>
    <button type="submit" name="submit">Sign-up</button>
    
  </form>
</div>

 

fns.inc.php

<?php
function insert($email,$pwd,$nama,$tgl_lahir,$jekel,$tmpt,$alamat,$kota,$propinsi)
{
$tambahuser="INSERT INTO `users` (`email`,`password`,`nama`,`tempat_lahir`,
`tgl_lahir`,`jenis_kelamin`,`alamat`,`kota`,`propinsi`)
VALUES ('$email','$pwd','$nama','$tmpt','$tgl_lahir','$jekel','$alamat','$kota','$propinsi');";   
$exec=mysql_query($tambahuser);
if ($exec)
{
   header('location:thanks.php');
   return mysql_affected_rows();
}
else
{
    echo mysql_error();
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/144059-php-function/
Share on other sites

Your function will not have access to your mysql connection as it is out of the function scope. Do you get an error?

You can either use a global variable within the function to your connection or pass in as a parameter

 

$x = mysql_connect();

function example() {
  global $x;
}

 

Im not sure why you would functionalise this procedure unless you are using this insert process from more than script in your website application.

 

Also your function has way too many parameters. You are better passing in the POST array:

 

function example($values) {
  print $values['name'];
}

example($_POST);

Link to comment
https://forums.phpfreaks.com/topic/144059-php-function/#findComment-755896
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.