2016-07-28 14 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); 

?> 

考慮のコメントをしてください

+5

義務、あなた-持っ-SQLインジェクションの脆弱性 - 、および 'mysql_ *' -is-時代遅れ、コメントを参照してください。 –

+3

必要な読書:[PHPでSQLインジェクションを防ぐにはどうすればいいですか?](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

私はそれを行う方法がわからないとどのように私は同じページに変数にisset($_GET['type'])値の値を格納しますAlexander O'Maraはユーザーの入力を消毒し、最新の機能を使用しています。関数(「I」末尾せず)をmysql_はPHP 5.5.0以降廃止され、あなたが変数にタイプを格納し、例えば、その上にスイッチを使用することができる

0

PHP 7から除去されています

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

Switch

関連する問題