Jump to content

Recommended Posts

Posted

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
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="";
}
?>

 
   
   Data keanggotaan
   

   Email anda
   
    
   
   Password
       6-12 karakter
   
   
    
   
   Ulangi Password
   
   
    
   
   
   
   Data pribadi
   

      * Mohon diisi dengan data yang sebenarnya
   
   

   Nama lengkap anda *
   Mohon nama asli anda
   
   
   
   Tempat lahir anda *
   
   
   
   
   Tanggal lahir *
   dd/mm/yyyy
   
   
       1
       2
       3
       4
       5
       6
       7
       8
       9
       10
       11
       12
       13
       14
       15
       16
       17
       18
       19
       20
       21
       22
       23
       24
       25
       26
       27
       28
       29
       30
       31
     
       
       1
       2
       3
       4
       5
       6
       7
       8
       9
       10
       11
       12
       
       

       
       Jenis Kelamin
Jenis kelamin anda

       
 Pilih salah satu... 
 Pria
 Wanita
 
       
       
       Alamat 
       Alamat lengkap anda
       
       
       
       Kota 
       Kota anda
       
       
       
       Propinsi 
       Propinsi anda
       
       
       Pilih salah satu....
 Nanggroe Aceh Darussalam
Sumatera Utara
Sumatera Barat
Bengkulu
Riau
Kepulauan Riau
Jambi
Sumatera Selatan
Lampung
Kepulauan Bangka Belitung
DKI Jakarta
Jawa Barat
Banten
Jawa Tengah
DI Yogyakarta
Jawa Timur
Kalimantan Barat
Kalimantan Tengah
Kalimantan Selatan
Kalimantan Timur
Bali
Nusa Tenggara Barat
Nusa Tenggara Timur
Sulawesi Barat
Sulawesi Utara
Sulawesi Tengah
Sulawesi Selatan
Sulawesi Tenggara
Gorontalo
Maluku
Maluku Utara
Papua Barat
Papua
       

       
   

   Sign-up
   
 

 

fns.inc.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();
}
}
?>

Posted (edited)

What error messages are you getting (if any).

 

Try displaying the contents of $tambahuser before or after

$exec=mysql_query($tambahuser);

 

 

$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."')";

 

Note you don't need the quotes for the table name and paramaters!

 

The string is concatinated for example:

'[email protected]', 'pass1234','...

 

Make sure your single quotes are inside the double quotes.

e.g.

" '_" . $myVar . "_' "

Remove the _, this is to show there is a single quote embedded with double quotes.

 

This is one issue i've noticed, you sure you're connecting to the database ok?!

Edited by Pashers
Posted

dont forget that at some point the first file will need know where to find the functions.

 

you will need an include() or similar.

 

 

you may have done this but it is not in the listings supplied also the <?php was missing from the start assumed that it was chopped of in the paste

Posted

I got no error ouput there,i've include the db connection file in the fns.inc.php..Suppose i do something wrong with calling the function?Thanks a lot...For the output_fns.php here are the full codes..

 


include('fns.inc.php');
//buat header
function do_kepala($title){
?>
"http://www.w3.org/TR/html4/strict.dtd">








};
//buat body
function do_body()
{
login_form();
?>
};
function do_footer()
{
?>
   


};
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))
{
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="";
}
?>

 
   
   Data keanggotaan
   

   Email anda
   
    
   
   Password
       6-12 karakter
   
   
    
   
   Ulangi Password
   
   
    
   
   
   
   Data pribadi
   

      * Mohon diisi dengan data yang sebenarnya
   
   

   Nama lengkap anda *
   Mohon nama asli anda
   
   
   
   Tempat lahir anda *
   
   
   
   
   Tanggal lahir *
   dd/mm/yyyy
   
   
       1
       2
       3
       4
       5
       6
       7
       8
       9
       10
       11
       12
       13
       14
       15
       16
       17
       18
       19
       20
       21
       22
       23
       24
       25
       26
       27
       28
       29
       30
       31
     
       
       1
       2
       3
       4
       5
       6
       7
       8
       9
       10
       11
       12
       
       

       
       Jenis Kelamin
Jenis kelamin anda

       
 Pilih salah satu... 
 Pria
 Wanita
 
       
       
       Alamat 
       Alamat lengkap anda
       
       
       
       Kota 
       Kota anda
       
       
       
       Propinsi 
       Propinsi anda
       
       
       Pilih salah satu....
 Nanggroe Aceh Darussalam
