我希望能够从数据库中提取单选按钮值并能够对其进行编辑。我该怎么做呢?一切都插入正常,但是当我尝试搜索时,我得到一个错误"注意:未定义索引:isVolunteer在C:\ 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 :(得分: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/