How do I Update Form with SELECT & Show DB Entries
Posted: Sat Aug 23, 2014 9:37 am
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?
Thank you in advance for any help.
P.S. Pushing 50. This is not some school assignment.
Here's the table if it helps...
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>';
}
?>
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));