Sumatera Utara
Sumatera Barat
Bengkulu
Riau
Kepulauan Riau
Jambi
Sumatera Selatan
Lampung
Kepulauan Bangka Belitung
DKI Jakarta
Jawa Barat
Banten
Jawa Tengah
DI Yogyakarta
Jawa Timur
Kalimantan Barat
Kalimantan Tengah
Kalimantan Selatan
Kalimantan Timur
Bali
Nusa Tenggara Barat
Nusa Tenggara Timur
Sulawesi Barat
Sulawesi Utara
Sulawesi Tengah
Sulawesi Selatan
Sulawesi Tenggara
Gorontalo
Maluku
Maluku Utara
Papua Barat
Papua
       

       
   

   Sign-up
   
 


};
function doError( $field )
{
 if( isset($errors[$field]) ) {
   echo ''.$errors[$field].'';
}
?>
};
function login_form()
{
?>


Email anda 
       
       
       Password 
       
       
       
       
Log In

Register



};
?>

Posted

Assuming no header information has been sent when the code hits...

 

header('location:thanks.php');

return mysql_affected_rows();

 

the page will redirect to thanks.php and will never return the affected rows.

 

To track the bug, change that to display the mysql return value and string too.

 

As others have said what errors do you get back from mySQL?

 

I always put single quotes around the values for string fields and leave them off for numeric fields. Not sure if it's vital, but it helps me spot numeric and string fields.

Posted
Hmmm, so no errors when connecting to the database, no errors when INSERTing the data, and yet nothing in the database tables when you check them via phpmyadmin or similar program?
Posted

Could you try:

 

if(empty($Error))
{
echo "TESTING TESTING 123"
insert($email,$pwd,$nama,$tgl_lahir,$jekel,$tmpt,$alamat,$kota,$propinsi);
}

 

Line 72 in the fns.inc.php

 

Does the TESTING TESTING 123 display on the page?

Posted

It's been a while since i looked at PHP, but the IF statement in the fns.inc.php doesnt have a condition with it, ie if($exec=whatever) rather than just if($exec)

 

 

Should there not be more to it?

Posted

Sorry I have to go off now. However, try continuing with the following:

 

function display_login_form()
{
echo "ENTER LOGIN"
$Error = array("email" => "","pwd" =>"","pwd1"=>"");
if(isset($_POST['submit']))
{
echo "ENTER SUBMIT"
$email=mysql_real_escape_string($_POST['email']);
if(empty($email)){
 echo "ENTER EMPTY EMAIL"
 $Error['email'] = "Email tidak boleh kosong";
}
else {
 if (!strchr($email, '@')) {
  echo "ENTER EMAIL NO @"
  $Error['email'] = "Email yang anda masukkan tidak valid";
 }

 

Note the echo, place them where ever you can to try and see which directions it goes. This may/may not help. But should help show the issues!

Posted
Hi cullingsh,the if($exec) doesn't get any problem if i put it in the output_fns.php.Maybe i have missed some statements there..Thanks..
Posted

How about adding error_reporting(E_ALL); at the beginning of index.php, to see if any errors are being generated. I use it all the time I'm developing to make sure I don't have any errors, uninitialised variables are the ones that slip through. They only generate warnings, but I prefer to kill them all with isset() checks.

 

Taking a quick at the output_fns.php... is the insert function being called at all?

 

you initialise the array...

 

$Error = array("email" => "","pwd" =>"","pwd1"=>"");

 

then test if it's empty...

 

if(empty($Error))

{

insert($email,$pwd,$nama,$tgl_lahir,$jekel,$tmpt,$alamat,$kota,$propinsi);

 

Try changing the declaration of the variable to $Error=array();

then the check for errors may work, but if (count($Error)==0) might be a more reliable check.

Posted
Hi Paul,i've tried it.It displasy error says that undefined index..If i put the queries in the display_login_form(),it works,The database was updated.All i'm trying to do is to make another page that contains the queries function..Thanks a lot...
Posted

Does undefined index appear when $Error['email']="Email error" ?

 

Because I'd expect that. You can just add to the errors array without specifying an index...

 

$error[]="Email error";

 

...

 

$error[]="Password error";

 

Personally, I just use a string ($errors) and add to it with each error message wrapped in a

  • ...
  • tag then display as follows..

     

    print "

      $errors
    ";
    Posted
    Yes,it does.Paul,what do you do usually when dealing with member registration?Will you put the insert function within the registration page or you put the function in a another page?Thanks a lot..

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now



    ×
    ×
    • Create New...