我目前正在开展一个学校项目,并且无法使用此功能。在我的代码中,用户应该能够在textarea中插入他们的名字和/或电子邮件,当他们按下更新按钮时,应该在MySql数据库中更新信息。为了理解代码我做了两个if语句来检查值“updateUser”文件获取是否为空。
我的选择代码:
<?php
include('connection.php');
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
$firstname = mysqli_real_escape_string($conn,$_POST["firstname"]);
$email = mysqli_real_escape_string($conn,$_POST["email"]);
$sql = "SELECT * FROM employee where username ='$username'";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){ //kollar/retunerar rows i result set
echo "<table>"; //tabellen
echo "<table border=1";
echo "<tr>";
echo "<th>firstname</th>";
echo "<th>email</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){ //Fetch a result row as an array
echo "<tr>";
echo '<form class="" action="updateEmployee.php" method="post">';
echo "<textarea >" . $row['firstname'] . "</textarea>";
echo "<textarea>" . $row['email'] . "</textarea>";
echo '<input type="submit" name="updateEmp" value="uppdatera">';
echo "</form>";
echo '</tr>';
}
echo "</table>";
mysqli_free_result($result);
$_SESSION["email"] = "$email";
$_SESSION["firstname"] = "$firstname";
} else{
echo "no user.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
}
// Close connection
mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form class="" action="selectUser.php" method="post">
username <input type="text" name="username">
<input type="submit" name="" value="test">
</form>
</body>
</html>
我的更新代码:
<?php
include('connection.php');
session_start();
$firstname = $_SESSION["firstname"];
$email = $_SESSION["email"];
if ($email == "") {
echo "lname ej";
}
if ($firstname == ""){
echo "fname gick ej";
}
else {
$sql_update = mysqli_query($conn,"UPDATE employee SET firstname = '$firstname', WHERE email = '$email'");
}
mysqli_close($conn);
?>
我是PHP的新手,但有Java和C#的经验。思想过程是一样的吗?
谢谢!