2016-07-28 77 views
-5

我想將我的if else結構轉換爲開關盒如何將其更改爲開關盒。如果將其他結構轉換爲開關盒結構

<?php 
    header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim  ajax Calling request 
mysql_connect("localhost","root",""); 
mysql_select_db("ocean"); 
if(isset($_GET['type'])) 
    { 
    $res = []; 


    if($_GET['type'] =="add"){ 
    $name = $_GET ['Name']; 
    $lname = $_GET['Lname']; 
    $userN = $_GET['User']; 
    $passW = $_GET['Pass']; 
    $gen = $_GET['Gender']; 
    $mail = $_GET ['Email']; 
    $mobile = $_GET ['Mobile']; 
    $address= $_GET['Address']; 

    $query1="select uid from oops where email='$mail'"; 
    $result1= mysql_query($query1); 
    if(mysql_num_rows($result1)>0){ 
     $res["flag"]= TRUE; 
     $rest["message"] = "There is already a user with that email!"; 
    }else{ 
    $query1 = "insert into oops(username, password, firstname, lastname, gender, email, mobile, address) values('$userN','$passW','$name','$lname','$gen','$mail','$mobile','$address')"; 
    $result1 = mysql_query($query1); 

    if($result1) 
    { 
     $res["flag"] = true; 
     $rest["message"] = "Data Inserted Successfully"; 
    } 
    else 
    { 
     $res["flag"] = false; 
     $rest["message"] = "Oppes Errors"; 
    } 
    } 

    } 
    if($_GET['type'] =="edit") { 
     $id=$_GET['id']; 
    $name = $_GET ['Name']; 
    $lname = $_GET['Lname']; 
    $userN = $_GET['User']; 
    $passW = $_GET['Pass']; 
    $gen = $_GET['Gender']; 
    $mail = $_GET ['Email']; 
    $mobile = $_GET ['Mob']; 
    $address= $_GET['Address']; 
    //$id = $_GET['id']; 
    // echo var_dump($_GET); 
    $query1 =("UPDATE oops SET username = '$userN',password = '$passW', firstname= '$name',lastname='$lname',gender = '$gen', email = '$mail', mobile = '$mobile' , address = '$address' WHERE uid = '$id'")or die('fail to update'); 
    $result1 = mysql_query($query1); 
    if($result1) 
    { 
     $res["flag"] = true; 
     $rest["message"] = "Data Updated Successfully"; 
    } 
    else 
    { 
     $res["flag"] = false; 
     $rest["message"] = "Oppes Errors"; 
    } 
    } 
    if($_GET['type'] == "delete"){ 
     $id=$_GET['id']; 

     $query1=("DELETE FROM oops WHERE uid='$id'"); 
     $result1= mysql_query($query1); 
     if($result1){ 
      $res["flag"] = true; 
      $rest["message"] = "User deleted Successfully"; 
      // header("location:client.php"); 
     } 
     else{ 
     $res["flag"] = false; 
     $rest["message"] = "Oppes Errors"; 
     } 
    } 

} 

else{ 
$res["flag"] = false; 
$rest["message"] = "Invalid format"; 
} 

echo json_encode($rest); 

?> 

我不知道該怎麼做,我怎麼isset($_GET['type'])值的值保存到變量同一頁上

+5

強制性的,你具備的,SQL注入的漏洞,和'mysql_ *'-is過時,評論。 –

+3

必讀:[我怎樣才能防止SQL注入在PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)和[爲什麼shouldn'我在PHP中使用mysql_ *函數?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 –

+6

呃,你還使用純文本密碼?這是我長期以來看到的最差的代碼。閱讀這也是:http://plaintextoffenders.com/faq/devs –

回答

0
switch($_GET['type']) { 
    case "add": 
     //do something 
     break; 
    case "delete": 
     // do something else etc 

    default: 
     //if none of the cases apply, do something here (same as else in if-elseif-else) 
} 
+0

我在我的代碼中編輯但它將空的結果存儲到數據庫中 – oceanier

+0

我得到了結果感謝您的幫助 – oceanier

0
switch($_GET['type']) { 
    case "add": insert code here; break; 
    case "edit": insert code here; break; 
    case "delete": insert code here; break; 
    default: insert code of last 'else' here; 
} 

http://php.net/manual/en/control-structures.switch.php

也請考慮的意見Alexander O'Mara關於消毒用戶輸入和使用最新功能。 mysql_(沒有結尾的'i')函數從PHP 5.5.0開始已棄用,並且已從PHP 7中刪除。

0

您可以將類型存儲到變量中並使用開關

$type = $_GET['type']; 
switch($type){ 
    case "add": 
    .. 
    break; 
    case "edit": 
    .. 
    break; 
} 

Switch