Jump to content
theywe

How To If Else?

Recommended Posts

$query = "UPDATE userprofile"
        . "\n, ethnicity = " . $database->Quote($_POST['ethnicity'])
how to if else tis statement. Contoh yg sama tapi lain sket coding:
if ( $_POST['ethnicity']=="Others" ){
$row ->ethnicity = trim($_POST['other_ethnic']);
}    else { 
    $row ->ethnicity = trim($_POST['ethnicity']);     
    }

Thxs..

Share this post


Link to post
Share on other sites

huhuhu.. soalan

kat atas tu satu statement tuk post dalam database.. tapi nak kena guna if else macam contoh kat bawah tu. Caranya sama cuma penyampaiannya lain!! hehehehe

Share this post


Link to post
Share on other sites

Hello again!!

Ok kat bawah nie ada 2 code, but the process is same. cuma penyampaian je lain. Aku nak buat if else utk code 1st nieh? tapi bila run dia error unexpected T_IF. macam2 tempat dah aku if kan, tapi error still sama. HOW YA??

CODE YG PERLU DIUBAH

$query = "UPDATE userprofile"
        . "\n, icNo = " . $database->Quote($_POST['icNo'])
        . "\n, dob = " . $database->Quote($_POST['dob'])
        . "\n, ethnicity = " . $database->Quote($_POST['ethnicity'])
CODE ASAL
if ( $_POST['ethnicity']=="Others" ){
$row ->ethnicity = trim($_POST['other_ethnic']);
}    else { 
    $row ->ethnicity = trim($_POST['ethnicity']);     
    }

Thxs..

Edited by theywe

Share this post


Link to post
Share on other sites

Still tak faham. Apa yang nak dibuat sebenarnya? Tengok IF statement tu elok je. :huh:

Share this post


Link to post
Share on other sites

Still tak faham. Apa yang nak dibuat sebenarnya? Tengok IF statement tu elok je. :huh:

yg code bawah tu mmg ok..

masalah code atas tu.. sebab bentuk dia lain. Objective tetap sama nak simpan data.

cuma tak tau camna nak letak if else.. macam2 tempat if aku letak!!

Share this post


Link to post
Share on other sites

yg code bawah tu mmg ok..

masalah code atas tu.. sebab bentuk dia lain. Objective tetap sama nak simpan data.

cuma tak tau camna nak letak if else.. macam2 tempat if aku letak!!

Ubah daripada:

$query = "UPDATE userprofile"
        . "\n, icNo = " . $database->Quote($_POST['icNo'])
        . "\n, dob = " . $database->Quote($_POST['dob'])
        . "\n, ethnicity = " . $database->Quote($_POST['ethnicity'])
Kepada:
$query = "UPDATE userprofile icNo = '" . $database->Quote($_POST['icNo'])."' dob = '" . $database->Quote($_POST['dob'])."' ethnicity = '" . $database->Quote($_POST['ethnicity'])."'";

Tak faham apa fungsi Quote() tu. Tapi biasa kalau guna UPDATE mesti ada condition

Share this post


Link to post
Share on other sites

Quote an identifier name (field, table, etc) nie yg dapat aku terangkan.

Edited by theywe

Share this post


Link to post
Share on other sites

Quote an identifier name (field, table, etc) nie yg dapat aku terangkan.

Huh :blink: biar benar itu je? Tapi kenapa structure yang digunakan tu tak sama pung. Confused gila

Share this post


Link to post
Share on other sites

Huh :blink: biar benar itu je? Tapi kenapa structure yang digunakan tu tak sama pung. Confused gila

takpe.. dah lain stucture dia.. tapi kena teruskan.. kena ada if else juga

Share this post


Link to post
Share on other sites

takpe.. dah lain stucture dia.. tapi kena teruskan.. kena ada if else juga

Kenapa kena ada if else juga? Itukan sekadar SQL query?

Share this post


Link to post
Share on other sites

