PHP编辑表A,显示到文本框然后将其保存到表B中

时间:2015-05-14 08:14:37

标签: php mysql html-table

表CREW(A) - user_id,名称,用户名,状态
表数据(B) - user_id,名称,用户名,密码,月份,状态

记录来自表格a 。我的想法是解决这个问题:

表格a 搜索记录,编辑记录 为密码和月份添加新文本框,然后将其保存到表b

我已成功从表a中提取记录但无法将其保存到表b。它能够保存记录,但只保存user_id和月份,其余列缺失。

updateform.php:

<?PHP
$sql = "SELECT * FROM crew WHERE user_id = $sel_record";
        $result = mysql_query($sql, $con) or die (mysql_error());
        if(!$result) {
            print "<h1>Something has gone wrong!</h1>";
        } else {
            while ($record = mysql_fetch_array($result)) {
                $user_id = $record['user_id'];
                $lname = $record['lname'];
                $username = $record['username'];
                $status = $record['status'];
            } 
<form id = "myForm" method="POST" action = "update.php">
    <tr>
        <td>
            <label for="user_id">Crew ID*:</label></td><td>
            <input type="text" name="user_id" id="user_id" value="$user_id" >
        </td>
    <tr>
        <td>
            <label for="lname">LastName*:</label></td><td>
            <input type="text" name="lname" id="lname" value="$lname" >
        </td>
    </tr>
<tr>
        <td>
            <label for="username">Username*:</label></td><td>
            <input type="text" name="username" id="username" value="$username" >
        </td>
    </tr>       
<tr>
        <td>
            <label for="password">Password*:</label></td><td>
            <input type="text" name="password" id="password" value="$username" >
        </td>
    </tr>                                                       
    <tr>
        <td>
            <label for="month">Month*:</label></td><td>
            <input type="text" name="month" id="month" value="$month" >
        </td>
    </tr>
<tr>
        <td>
            <label for="status">Status*:</label></td><td>
            <input type="text" name="status" id="status" value="$status" >
        </td>
    </tr>   
    <tr>
    <td id="mySubmit">
        <input type="submit" name="submit" value="Modify Record">   
    </td>
    </form>
?>

***update.php
<?PHP
include "connect.php";
$user_id = $_POST[user_id];
$lname = $_POST[lname];
$username = $_POST[username];
$password = $_POST[password];
$status = $_POST[status];
$month = $_POST[month];

$sql = "INSERT INTO data (user_id, lname, username, password, month, status) VALUES ('$user_id', '$lname', '$username', '$password', '$month', '$status')";
$result=mysql_query($sql) or die (mysql_error());`enter code here`
?>

1 个答案:

答案 0 :(得分:0)

首先,您的表单不正确,您必须更改name元素的input属性以接受输入数组。当前代码只使用最后一个。

<form id = "myForm" method="POST" action = "update.php">
    <tr>
        <td>
            <label for="user_id">Crew ID*:</label></td><td>
            <input type="text" name="users[user_id][user_id]" id="user_id" value="$user_id" >
        </td>
    <tr>
        <td>
            <label for="lname">LastName*:</label></td><td>
            <input type="text" name="users[user_id][lname]" id="lname" value="$lname" >
        </td>
    </tr>
    <tr>
        <td>
            <label for="username">Username*:</label></td><td>
            <input type="text" name="users[user_id][username]" id="username" value="$username" >
        </td>
    </tr>       
    <tr>
        <td>
            <label for="password">Password*:</label></td><td>
            <input type="text" name="users[user_id][password]" id="password" value="$username" >
        </td>
    </tr>                                                       
    <tr>
        <td>
            <label for="month">Month*:</label></td><td>
            <input type="text" name="users[user_id][month]" id="month" value="$month" >
        </td>
    </tr>
    <tr>
        <td>
            <label for="status">Status*:</label></td><td>
            <input type="text" name="users[user_id][status]" id="status" value="$status" >
        </td>
    </tr>   
    <tr>
    <td id="mySubmit">
        <input type="submit" name="submit" value="Modify Record">   
    </td>
</form>

update.php文件也需要更新。

$postUsers = $_POST['users'];

$sqlValues = array();

foreach ($postUsers as $user)
{
    $user_id = $user[user_id];
    $lname = $user[lname];
    $username = $user[username];
    $password = $user[password];
    $status = $user[status];
    $month = $user[month];

    $sqlValues[] = "('$user_id', '$lname', '$username', '$password', '$month', '$status')";
}


$sql = "INSERT INTO data (user_id, lname, username, password, month, status) VALUES " . implode(",", $sqlValues);

另外,不要再使用mysql_*命令,而是使用PDO。