How do I Update Form with SELECT & Show DB Entries

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

How do I Update Form with SELECT & Show DB Entries

Post by diseman »

Trying to learn PHP and this one piece is just not happening for me. At this point, I've probably got more hours into trying to figure it out than I care to share. Thing is, I feel like the solution must be pretty simple. Anyways, looking for some help on the part between lines 54-65 (between the two //******************).

This form saves and returns the data on all the other fields, but not for this SELECT field. How do I fix this?

Code: Select all

<?php

include_once ("_includes/_connect.php");

if (isset($_POST["submit"]))    {

    foreach($_POST["id"] AS $id)    {

        $name           = mysqli_real_escape_string($con, $_POST["name"]        [$id]);
        $relationship   = mysqli_real_escape_string($con, $_POST["relationship"][$id]);
        $address        = mysqli_real_escape_string($con, $_POST["address"]     [$id]);
        $email          = mysqli_real_escape_string($con, $_POST["email"]       [$id]);
        $phone          = mysqli_real_escape_string($con, $_POST["phone"]       [$id]);

        $update         = mysqli_query($con, "UPDATE test_dirk SET name = '$name', relationship = '$relationship', address = '$address', email = '$email', phone = '$phone' WHERE id = $id  ") ;
}
}

           $relationship_arr['0']	    	    = "Select";
           $relationship_arr['Acquaintance']    = "Acquaintance";
		   $relationship_arr['Aunt']		    = "Aunt";
           $relationship_arr['Boss']		    = "Boss";
           $relationship_arr['Boyfriend']	    = "Boyfriend";
		   $relationship_arr['Brother']		    = "Brother";
		   $relationship_arr['Daughter']	    = "Daughter";
		   $relationship_arr['Father']		    = "Father";
		   $relationship_arr['Friend']		    = "Friend";
		   $relationship_arr['Husband']		    = "Husband";
           $relationship_arr['Girlfriend']	    = "Girlfriend";
		   $relationship_arr['Grandfather']	    = "Grandfather";
		   $relationship_arr['Grandmother']	    = "Grandmother";
		   $relationship_arr['Mother']		    = "Mother";
		   $relationship_arr['Nephew']		    = "Nephew";
		   $relationship_arr['Niece']		    = "Niece";
		   $relationship_arr['Partner']		    = "Partner";
		   $relationship_arr['Sister']		    = "Sister";
		   $relationship_arr['Son']			    = "Son";
           $relationship_arr['Supervisor']	    = "Supervisor";
           $relationship_arr['Uncle']		    = "Uncle";
		   $relationship_arr['Wife']		    = "Wife";

$result = mysqli_query($con, "SELECT * FROM test_dirk");

if (mysqli_num_rows($result) >= 0) {

    echo '<form method = "post">';

        While ($row = mysqli_fetch_assoc($result)) {

            echo 'Full Name: <input type="text" name=name['.$row["id"].'] value='.$row["name"].'>';

//***********************************************************************************************************************************

            echo 'Relationship: <SELECT name=relationship['.$row["id"].']>'  ;

                foreach ($relationship_arr as $key => $val)
                {
                    if ($relationship == $key)

                        print "<option value='".$key."' selected='selected'>".$val."</option>";
	                else
                        print "<option value='".$key."'>".$val."</option>";
                }
            echo '</SELECT>';

//***********************************************************************************************************************************

            echo 'Address: <textarea rows="4" name=address['.$row["id"].']>'.$row["address"].'</textarea>';
            echo 'Email Address: <input type="text" name=email['.$row["id"].'] value='.$row["email"].'>';
            echo 'Phone: <input type="text" name=phone['.$row["id"].'] value='.$row["phone"].'><br /><br />';
            echo '<input type="hidden" name="id[]" value="'.$row["id"].'">  '."\n";
}

    echo '<input type="submit" name="submit" value="Multiple Update">' ;
    echo '</form>';
}

?>
Thank you in advance for any help.

P.S. Pushing 50. This is not some school assignment. :)

Here's the table if it helps...

Code: Select all

$sql = "DROP TABLE test_dirk" ; (!mysqli_query($con, $sql));
$sql = "CREATE TABLE test_dirk (

id          int(32) NOT NULL auto_increment,
name        varchar(256),
relationship varchar(256),
address     varchar(256),
email       varchar(256),
phone       varchar(256),

PRIMARY KEY (id) )" ;

// Execute query

if (mysqli_query($con,$sql)) {

  echo "Test Table Created Successfully";
} else {
  echo "Error Creating Table: " . mysqli_error($con);
}

echo "<br><br>";

$sql = "INSERT INTO test_dirk VALUES (1, 'Michael', 'Father', '123 Street', 'me1@gmail.com', '123-456-7890')";   (mysqli_query($con,$sql));
$sql = "INSERT INTO test_dirk VALUES (2, 'Dirk', 'Son', '456 Street', 'me2@gmail.com', '321-456-7890')";    (mysqli_query($con,$sql));
Last edited by diseman on Sat Aug 23, 2014 1:17 pm, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I Update Form with SELECT & Show DB Entries

Post by Celauran »

You're trying to update the relationship column..

Code: Select all

"UPDATE test_dirk SET name = '$name', relationship = '$relationship', address = '$address', email = '$email', phone = '$phone' WHERE id = $id  "
which doesn't appear to exist.

Code: Select all

CREATE TABLE test_dirk (

id          int(32) NOT NULL auto_increment,
name        varchar(256),
address     varchar(256),
email       varchar(256),
phone       varchar(256),

PRIMARY KEY (id) )
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: How do I Update Form with SELECT & Show DB Entries

Post by diseman »

It exists.

Sorry, I added that field to the db manually and forgot to update my tables script.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I Update Form with SELECT & Show DB Entries

Post by Celauran »

Only problem I see is you're comparing against $relationship, which isn't defined, instead of $row['relationship'] on line 56.
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: How do I Update Form with SELECT & Show DB Entries

Post by diseman »

don't understand your reference to line 56.

Can you please write it out like it should be?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I Update Form with SELECT & Show DB Entries

Post by Celauran »

Code: Select all

if ($relationship == $key)
$relationship is undefined.

Code: Select all

if ($row['relationship'] == $key)
Now your select lists work as they should. Everything else was already working fine.
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: How do I Update Form with SELECT & Show DB Entries

Post by diseman »

Good Lord! I knew I was getting close.

Thank you VERY much. Finally, I can move on to learning something else. Very grateful to you for your help.
Post Reply