我有一個從數據庫獲取信息的頁面,並且應該在提交時更新信息。我使用「switch」語句,但它不能識別提交按鈕的值。表格代碼位於「personal_change_form.inc」文件中。代碼如下。提前感謝您提供任何幫助。 personal_change.phpPHP switch語句無法識別提交按鈕
<?php
/* Name: personal_change.php
* Desc: This page updates information if there is a change
*/
include("misc.inc");
session_start();
if(@$_SESSION['auth'] != "yes")
{
header("Location:user_login.php");
exit();
}
switch ($_POST['button'])
{
case "Update":
$connect = mysqli_connect($host,$user,$password,$database)
or die("Couldn't connect to database.");
$sql = "SELECT * FROM data WHERE loginName='{$_SESSION['logname']}'";
$result = mysqli_query($connect,$sql)
or die ("Couldn't execute query.");
$row= mysqli_fetch_assoc($result);
extract($row);
if($value != $_POST['value'])
{
$sql = "INSERT INTO data (loginName,createDate,password, firstName,lastName,title,gender,course,sector,experience,qualific,location,mobile,email) VALUES ('$_POST[loginName]',
'',
SHA1('$_POST[password]'),
'$_POST[firstName]',
'$_POST[lastName]',
'$_POST[Title]',
'$_POST[Gender]',
'$_POST[course]',
'$_POST[Sector]',
'$_POST[experience]',
'$_POST[qualific]',
'$_POST[Location]',
'$_POST[mobile]',
'$_POST[email]')";
mysqli_query($connect,$sql)
or die("Couldn't execute query");
header("location:personal.php");
}
break;
default:
include("personal_change_form.inc");
}
?>
personal_change_form.inc
<?php
/* Program: personal_change_form.inc
* Desc: This program displays information from the database
* and allow user to change it.
*/
include("misc.inc");
$personal_change_array = array ("loginName" => "User Name:",
"password" => "Password:",
"firstName" => "First Name:",
"lastName" => "Last Name:",
"title" => "Title:",
"gender" => "Gender:",
"course" => "Courses offered:",
"sector" => "Industry sector:",
"experience"=> "Trainer experience(years):",
"qualific" => "Academic qualifications:",
"location" => "Location\City:",
"mobile" => "Mobile:",
"email" => "E-mail:");
?>
<html>
<head><title>Update Information</title></head>
<style type="text/css">
<!--
label {font-weight:bold; float:left; width:27%;
margin-right: .5em; text-align:right;}
fieldset {border:2px solid #000000 }
legend {font-weight:bold; font-size: 1.2em;
margin-bottom: 20px; text-align:center; padding-left:20px; padding-right:20px;border: 1px solid #888;
border-right: 1px solid #666;
border-bottom: 1px solid #666;
padding: 5px;
background-color: rgba(255,102,0,1.00)}
h3 {text-align: center; margin: 2em;}
#wrapper {margin:0; padding: 0;}
#login {position: absolute; left:0; width:40%;
padding: 1em 0; }
#reg {position:absolute; left:40%; width:60%;
padding: 1em 0;}
#field {padding-bottom: .5em}
.menu {margin-left:150px; padding-bottom: .5em; width:204px}
.errors {font-weight: bold; font-size:12px font-style:italic;
font-size:90%; color: red; margin-top:0; text-align:center}
-->
</style>
<body>
<form action="<?php $_SERVER['PHP_SELF']?>"
method="POST">
<fieldset><legend>UPDATE INFORMATION</legend>
<?php
echo "<table style='width:20%; left:25%; position:absolute; font-weight:bold' cellspacing='28.5'>\n";
foreach($personal_change_array as $field=>$value)
{
echo "<tr><td>$value</td></tr>";
}
echo "</table>";
echo "<table style='width:10%; left:25%; position:relative; font-weight:bold' cellspacing='15'>\n";
$connect = mysqli_connect($host,$user,$password,$database)
or die("Couldn't connect to database.");
$sql = "SELECT * FROM data WHERE loginName='{$_SESSION['logname']}'";
$result = mysqli_query($connect,$sql)
or die ("Couldn't execute query.");
$row= mysqli_fetch_assoc($result);
foreach($row as $field => $value)
{
if($field!="createDate")
{
if($field=="password")
{
$type='password';
}
else
{
$type='text';
}
echo "<tr><td><input ' type='$type' value=$value></input></td></tr>";
}
}
echo "</table>";
?>
<input type="submit" name="button"
style='margin-left:40%; margin-bottom:.5em; margin-top:30px' value="Update" />
</fieldset></form>
</body>
</html>
我已經試過你的代碼,但即使按鈕不工作(不能點擊,不發送表格)。也許是因爲其他一些錯誤?例如,personal_change_form.inc中的行'echo「
'sha1'不是一個安全的哈希。使用['password_hash'](http://php.net/manual/en/function.password-hash.php)和['password_verify'](http://php.net/manual/en/function.password- verify.php)散列密碼。 – apokryfos
@GezaBoehm您提到的這行不是按鈕,它是表單輸入字段。按鈕行如下: –