Kenapa kena ada if else juga? Itukan sekadar SQL query?

sebab.. kalau orang tu click others.. then pop up satu textfield then user akan key in, data baru tu disimpan dalam other_ethnic.

Then kita umpukkan other_ethnic dalam ethnicity. kalau user bukan key in hanya pilih dari drop down. maka simpan terus dalam ethnicity. begitu la ceritanya.

Edited by theywe

Share this post


Link to post
Share on other sites

sebab.. kalau orang tu click others.. then pop up satu textfield then user akan key in, data baru tu disimpan dalam other_ethnic.

Then kita umpukkan other_ethnic dalam ethnicity. kalau user bukan key in hanya pilih dari drop down. maka simpan terus dalam ethnicity. begitu la ceritanya.

Mak aih complex nya. Kenapa tak ambil terus daripada user input? Kenapa nak ada other_ethnicity. Kan senang kalau check apa yang user input lepas tu assign variable. Rasanya tak la complex sangat.

Share this post


Link to post
Share on other sites

Mak aih complex nya. Kenapa tak ambil terus daripada user input? Kenapa nak ada other_ethnicity. Kan senang kalau check apa yang user input lepas tu assign variable. Rasanya tak la complex sangat.

so time update form.. kita assign dulu ker var tu?

<?php                     
<select name ethnicity>
             <?php if ( $_POST['ethnicity']=="Others" ){
                    $row ->ethnicity = ($_POST['other_ethnic']);
                    }else { 
               $row ->ethnicity = ($_POST['ethnicity']);     
         }?>
<option value='Others'     >Others</option>

<div id='other_ethnic_layer' style='display:none'>
        Please enter your Ethnicity: *
        <br/><input type='text' size=30  name='other_ethnic' />  <span onClick='showCommonEthnicity()'><u>Cancel</u></span>      
</div>

adakah ia akan menjadi begini??

Tapi tak echo user key in, still echo Others dari drop down menu

Share this post


Link to post
Share on other sites

Macam ni... bila user dah submit buatkan supaya system tu check apa ethnic dia. Lepas dah check tu assign kan ethnic dia. Dan boleh dah set SQL nya. Senang je :lol:

Share this post


Link to post
Share on other sites

Macam ni... bila user dah submit buatkan supaya system tu check apa ethnic dia. Lepas dah check tu assign kan ethnic dia. Dan boleh dah set SQL nya. Senang je :lol:

tak faham.. maknanya nak letak kat <select>

atau kat post?

atau kat memana?

masa register dah check ethnic tu datang dari drop down atau textfield. macam contoh kat atas tu if else. code tu letak masa nak post.

cuma kat update dianya code berbeza dari register. so nak letak if else tu tak boleh ada error.

Edited by theywe

Share this post


Link to post
Share on other sites

tak faham.. maknanya nak letak kat <select>

atau kat post?

atau kat memana?

Macam ni, mula mula list out semua ethnic tu. Bila user pilih Others akan keluar satu input box. Kan? Lepas tu user pun bagi ethnic dia dan submit. Lepas submit ni mesti pergi ke page PHP untuk process kan? Dalam process tu baru masukkan IF statement.

Logik tak?

Share this post


Link to post
Share on other sites

Macam ni, mula mula list out semua ethnic tu. Bila user pilih Others akan keluar satu input box. Kan? Lepas tu user pun bagi ethnic dia dan submit. Lepas submit ni mesti pergi ke page PHP untuk process kan? Dalam process tu baru masukkan IF statement.

Logik tak?

Logik.. mmg buat kat proces la tu.. tapi cara penyampaian macam code atas tu.. cuma masalah bila letak if else jer.. ada error

Share this post


Link to post
Share on other sites

Logik.. mmg buat kat proces la tu.. tapi cara penyampaian macam code atas tu.. cuma masalah bila letak if else jer.. ada error

Dah cuba restructure SQL statement macam yang dicadangkan?

