How to modify form data in php

coldplay.xixi
Release: 2023-03-06 11:18:01
Original
3613 people have browsed it

How to modify form data in php: First set the code to [type="hidden"]; then modify the selection of the default value of the page, the code is [$db = new MySQLi("localhost","root" ,"root","dbname")].

How to modify form data in php

How to modify form data in php:

1. The code item is the primary key in the database and cannot be changed. , so set it to type="hidden". You can also use type="text" style="display:none". The readonly attribute can also be used, but the disable attribute cannot be used. (Disable means to make it unavailable. Although it cannot be modified, it will not pass information to the back.)

2. It is easier to write gender with the ternary operator.

3. Modify the selection of page default value.

4. $db = new MySQLi("localhost","root","root","dbname"); You only need to write it once in this page, and you do not need to write it again in the php code inserted later. .

The first page xiugai.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<h1>修改数据</h1>
<?php
$code = $_GET["code"];
$db = new MySQLi("localhost","root","root","dbname");
$sql = "select * from info where code=&#39;{$code}&#39;";
$result = $db->query($sql);
$arr = $result->fetch_row();
?>
<form action="update.php" method="post">
    <div>
    <input type="hidden" name="code" value="<?php echo $arr[0]; ?>" />
    </div>
    
    <div>姓名:
    <input type="text" name="name" value="<?php echo $arr[1]; ?>" />
    </div>
    <div>性别:
    <input type="radio" name="sex" value="1" <?php echo $arr[2]?"checked=&#39;checked&#39;":""; ?> />男
    <input type="radio" name="sex" value="0" <?php echo $arr[2]?"":"checked=&#39;checked&#39;"; ?> />女
    </div>
    <div>民族:
    <select name="nation">
        <?php
        $snation = "select * from nation";
        $rnation = $db->query($snation);
        while($attr = $rnation->fetch_row())
        {
            //判断将要输出的民族是否和该人员的相同
            
            if($arr[3]==$attr[0])
            {
                echo "<option value=&#39;{$attr[0]}&#39; selected=&#39;selected&#39;>{$attr[1]}</option>";
            }
            else
            {
                echo "<option value=&#39;{$attr[0]}&#39;>{$attr[1]}</option>";
            }    
        }
        ?>
    </select>
    </div>
    <div>生日:
    <input type="text" name="birthday" value="<?php echo $arr[4] ?>" />
    </div>
    <div><input type="submit" value="修改" /></div>
</form>
</body>
</html>
Copy after login

The second page update.php

<?php
$code = $_POST["code"];
$name = $_POST["name"];
$sex = $_POST["sex"];
$nation = $_POST["nation"];
$birthday = $_POST["birthday"];
$db = new MySQLi("localhost","root","root","dbname");
$sql = "update info set name=&#39;{$name}&#39;,sex={$sex},nation=&#39;{$nation}&#39;,birthday=&#39;{$birthday}&#39; where code=&#39;{$code}&#39;";
if($db->query($sql))
{
    header("location:main.php");
}
else
{
    echo "修改失败!";
}
Copy after login

If you want to learn more about programming, please pay attention php training column!

The above is the detailed content of How to modify form data in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template