我想将表格中一行中的所有字段拉入表单,更新它们,然后将它们发回数据库。这就是我所拥有的,从我的表行中的所有东西都拉进了表单但是当我更新时,我得到$ 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>
此外,每个编辑链接仅从第一行的主键中提取数据。我希望每行最后一列中的编辑链接从每行第一列中的主键拉出。
<?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;
?>
请原谅我,如果这是一团糟,我对编码很新,我正在为应用程序开发课程中的一个项目做这个,而我的教授似乎并不知道她在教什么。提前谢谢。
答案 0 :(得分:1)
为提交按钮指定名称并检查其是否已设置并替换if条件中的所有其他字段。 并将更新操作重定向到另一个页面并在那里更新。