Share this post


Link to post
Share on other sites

Dah cuba restructure SQL statement macam yang dicadangkan?

masalahnya sekarang structure ikut joomla.. so nie semua hard code.. tambah2 dalam joomla.. sebab konunnya taknak pakai third party.. kan dah menyusahkan..

takpela dewi try it 1st. try structure lain

thxs

Share this post


Link to post
Share on other sites

OK dah baca dah tentang Joomla punya Quote():

Returns a quoted string with all characters that have special meaning for the database escaped. This function is the same as database->getEscaped except that the string returned is surrounded by single quotes.

Syntax:

string Quote ( string $text )

Maknanya Quote ni tak buat apa sangat setakat filter input je. Memang rasanya kena restructure balik SQL tu. Cuba tengok bebetul your SQL statement. Kalau tak guna condition dalam UPDATE, macam mana system nak tau user mana yang perlu dikemaskinikan?

Cadangan je la kan. B)

Share this post


Link to post
Share on other sites

OK dah baca dah tentang Joomla punya Quote():

Syntax:

string Quote ( string $text )

Maknanya Quote ni tak buat apa sangat setakat filter input je. Memang rasanya kena restructure balik SQL tu. Cuba tengok bebetul your SQL statement. Kalau tak guna condition dalam UPDATE, macam mana system nak tau user mana yang perlu dikemaskinikan?

Cadangan je la kan. B)

mmg betul apa yang awak cakap tu shidi!! heehhe

mmg kena tukar structure..

mmg tak boleh guna if else ke kat code 1st tu.. mmg kena single post aje ke? tak leh letak if else

Share this post


Link to post
Share on other sites

mmg betul apa yang awak cakap tu shidi!! heehhe

mmg kena tukar structure..

mmg tak boleh guna if else ke kat code 1st tu.. mmg kena single post aje ke? tak leh letak if else

If Else sekadar nak assign variable je. Untuk keywords tu tak perlu.

Share this post


Link to post
Share on other sites

Shidi.. selain structure nie.. structure mana lagi boleh buat tuk tambah if else..

$database2 = new database( "localhost", "root", "user", "imation", "");    
    
    $query = "UPDATE userprofile"
        
        . "\n SET fullname = " . $database->Quote($row->name)
        . "\n, email = " . $database->Quote($row->email)
        . "\n, password = " . $database->Quote($row->password)
             

        . "\n, ethnicity = " . $database->Quote($_POST['ethnicity'])
        . "\n, religion = " . $database->Quote($_POST['religion'])
        
        . "\n WHERE userID = " . $database->Quote( $orig_username );
        
        $database2->setQuery( $query );
        $database2->query();
    print ("query >> $query <br>");

nak wat if else je kat ethnicity ngan religion. dapat letak if else gerenti boleh masuk dalam database. structure camni tak boleh letak if else ke? ada error if statement!!

Share this post


Link to post
Share on other sites

Shidi.. selain structure nie.. structure mana lagi boleh buat tuk tambah if else..

$database2 = new database( "localhost", "root", "user", "imation", "");    
    
    $query = "UPDATE userprofile"
        
        . "\n SET fullname = " . $database->Quote($row->name)
        . "\n, email = " . $database->Quote($row->email)
        . "\n, password = " . $database->Quote($row->password)
             

        . "\n, ethnicity = " . $database->Quote($_POST['ethnicity'])
        . "\n, religion = " . $database->Quote($_POST['religion'])
        
        . "\n WHERE userID = " . $database->Quote( $orig_username );
        
        $database2->setQuery( $query );
        $database2->query();
    print ("query >> $query <br>");

nak wat if else je kat ethnicity ngan religion. dapat letak if else gerenti boleh masuk dalam database. structure camni tak boleh letak if else ke? ada error if statement!!

Error IF statement? Macam mana takda IF Else tapi ada error IF statement? Menarik ni.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...