2016-10-25 20 views
0

ラジオボタンの値をデータベースから取得して編集できるようにします。これをどうやって行うのですか?すべてが正常に挿入が、私がしようと検索したとき、私はのエラーが出る "注意:未定義のインデックス:CでisVolunteer:\ xamppの\ htdocsに\ php_insert_update_delete_search.phpライン38上の"データベースからラジオボタンの値を取得

マイコード:

<?php 
$host = "localhost"; 
$user = "root"; 
$password =""; 
$database = "2104299_test"; 

$personID = ""; 
$personFName = ""; 
$personLName = ""; 
$personAddress = ""; 
$personContact = ""; 
$personEmail = ""; 
$personPassword = ""; 
$isVolunteer = ""; 


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 

// connect to mysql database 
try{ 
$connect = mysqli_connect($host, $user, $password, $database); 
} catch (mysqli_sql_exception $ex) { 
echo 'Error'; 
} 

// get values from the form 
function getPosts() 
{ 
$posts = array(); 
$posts[0] = $_POST['personID']; 
$posts[1] = $_POST['personFName']; 
$posts[2] = $_POST['personLName']; 
$posts[3] = $_POST['personAddress']; 
$posts[4] = $_POST['personContact']; 
$posts[5] = $_POST['personEmail']; 
$posts[6] = $_POST['personPassword']; 
$posts[7] = $_POST['isVolunteer']; 
return $posts; 
} 

// Search 

if(isset($_POST['search'])) 
{ 
$data = getPosts(); 

$search_Query = "SELECT * FROM tbl_people WHERE personID = $data[0]"; 

$search_Result = mysqli_query($connect, $search_Query); 

if($search_Result) 
{ 
    if(mysqli_num_rows($search_Result)) 
    { 
     while($row = mysqli_fetch_array($search_Result)) 
     { 
      $personID = $row['personID']; 
      $personFName = $row['personFName']; 
      $personLName = $row['personLName']; 
      $personAddress = $row['personAddress']; 
      $personContact = $row['personContact']; 
      $personEmail = $row['personEmail']; 
      $personPassword = $row['personPassword']; 
      $isVolunteer = $row['isVolunteer']; 
     } 
    }else{ 
     echo 'No Data For This Id'; 
    } 
}else{ 
    echo 'Result Error'; 
} 
} 


// Insert 
if(isset($_POST['insert'])) 
{ 
$data = getPosts(); 
$insert_Query = "INSERT INTO `tbl_people`(`personFName`, `personLName`, `personAddress`,`personContact`,`personEmail`,`personPassword`,`isVolunteer`) 
VALUES ('$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]')"; 
try{ 
    $insert_Result = mysqli_query($connect, $insert_Query); 

    if($insert_Result) 
    { 
     if(mysqli_affected_rows($connect) > 0) 
     { 
      echo 'Data Inserted'; 
     }else{ 
      echo 'Data Not Inserted'; 
     } 
    } 
} catch (Exception $ex) { 
    echo 'Error Insert '.$ex->getMessage(); 
} 
} 

// Delete 
if(isset($_POST['delete'])) 
{ 
$data = getPosts(); 
$delete_Query = "DELETE FROM `tbl_people` WHERE `personID` = $data[0]"; 
try{ 
    $delete_Result = mysqli_query($connect, $delete_Query); 

    if($delete_Result) 
    { 
     if(mysqli_affected_rows($connect) > 0) 
     { 
      echo 'Data Deleted'; 
     }else{ 
      echo 'Data Not Deleted'; 
     } 
    } 
} catch (Exception $ex) { 
    echo 'Error Delete '.$ex->getMessage(); 
} 
} 

// Edit 
if(isset($_POST['update'])) 
{ 
$data = getPosts(); 
$update_Query = "UPDATE `tbl_people` SET `personFName`= '$data[1]',`personLName`= '$data[2]',`personAddress`= '$data[3]', 
`personContact`= '$data[4]',`personEmail`= '$data[5]',`personPassword`= '$data[6]',`isVolunteer`= '$data[7]' 
WHERE `personID` = $data[0]"; 
try{ 
    $update_Result = mysqli_query($connect, $update_Query); 

    if($update_Result) 
    { 
     if(mysqli_affected_rows($connect) > 0) 
     { 
      echo 'Data Updated'; 
     }else{ 
      echo 'Data Not Updated'; 
     } 
    } 
    } catch (Exception $ex) { 
    echo 'Error Update '.$ex->getMessage(); 
    } 
} 



?> 


<!DOCTYPE Html> 
<html> 
<head> 
    <title>PHP INSERT UPDATE DELETE SEARCH</title> 
</head> 
<body> 
    <form action="php_insert_update_delete_search.php" method="post"> 
     Person ID<br><input type="number" name="personID" placeholder="ID" value="<?php echo $personID;?>"><br><br> 
     First Name<br><input type="text" name="personFName" placeholder="First Name" value="<?php echo $personFName;?>"><br><br> 
     Last Name<br><input type="text" name="personLName" placeholder="Last Name" value="<?php echo $personLName;?>"><br><br> 
     Address<br><input type="text" name="personAddress" placeholder="Address" value="<?php echo $personAddress;?>"><br><br> 
     Contact Number<br><input type="mobile" name="personContact" placeholder="Contact" value="<?php echo $personContact;?>"><br><br> 
     Email<br><input type="email" name="personEmail" placeholder="Email" value="<?php echo $personEmail;?>"><br><br> 
     Password<br><input type="text" name="personPassword" placeholder="Password" value="<?php echo $personPassword;?>"><br><br> 
     Active Or Not:<br> 
     <input type="radio" name="isVolunteer" value="<?php echo $isVolunteer;?>" />Yes 
     <input type="radio" name="isVolunteer" value="<?php echo $isVolunteer;?>" />No 
     <br><br> 
     <div> 
      <!-- Input For Add Values To Database--> 
      <input type="submit" name="insert" value="Add"> 

      <!-- Input For Edit Values --> 
      <input type="submit" name="update" value="Update"> 

      <!-- Input For Clear Values --> 
      <input type="submit" name="delete" value="Delete"> 

      <!-- Input For Find Values With The given ID --> 
      <input type="submit" name="search" value="Find"> 
     </div> 
    </form> 
    </body> 
</html> 
+0

あなたが響いている「PERSパスワードで "ラジオの値に? wtf – Phiter

+0

どちらのラジオボタンもチェックされていなければ、 'isVolunteer'はフォームと一緒には送信されません - チェックされていないラジオ/チェックボックスは" vegasに滞在 "します。あなたは単にすべてのフォームフィールドが記入されているとみなしています。また、[SQLインジェクション攻撃](http://bobby-tables.com)にも脆弱です。 –

+0

@PhiterFernandes申し訳ありませんが私のために間違いでした。 –

答えて

0

<div class="container"> 
 
<div class="main"> 
 

 
<form action="form.php" method="post"> 
 
<!---- Radio Button Starts Here -----> 
 
<label class="heading">Radio Buttons :</label> 
 
<input name="radio" type="radio" value="Radio 1">Radio 1 
 
<input name="radio" type="radio" value="Radio 2">Radio 2 
 
<input name="radio" type="radio" value="Radio 3">Radio 3 
 
<input name="radio" type="radio" value="Radio 4">Radio 4 
 
<?php include'radio_value.php'; ?> 
 
<input name="submit" type="submit" value="Get Selected Values"> 
 
</form> 
 
</div> 
 
</div>

リンク: - https://www.formget.com/php-select-option-and-php-radio-button/

関連する問題