0
我想將我表中一行中的所有字段拖入表單中,更新它們並將它們發回數據庫。這就是我所擁有的,從我的表格行中的所有東西都拉到表格中,但是當我更新時,我得到了$ row的未定義變量。使用一個PHP表單更新mysql數據庫的多個字段
<?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;
?>
原諒我,如果這是一個爛攤子,我完全新的編碼,我這樣做是爲應用程序開發類項目和我的教授並沒有真正似乎知道她正在教學。提前致謝。
當$ _GET ['edit']被設置時,$ row只會在您的第一個代碼片段中被設置。在高度多餘的更新調用的另一個路徑中,您永遠不會讀取一行,$ row永遠不會被設置。同樣,你的教授正在向[SQL注入攻擊](http://bobby-tables.com)敞開大門。 –
1'mysql_query'請求每個查詢,它不能爲您運行無限量(儘管您可以重寫爲_one_查詢)。 (你真的不應該使用已棄用的'mysql_ *'函數)。 – Wrikken
我將如何重寫爲一個查詢?你能否給我一個資源來幫助我解決我想要做的事情? – user3288696