MY project stores and displays public airport information. In the admin panel, the admin is able to modify this airport info.
The airport info is stored in a table called "data" with 4 columns: id, varname, varvalue, and airport.
So some info will look like this:
805 - OWNER - JOHN DOE - 56789
806 - LOCATION - 87.000, 85.000 - 56789
807 - OWNER - BILLY MAYS - 67891
and so on...
Basically, I want to have the ability to edit these. I have already programmed it so that it'll select on airport and retrieve all of the varname, varvalues and for every row returned, print out:
$varname . '<input type="text" name="' . $varname . '" value="' . $varvalue . '">';
And when I save, it goes through every POST variable is has and updates the variable with the new value. But whenever I try, it doesn't update anything. At first I thought it was because spaces are turned into underscores, but I used str_replace to fix that and yet it still didn't work. I outputted the POST data, and as it turns out non of the information is being saved. As if value is fixed and cannot be changed.
To put this into better terms, I have an input called A, it has the information "apple", and I change that to "pear" and submit. $_POST['apple'] is equal to "apple".
Why is this? I've been sort of lacking recently so I'm sure I'm making some silly little mistake.
I've also noticed that some of the variables are actually getting through. Here's a screenshot for the form and the POST data received:


As for code, it's all in one file. Minimized down to the basics:
<?php
error_reporting(E_ALL);
$page = array();
$page['content'] = "";
require "inc/db.class.php";
$db = new db();
$page['title'] = '<a href="admin.php">Admin</a> - <a href="?page=records">Records</a> - Manage airport data';
$mysqli = $db->connect();
if(isset($_GET['custom_action'])){
$ca = $_GET['custom_action'];
switch($ca){
case "edit":
$page['title'] = '<a href="admin.php">Admin</a> - <a href="?page=records">Records</a> - <a href="?page=records-manage">Manage airport data</a> - Editing ' . $_GET['airport'];
if($stmt = $mysqli->prepare("SELECT varname, varvalue FROM data WHERE airport=?")){
$stmt->bind_param("s", $_GET['airport']);
$stmt->execute();
$stmt->bind_result($varname, $varval);
$page['content'] .= '<form action="?page=records-manage&custom_action=save&airport=' . $_GET['airport'] . '" method="post"><table>';
while($stmt->fetch()){
$page['content'] .= '<tr><td>' . $varname . '</td><td><input type="text" name="' . $varname . '" value="' . $varval . '"></td></tr>';
}
$page['content'] .= '</table>';
$page['content'] .= '<div class="actions">
<input type="submit" class="btn primary" value="Save changes (be patient, this may take awhile)"> <button type="reset" class="btn" onclick="history.go(-1);">Cancel</button>
</div></form>';
} else {
die("There was an error");
}
break;
case "save":
$page['title'] = '<a href="admin.php">Admin</a> - <a href="?page=records">Records</a> - <a href="?page=records-manage">Manage airport data</a> - Save';
$win = 0;
var_dump($_POST);
foreach($_POST as $varname=>$varvalue){
if($stmt = $mysqli->prepare("UPDATE data SET varvalue=? WHERE varname=? AND airport=?")){
$varname = str_replace("_", " ", $varname);
$airport = $_GET['airport'];
$stmt->bind_param('sss', $varname, $varvalue, $airport);
$stmt->execute();
$stmt->store_result();
if($stmt->affected_rows > 0){
$win++;
}
}
}
if($win == count($_POST)){
$page['content'] .= '<div class="alert-message success">
<a class="close" href="#">×</a>
<p><strong>New information has been saved</strong></p>
</div>';
} else if($win == 0){
$page['content'] .= '<div class="alert-message error">
<a class="close" href="#">×</a>
<p><strong>No records were saved</strong> Either no data was changed or there was an error.</p>
</div>';
} else {
$page['content'] .= '<div class="alert-message warning">
<a class="close" href="#">×</a>
<p><strong>Some records were not updated</strong> Only ' . $win . ' out of ' . count($_POST) . ' were updated.</p>
</div>';
}
$page['content'] .= '<a href="#" onclick="history.go(-1)">Continue editing</a> or go back to <a href="#" onclick="history.go(-2)">Records Management</a>';
break;
}
}
?>
Oh, and even though there's that one POST data being sent, it isn't saved.

New Topic/Question
Reply




MultiQuote



|