Looking for some help with my php 'learning' project.
Seems every time I save another name to my list of names the order they are presented back to me is changed. In other words, if I enter/save three names - the order of those names being presented back to me changes each time I click the save button. Not a huge deal, but tell that to my OCD side!
Here is the code:
Code: Select all
$sql = "Delete FROM notifications WHERE username = '".$requestor."' " ;
$result = mysqli_query($con, $sql) or die (mysqli_error($con));
foreach ($_POST['txt'] as $key => $val) {
if (!empty($val)) {
$txtbox = mysqli_real_escape_string($con, $_POST['txt'] [$key]);
$relationship = mysqli_real_escape_string($con, $_POST['relationship'] [$key]);
$address = mysqli_real_escape_string($con, $_POST['address'] [$key]);
$email = mysqli_real_escape_string($con, $_POST['email'] [$key]);
$phone = mysqli_real_escape_string($con, $_POST['phone'] [$key]);
$fields = "userid = '$userid',
username = '$username',
name = '$txtbox',
relationship = '$relationship',
address = '$address',
email = '$email',
phone = '$phone'";
$sql = "INSERT notifications SET ".$fields." ";
mysqli_query($con, $sql) or die (mysqli_error($con));;
} // if
} // foreach
} // post
$txtbox = $relationship = $address = $email = $phone = array();
// Default number of empty dynamic Rows on the form & set variables
for ($i = 0; $i < 6; $i++)
{
$txtbox[$i] = "";
$relationship[$i] = "";
$address[$i] = "";
$email[$i] = "";
$phone[$i] = "";
}
$query = "SELECT name, relationship, address, email, phone FROM notifications WHERE username = '".$requestor."' " ;
$result = mysqli_query($con, $query);
if (!$result) die(mysql_error());
else {
$i = 0;
while ($row = mysqli_fetch_object($result)) {
$txtbox[$i] = $row->name;
$relationship[$i] = $row->relationship;
$address[$i] = $row->address;
$email[$i] = $row->email;
$phone[$i] = $row->phone;
$i++;
}
mysqli_free_result($result);
}
Ideally, I would prefer to UPDATE, but that fix has proved to be illusive as well.
Seems to me they should be added to the db the same way they are listed in my form and then displayed back to me the same way.
Thank you in advance for any help,
Michael