donglun2024 2014-02-09 02:51
浏览 80

使用php表单更新mysql数据库的多个字段

I want to to pull all fields from a row in my table into a form, update them, and post them back to the database. This is what I have, everything from my table row is pulling into the form but when I update I get undefined variable for $row.

<?php
include("header.php");
include("config.php"); 
if( isset($_GET['edit']))
{
        $id = $_GET['edit'];
        $result= mysql_query("SELECT * FROM customers");
        $row= mysql_fetch_array($result);
}
if ( isset($_POST['id'], $_POST['fName'], $_POST['lname'], $_POST['telNum'], $_POST['address'], $_POST['city'], $_POST['state'], $_POST['zip'], $_POST['email'], $_POST['service'], $_POST['notes']))
{
    $id = $_POST['id'];
    $fName = $_POST['fName'];
    $lName = $_POST['lName'];
    $telNum = $_POST['telNum'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zip = $_POST['zip'];
    $email =$_POST['email'];
    $service = $_POST['service'];
    $notes = $_POST['notes'];
    $sqlFn = "UPDATE customers SET fName = $fname WHERE id = $id";
    $sqlLn = "UPDATE customers SET lName = $lName WHERE id = $id";
    $sqlTelNum = "UPDATE customers SET telNum = $telNum WHERE id = $id";
    $sqlAddress = "UPDATE customers SET address = $address WHERE id = $id";
    $sqlCity = "UPDATE customers SET city = $city WHERE id = $id";
    $sqlState = "UPDATE customers SET state = $state WHERE id = $id";   
    $sqlZip = "UPDATE customers SET zip = $zip WHERE id = $id";
    $sqlEmail = "UPDATE customers SET email = $email WHERE id = $id";
    $sqlService = "UPDATE customers SET service = $service WHERE id = $id";
    $sqlNotes = "UPDATE customers SET notes = $notes WHERE id = $id";
    $result = mysql_query($sqlFn, $sqlLn, sqlTelNum, sqlAdress, sqlCity, 
                          sqlState, sqlZip, sqlEmail, sqlService, sqlNotes)
                          or die("Could not update".mysql_error());
        echo "<meta http-equiv='refresh' content='0;url=viewClients.php'>";
}

?>

<form action="edit.php" method="post">
    <div class="CSSTableGenerator" >
<table> 
    <tr>
    </tr>
    <tr>
        <td>ID:</td>
        <td><input type="text" name="id" value="<?php echo $row[0]; ?>"></td>
    <tr>
        <td>First Name:</td>
        <td><input type="text" name="fName" value="<?php echo $row[1]; ?>"></td>
    </tr>
    <tr>
        <td>Last Name:</td>
        <td><input type="text" name="lName" value="<?php echo $row[2]; ?>"></td>
    </tr>
    <tr>
        <td>Telephone #:</td>
        <td><input type="text" name="telNum" value="<?php echo $row[3]; ?>"></td>
    </tr>
    <tr>
        <td>Street Address:</td>
        <td><input type="text" name="address" value="<?php echo $row[4]; ?>"></td>
    </tr>
    <tr>
        <td>City:</td>
        <td><input type="text" name="city" value="<?php echo $row[5]; ?>"></td>
    </tr>
    <tr>
        <td>State:</td>
        <td><input type="text" name="state" value="<?php echo $row[6]; ?>"></td>
    </tr>
    <tr>
        <td>Zip:</td>
        <td><input type="text" name="zip" value="<?php echo $row[7]; ?>"></td>
    </tr>
    <tr>
        <td>Email:</td>
        <td><input type="text" name="email" value="<?php echo $row[8]; ?>"></td>
    </tr>
    <tr>
        <td>Service:</td>
        <td><input type="text" name="service" value="<?php echo $row[9]; ?>"></td>
    </tr>
    <tr>
        <td>Notes:</td>
        <td><input type="text" name="notes" value="<?php echo $row[10]; ?>"></td>
    </tr>
</table>
</div>
<div class="CSSTableGenerator" >
<table>
    <tr>
        <td><input type="submit" value="Update"/></td>
    </tr>
</table>
</div>
</form>

Also each of these edit links is only pulling data from the primary key in the first row. I want the edit link in the last column of each row to pull from the primary key in the first column of each row.

    <?php
include("header.php");
include("config.php"); // connect to database

mysql_query("INSERT INTO customers (id, fName, lName, telNum, address, city, state, zip, email, service, notes)
VALUES ('$_POST[id]', '$_POST[fName]', '$_POST[lName]', '$_POST[telNum]', '$_POST[address]', '$_POST[city]', '$_POST[state]', '$_POST[zip]', '$_POST[email]', '$_POST[service]', '$_POST[notes]')")
    or die(mysql_error());
$id = $_POST['id'];
$fName = $_POST['fName'];
$lName = $_POST['lName'];
$telNum = $_POST['telNum'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$service = $_POST['service'];
$notes = $_POST['notes'];


echo "<h1>New Client Added</h1>";
echo <<<HTML
<html>
<head>
<link rel ="stylesheet" type="text/css" href="sample.css"/>
<link rel ="stylesheet" type="text/css" href="TableCSSCode.css"/>
</head>
<body>
<div class="CSSTableGenerator">
<table> 
    <tr>
        <td>ID</td>
        <td>First Name</td>
        <td>Last Name</td>
        <td>Telephone #</td>
        <td>Street Address</td>
        <td>City</td>
        <td>State</td>
        <td>Zip</td>
        <td>Email</td>
        <td>Service</td>
        <td>Notes</td>
    </tr>
    <tr>
        <td>$id</td>
        <td>$fName</td>
        <td>$lName</td>
        <td>$telNum</td>
        <td>$address</td>
        <td>$city</td>
        <td>$state</td>
        <td>$zip</td>
        <td>$email</td>
        <td>$service</td>
        <td>$notes</td>
    </tr>

</table>
</body>
</html>
HTML;


?>

Forgive me if this is a mess, I'm totally new to coding, I'm doing this for a project in an application program development class and my Prof. doesn't really seem to know what she is teaching. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongshuxi3105 2014-02-09 03:08
    关注

    Give the submit button a name and check whether it is set or not and replace all the other fields in the if condition. And redirect the update action to another page and update it there.

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)