"Notice: Undefined variable: data in" error?!

Hi all,

For some strange reason my ‘update.php’ file/s to update my database tables are no longer working and when I submit/POST the data I get the following in each box or … “Notice: Undefined variable: data in …update.php on line 198”

some of my code is below;-
[php]

<?php require '../includes/database-connect.php'; $id = null; if ( !empty($_GET['id'])) { $id = $_REQUEST['id']; } if ( null==$id ) { header("Location: index.php"); } if ( !empty($_POST)) { // keep track validation errors $projectError = null; // keep track post values $date = $_POST['date']; $project = $_POST['project']; $client = $_POST['client']; $client_id = $_POST['client_id']; // validate input $valid = true; if (empty($project)) { $projectError = 'Please enter a project name'; $valid = false; } // update data if ($valid) { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "UPDATE fms_tbl_projects set date = ?,project = ?,client = ?, client_id = ? WHERE id = ?"; $q = $pdo->prepare($sql); $q->execute(array($date,$project,$client, $client_id, $id)); Database::disconnect(); header("Location: index.php"); } } else { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "SELECT * FROM fms_tbl_projects where id = ?"; $q = $pdo->prepare($sql); $q->execute(array($id)); $data = $q->fetch(PDO::FETCH_ASSOC); $date = $data['date']; $project = $data['project']; $client = $data['client']; $client_id = $data['client_id']; Database::disconnect(); } ?>

[/php]

You could do something like this
[php]<?php

require ‘…/includes/database-connect.php’;

$id = filter_input(INPUT_POST, ‘id’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$date = filter_input(INPUT_POST, ‘date’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$project = filter_input(INPUT_POST, ‘project’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$client = filter_input(INPUT_POST, ‘client’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$client_id = filter_input(INPUT_POST, ‘client_id’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

$id = isset($id) ? trim($id) : ‘’; // Ensures that user just didn’t enter space to get a non-empty variable:

if (isset($id) && !empty($id)) {
/* Ensures that user just didn’t enter space to get a non-empty variable */
$date = isset($date) ? trim($date) : ‘’;
$project = isset($project) ? trim($project) : ‘’;
$client = isset($client) ? trim($client) : ‘’;
$client_id = isset($client_id) ? trim($client_id) : ‘’;

/* Update data if project isn't empty and id matches in database table */
if (!empty($project)) {
    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "UPDATE fms_tbl_projects  set date = ?,project = ?,client = ?, client_id = ? WHERE id = ?";
    $q = $pdo->prepare($sql);
    $q->execute(array($date, $project, $client, $client_id, $id));
    Database::disconnect();
    header("Location: index.php");
}

} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = “SELECT * FROM fms_tbl_projects where id = ?”;
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$date = $data[‘date’];
$project = $data[‘project’];
$client = $data[‘client’];
$client_id = $data[‘client_id’];

Database::disconnect();

}
?>[/php]

Though I can’t vouch for it, for I haven’t tested it.

Which line is #198?

Also, you do a query, but, you don’t check if it returned any values. Perhaps your query is failing due to
the inputs?

thanks ‘Strider64’ but the code didnt work for me, I appreciate your help!

ErnieAlex the line 198 just refers to an input box, this error repeats again and again on all the input boxes.

Here’s a sample of the code [php][/php]

I cant understand how it is working on smaller scripts on my site but yet this particular on it throws up loads of errors…

Well, your code:

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = “SELECT * FROM fms_tbl_projects where id = ?”;
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);

$date = $data[‘date’];
$project = $data[‘project’];
$client = $data[‘client’];
$client_id= $data[‘client_id’];

Database::disconnect();

You create a query called $q. You process it. You fetch the results into an array called $data. Then, you
take out four fields of data. BUT, you fail to check to see if you got anything from the database. If the data
for that ID does not exist, then you have an empty $data[] array and it would throw out an error every time
you attempt to display something from that array. You need to check if you got a result from the query.

My guess is that either you do not have a field named “id” in your table (check caps and spelling) or you did
not fill in the $id variable correctly from your posted info. If the $id you compare with in the query is empty,
you would get an empty returned recordset which means an empty $data[] array.

You need to put in some error checking to make sure data is actually returned from the query!
Hope that helps…

Hi ErnieAlex,

I understand your comment however the information exists on the database as it displays it in the input boxes, it only displays the error when I click ‘update’ and post the values. The other strange this is that it does actually update/change the database???

Your code could actually be simplified a lot, but I don’t have time to go into detail on how…

First thing you need to do, wrap anything that deals with databases, ie connections, queries, ANYTHING that could possibly fail in a try catch statement. When an error happens, you want to know about it.

[php]try {

// sql statement fails/ database can’t be contacted

} catch ( PDOException $e ) {
// display generic error message for normal users
// and a useful one for debugging
}[/php]

What is the exact error message?

Hi Astoneciper,

sorry for the late response, I’ve tried a few alterations and got my revised code working and with no errors. I had to alter to the following;-

[php]

<?php if(isset($_GET['u'])): if(isset($_POST['pro'])): $stmt = $mysqli->prepare("UPDATE fms_tbl_projects set date = ?,project = ?,client = ? WHERE client_proj_no = ?"); $stmt->bind_param('ssss', $date,$project,$client, $client_proj_no); $date = $_POST['date']; $project = $_POST['project']; $client = $_POST['client']; if($stmt->execute()): echo ""; else: echo ""; endif; endif; $res = $mysqli->query("SELECT * FROM fms_tbl_projects WHERE client_proj_no=".$_GET['u']); $row = $res->fetch_assoc(); ?>

[/php]

thanks for you help

Dan, glad you solved it… !

I gave Astone the Karma on this one. Looking forward to your next puzzle… LOL

Sponsor our Newsletter | Privacy Policy | Terms of Service