我有简单的HTML表单,有两个按钮(搜索和更新)。 下面的代码将通过" Search"从MySQL检索值。按钮,然后检索到的值"年龄"将通过按钮"更新"。
插入数据库HTML表单包含字段:
所以,我真正需要的是帮助......
如何以HTML格式管理,只有Search Key(privateno)字段可编辑,其他所有字段为#34;只读"直到和除非如果" privateno"确实存在于数据库中(在这种情况下,字段值将从数据库中自动插入)?
<?php
// php code to search data in mysql database and set it in input text
if(isset($_POST['search']))
{
// id to search
$id = $_POST['id'];
// connect to mysql
$connect = mysqli_connect("localhost", "root", "","test");
// mysql search query
$query = "SELECT `user`, `age`, `privateno` FROM `test` WHERE `privateno` = $id LIMIT 1";
$result = mysqli_query($connect, $query);
// if id exist
// show data in inputs
if(mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_array($result))
{
$user = $row['user'];
$age = $row['age'];
$privateno = $row['privateno'];
}
}
// if the id not exist
// show a message and clear inputs
else
{
echo "User doesn't exist";
$user = "";
$age = "";
$privateno = "";
}
mysqli_free_result($result);
mysqli_close($connect);
}
// in the first time inputs are empty
else
{
$user = "";
$age = "";
$privateno = "";
}
?>
<?php
if(isset($_POST['update']))
{
$age = $_POST['lname'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO test (text)
VALUES ('$age')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="index.php" method="post">
PrivateNo:<input type="text" name="id" value="<?php echo $privateno;?>"><br><br>
First Name:<input type="text" name="fname" value="<?php echo $user;?>"><br>
<br>
Age:<input type="text" name="lname" value="<?php echo $age;?>"><br><br>
<input type="submit" value="search" name="search" />
<input type="submit" value="update" name="update" />
</form>
</body>
</html>
答案 0 :(得分:0)
First Name:<input type="text" name="fname" value="<?php echo $user;?>"readonly><br>
这很简单,我的不好,我在发布这里后几分钟就得到了一个想法。
我认为如果我将字段置于“只读”模式,则不会将DB值检索到特定的“只读”字段。