0

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.

3
  • $row only gets set in your first code snippet when $_GET['edit'] is set. In the other path with the highly redundant update calls, you never fetch a row and $row never gets set. As well, your prof is leaving you wide open to SQL injection attacks. Commented Feb 9, 2014 at 2:53
  • 1 mysql_query call per query please, it cannot run an infinite amount for you (although you could rewrite is as one query). (And you really shouldn't be using the deprecated mysql_* functions anymore). Commented Feb 9, 2014 at 2:55
  • How would I rewrite as one query? Could you give me a resource to help me out with what I'm trying to do? Commented Feb 9, 2014 at 4:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.