停留在我实验室的一部分上,它要求创建一个动态链接,以便取消选中'来自数据库的值。我该怎么做呢?其内容如下:
"修改您的上一个程序,以便在每条记录的末尾都有一个动态链接,表示"取消"。点击后,它应该"取消选中"星期一和星期二(也就是说,更改您存储在数据库中的值以表示"未选中"对于星期一和星期二"(我将如何表示此变量?)
我在页面加载时也收到未定义的变量警告。在指出我正确的方向时,任何帮助都是值得赞赏的。谢谢!
.php文件的代码:
<?php
if ($_POST){
$checkErr = $firstErr = $lastErr = $orgErr = $emailErr = $phoneErr = "";
$title=$_POST['title'];
$firstName=$_POST['firstName'];
$lname=$_POST['lastName'];
$organization=$_POST['organization'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$Monday=$_POST['monday'];
$Tuesday=$_POST['tuesday'];
$size = $_POST['t-shirt'];
if (empty($_POST["firstName"])) {
$firstErr = "First name is required";
}else {
$firstName=$_POST['firstName'];
}
if (empty($_POST["lastName"])) {
$lastErr = "Last name is required";
}else {
$lastName=$_POST['lastName'];
}
if (empty($_POST["organization"])) {
$orgErr = "Organization is required";
}else {
$organization=$_POST['organization'];
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
}else {
$email=$_POST['email'];
}
if (empty($_POST["phone"])) {
$phoneErr = "Phone number is required";
}else {
$phone=$_POST['phone'];
}
}
$dbserver = "142.204.000.06;
$uid = "dummy";
$pw = "dummy";
$dbname = "dummy";
//Connect to the mysql server and get back our link_identifier
$link = mysqli_connect($dbserver, $uid, $pw, $dbname) or die('Could not connect: ' . mysqli_error($link). "<br>");
$sql_query = 'INSERT INTO lab3 set title="'. $title .'",firstname="' . $firstName . '", lastname="' . $lastName . '", organization="'. $organization .'",phone="'. $phone . '", email="'. $email .'", ATTENDING_MON="'. $Monday .'", ATTENDING_TUE="'. $Tuesday .'", Size="'. $size .'"' ;echo "<br>";
$result = mysqli_query($link, $sql_query) or die('Error with your query '. mysqli_error($link)). '<br>';
$sql_query = "SELECT * from lab3";
$result = mysqli_query($link, $sql_query) or die('Error with your query '. mysqli_error($link)). '<br>';
?>
<table border="1">
<tr>
<th>Title</th><th>First Name</th><th>Last Name</th> <th>Organization</th> <th>Email</th><th>Phone Number</th><th> Day Attanding </th><th>T-Shirt size</th>
<?php
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php print $row['TITLE']; ?></td>
<td><?php print $row['FIRSTNAME']; ?></td>
<td><?php print $row['LASTNAME']; ?></td>
<td><?php print $row['ORGANIZATION'];?></td>
<td><?php print $row['EMAIL']; ?></td>
<td><?php print $row['PHONE']; ?></td>
<td><?php print $row['ATTENDING_MON']; ?> <?php print $row['ATTENDING_TUE']; ?> </td>
<td><?php print $row['SIZE']; ?></td>
</tr>
<?php
}//ending while loop
echo"</table>\n";
mysqli_free_result($result);
mysqli_close($link);
?>
<html>
<head>
<title>FSOSS Registration</title>
</head>
<body>
<h1>FSOS Registration</h1>
<form method="post" >
<table>
<tr>
<td valign="top">Title:</td>
<td>
<table>
<tr>
<td><input type="radio" name="title" value="mr" <?php if ($_POST['title'] == 'mr') {echo "CHECKED";} ?>>Mr</td>
</tr>
<tr>
<td><input type="radio" name="title" value="mrs" <?php if ($_POST['title'] == 'mrs') {echo "CHECKED";} ?> >Mrs</td>
</tr>
<tr>
<td><input type="radio" name="title" value="ms" <?php if ($_POST['title'] == 'ms') {echo "CHECKED";} ?> >Ms</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>First name:</td>
<td><input name="firstName" type="text" value="<?php if(isset($_POST['firstName'])){echo $_POST['firstName'];} ?>"><?php if(isset($_POST['submit'])){ echo $firstErr; }?></td>
</tr>
<tr>
<td>Last name:</td>
<td><input name="lastName" type="text" value="<?php if(isset($_POST['lastName'])){echo $_POST['lastName'];} ?>"><?php if(isset($_POST['submit'])){ echo $lastErr; }?></td>
</tr>
<tr>
<td>Organization:</td>
<td><input name="organization" type="text" value="<?php if(isset($_POST['organization'])){echo $_POST['organization'];} ?>"><?php if(isset($_POST['submit'])){ echo $orgErr; }?></td>
</tr>
<tr>
<td>Email address:</td>
<td><input name="email" type="text" value="<?php if(isset($_POST['email'])){echo $_POST['email'];} ?>"><?php if(isset($_POST['submit'])){ echo $emailErr; }?></td>
</tr>
<tr>
<td>Phone number:</td>
<td><input name="phone" type="text" value="<?php if(isset($_POST['phone'])){echo $_POST['phone'];} ?>"><?php if(isset($_POST['submit'])){ echo $phoneErr; }?></td>
</tr>
<tr>
<td>Days attending:</td>
<td>
<input name="monday" type="checkbox" value="monday" <?php if (isset($_POST['monday'])) {echo "CHECKED";}?>>Monday
<input name="tuesday" type="checkbox" value="tuesday" <?php if (isset($_POST['tuesday'])) {echo "CHECKED";}?>>Tuesday <td/>
</tr>
<tr>
<td>T-shirt size:</td>
<td>
<select name="t-shirt">
<option>--Please choose--</option>
<option name="small" value="s" <?php if($_POST['t-shirt'] == 's'){echo('selected');} ?> >Small</option>
<option value="m" <?php if($_POST['t-shirt'] == 'm'){echo('selected');} ?>>Medium</option>
<option value="l" <?php if($_POST['t-shirt'] == 'l'){echo('selected');} ?>>Large</option>
<option value="xl" <?php if($_POST['t-shirt'] == 'xl'){echo('selected');} ?>>X-Large</option>
</select>
</td>
</tr>
<tr><td><br></td></tr>
<tr>
<td></td>
<td><input name="submit" type="submit"></td>
</tr>
</form>
</body>
</html>
答案 0 :(得分:0)
嗨,您收到undefind
variavble
警告,因为首次访问的网页loads
没有form
values
它始终没有$_POST
变量用isset
检查一下。这将停止您的警告,因此将if($_POST)
更改为if(isset($_POST))
或if(isset($_POST) && !empty($_POST))
。我更喜欢第二种选择
您好我已经检查了您的所有代码并自己完成了,如果您发现它可以满足您的需求,请按照我的步骤进行操作
我已使用此查询在我的数据库中创建了一个表
CREATE TABLE IF NOT EXISTS `lab` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(20) DEFAULT NULL,
`firstname` varchar(20) NOT NULL DEFAULT '',
`lastname` varchar(255) NOT NULL,
`organization` varchar(111) NOT NULL,
`phone` int(10) NOT NULL,
`email` varchar(30) NOT NULL,
`ATTENDING_MON` char(1) NOT NULL,
`ATTENDING_TUE` char(1) NOT NULL,
`Size` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
)
并使用此代码来完成您的任务
index.php
<?php
$dbserver = "localhost";
$uid = "root";
$pw = "";
$dbname = "project";
//Connect to the mysql server and get back our link_identifier
$link = mysqli_connect($dbserver, $uid, $pw, $dbname) or die('Could not connect: ' . mysqli_error($link). "<br>");
if(isset($_REQUEST['did'])){
$did= $_REQUEST['did'];
if($did)
{//if you want to delete record use this
//mysqli_query($link,"delete from lab where id='$did'");
// if you want to set N in days select this
mysqli_query($link,"UPDATE lab SET ATTENDING_MON ='N' , ATTENDING_TUE = 'N' WHERE id='$did'");
}}
if(isset($_POST) && !empty($_POST)){
$checkErr = $firstErr = $lastErr = $orgErr = $emailErr = $phoneErr = "";
$title=$_POST['title'];
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$organization=$_POST['organization'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$monday=isset($_POST['monday']) ? 'Y' : '';
$tuesday=isset($_POST['tuesday']) ? 'Y' : '';
$size = $_POST['t-shirt'];
if(empty($monday) && empty($tuesday)) {
$checkErr = "Day is required";
}
if (empty($firstName)) {
$firstErr = "First name is required";
}
if (empty($lastName)) {
$lastErr = "Last name is required";
}
if (empty($organization)) {
$orgErr = "Organization is required";
}
if (empty($email)) {
$emailErr = "Email is required";
}
if (empty($phone)) {
$phoneErr = "Phone number is required";
}
}
if(isset($checkErr) && $checkErr == '' && $firstErr == '' && $lastErr == '' && $orgErr == '' && $emailErr == '' && $phoneErr == ""){
$sql_query = 'INSERT INTO lab set title="'. $title .'",firstname="' .$firstName . '",lastname="' . $lastName . '", organization="'. $organization .'",phone="'. $phone . '", email="'. $email .'", ATTENDING_MON="'. $monday .'", ATTENDING_TUE="'. $tuesday .'", Size="'. $size .'"' ;echo "<br>";
//echo $sql_query;die;
$result = mysqli_query($link, $sql_query) or die('Error with your query '. mysqli_error($link)). '<br>';
$_POST = array();
}
$sql_query = "SELECT * from lab";
$result = mysqli_query($link, $sql_query) or die('Error with your query '. mysqli_error($link)). '<br>';
?>
<table border="1">
<tr>
<th>Title</th><th>First Name</th><th>Last Name</th> <th>Organization</th> <th>Email</th><th>Phone Number</th><th> Day Attanding </th><th>T-Shirt size</th><th>Action</th>
<?php
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php print $row['title']; ?></td>
<td><?php print $row['firstname']; ?></td>
<td><?php print $row['lastname']; ?></td>
<td><?php print $row['organization'];?></td>
<td><?php print $row['email']; ?></td>
<td><?php print $row['phone']; ?></td>
<td><?php print $row['ATTENDING_MON'] == 'Y' ? 'Monday': '' ?> <?php print $row['ATTENDING_TUE'] == 'Y' ? 'Tuesday': '' ?> </td>
<td><?php print $row['Size']; ?></td>
<td><a href="index.php?did=<?php echo $row['id'];?>">Cancil</a></td>
</tr>
<?php
}//ending while loop
echo"</table>\n";
mysqli_free_result($result);
mysqli_close($link);
?>
<html>
<head>
<title>FSOSS Registration</title>
</head>
<body>
<h1>FSOS Registration</h1>
<form method="post" >
<table>
<tr>
<td valign="top">Title:</td>
<td>
<table>
<tr>
<td><input type="radio" name="title" value="mr" <?php if(isset($_POST['title']) && $_POST['title'] == 'mr'){echo "CHECKED";
}?> >Mr</td>
</tr>
<tr>
<td><input type="radio" name="title" value="mrs" <?php if(isset($_POST['title']) && $_POST['title'] == 'mrs'){echo "CHECKED";
}?>>Mrs</td>
</tr>
<tr>
<td><input type="radio" name="title" value="ms" <?php if(isset($_POST['title']) && $_POST['title'] == 'ms'){echo "CHECKED";
}?>>Ms</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>First name:</td>
<td><input name="firstName" type="text" value="<?php if(isset($_POST['firstName'])){echo $_POST['firstName'];} ?>"><?php if(isset($_POST['submit'])){echo $firstErr; }?></td>
</tr>
<tr>
<td>Last name:</td>
<td><input name="lastName" type="text" value="<?php if(isset($_POST['lastName'])){echo $_POST['lastName'];} ?>"><?php if(isset($_POST['submit'])){ echo $lastErr; }?></td>
</tr>
<tr>
<td>Organization:</td>
<td><input name="organization" type="text" value="<?php if(isset($_POST['organization'])){echo $_POST['organization'];} ?>"><?php if(isset($_POST['submit'])){ echo $orgErr; }?></td>
</tr>
<tr>
<td>Email address:</td>
<td><input name="email" type="text" value="<?php if(isset($_POST['email'])){echo $_POST['email'];} ?>"><?php if(isset($_POST['submit'])){ echo $emailErr; }?></td>
</tr>
<tr>
<td>Phone number:</td>
<td><input name="phone" type="text" value="<?php if(isset($_POST['phone'])){echo $_POST['phone'];} ?>"><?php if(isset($_POST['submit'])){ echo $phoneErr; }?></td>
</tr>
<tr>
<td>Days attending:</td>
<td>
<input name="monday" type="checkbox" value="monday"<?php if (isset($_POST['monday'])) {echo "CHECKED";}?>>Monday
<input name="tuesday" type="checkbox" value="tuesday"<?php if (isset($_POST['tuesday'])) {echo "CHECKED";}?>>Tuesday <?php if(isset($_POST['submit'])){ echo $checkErr; }?><td/>
</tr>
<tr>
<td>T-shirt size:</td>
<td>
<select name="t-shirt">
<option>--Please choose--</option>
<option value="s" <?php if(isset($_POST['t-shirt']) && $_POST['t-shirt'] == 's'){echo('selected');} ?> >Small</option>
<option value="m"<?php if(isset($_POST['t-shirt']) && $_POST['t-shirt'] == 'm'){echo('selected');} ?>>Medium</option>
<option value="l"<?php if(isset($_POST['t-shirt']) && $_POST['t-shirt'] == 'l'){echo('selected');} ?>>Large</option>
<option value="xl"<?php if(isset($_POST['t-shirt']) && $_POST['t-shirt'] == 'xl'){echo('selected');} ?>>X-Large</option>
</select>
</td>
</tr>
<tr><td><br></td></tr>
<tr>
<td></td>
<td><input name="submit" type="submit" value="submit"></td>
</tr>
</form>