6 Replies - 335 Views - Last Post: 05 February 2012 - 11:39 AM Rate Topic: -----

Topic Sponsor:

#1 creativecoding  Icon User is online

  • Hash != Encryption
  • member icon

Reputation: 412
  • View blog
  • Posts: 2,419
  • Joined: 19-January 10

Unsure if HTML or PHP problem.

Posted 04 February 2012 - 07:08 PM

Alright, so I have a little problem which from the looks of it, is the very last on my checklist for a project.

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:
Attached Image
Attached Image


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)">&nbsp;<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.

Is This A Good Question/Topic? 0
  • +

Replies To: Unsure if HTML or PHP problem.

#2 no2pencil  Icon User is online

  • 2 girls, 1 club
  • member icon

Reputation: 3062
  • View blog
  • Posts: 22,963
  • Joined: 10-May 07

Re: Unsure if HTML or PHP problem.

Posted 04 February 2012 - 07:08 PM

If you clear your cache or use a separate browser, can you replicate the results?
Was This Post Helpful? 0
  • +
  • -

#3 creativecoding  Icon User is online

  • Hash != Encryption
  • member icon

Reputation: 412
  • View blog
  • Posts: 2,419
  • Joined: 19-January 10

Re: Unsure if HTML or PHP problem.

Posted 04 February 2012 - 07:12 PM

Tried it on Firefox (using Chrome atm), same result.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2147
  • View blog
  • Posts: 5,431
  • Joined: 08-June 10

Re: Unsure if HTML or PHP problem.

Posted 05 February 2012 - 03:02 AM

is $_GET['airport'] passed correctly?
Was This Post Helpful? 0
  • +
  • -

#5 creativecoding  Icon User is online

  • Hash != Encryption
  • member icon

Reputation: 412
  • View blog
  • Posts: 2,419
  • Joined: 19-January 10

Re: Unsure if HTML or PHP problem.

Posted 05 February 2012 - 03:17 AM

I believe so. I outputted $_GET['airport'] on every step (form and save) and they both matched. Outputted the query and ran it in phpMyAdmin and it updated the row.
Was This Post Helpful? 0
  • +
  • -

#6 e_i_pi  Icon User is online

  • = -1
  • member icon

Reputation: 363
  • View blog
  • Posts: 1,023
  • Joined: 30-January 09

Re: Unsure if HTML or PHP problem.

Posted 05 February 2012 - 04:08 AM

You've got your bound values muddled up. Check out your code:
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);


The statement's value order is "varvalue, varname, airport" and the bind_param method's value order is "varname, varvalue, airport"

This post has been edited by e_i_pi: 05 February 2012 - 04:08 AM

Was This Post Helpful? 4
  • +
  • -

#7 creativecoding  Icon User is online

  • Hash != Encryption
  • member icon

Reputation: 412
  • View blog
  • Posts: 2,419
  • Joined: 19-January 10

Re: Unsure if HTML or PHP problem.

Posted 05 February 2012 - 11:39 AM

Perfect! And along with that, I found what was causing the POST data not to change. Some of the varnames/values were being outputted twice, with one of the other one's data being sent instead of the one that I changed. This is visible in the first screenshot.

Thanks all! Just about made my day.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1