Reputation: 1
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.
Upvotes: 0
Views: 1108
Reputation: 57
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.
Upvotes: 1