Updating a database using PHP/MySQL

Status
Not open for further replies.

iorrasnet

New Member
I have a database which I want to update the individual records using PHP / My SQL.

I have got as far as the following :

Listing all the records with an edit link after each record.

Listing the specific record, using the zid, in a form which allows the individual items of the records to be updated.

I can get the edited information into a SQL statement to UPDATE the record in the database. I cannot seem to get the zid to go into the SQL statement

So the SQL reads

$sqlQuery = "UPDATE table SET fieldInInfo = '$cleanInfo' where zid = '$cleanZID'";

When I run this, echoing the sql I get :

UPDATE table SET fieldInfo = 'Brian Cowen' where zid = ''

and it doesnt update the database.

Any thoughts ?

I am a newbie so it is probably something very simple that i just cannot see.

Thanks
 

louie

New Member
put the id into a hidden field or pass into the querystring.
 

iorrasnet

New Member
Thanks for your replies.

I had the ZID in a hidden field on the form. The problem I have is getting it from that hidden field into the SQL statement.

In the hidden field of the form displaying the records it is ZID.

It becomes $cleanZID in the function after it is posted from the form and passed through htmlspecialchars.

The coding I used is the same as the coding I am using for the other variables which are getting into the SQL statement correctly, but because the ZID is not being carried, the SQL statement does not know which record to update.

Am I missing something very obvious ?

Thanks again
 

iorrasnet

New Member
$cleanZID= htmlspecialchars ($_POST['recordID']);
$cleanCanditate= htmlspecialchars ($_POST ['electionCanditate']);

recordID is the name of the hidden field.
 

garycocs

New Member
Cool, you sure recordID is the name of the hidden field??

Just run an echo $cleanZID; after the _POST to see if it actually comes through.
 

iorrasnet

New Member
Actually it doesnt !??

Now I may have an overly complicated way of doing this so I will walk you through what i am doing.

There is a file called viewrecords.php which list the records and has an edit link.

When you click on the edit link you bring up a file called editrecord.php This file contains the following code :

if (isset($_GET['id'])) {

displayEditRecords (trim($_GET['id']));

}

displayEditRecords () outputs the form allowing you to edit the fields. When you click the submit button it activates the edit.php file. It is this file which has the function that makes the MySql statement and which starts off with the code that I posted earlier.

But echo the $cleanZID is not outputting anything.
 

iorrasnet

New Member
function output_form_edit ($zid) {
echo "<form action='edit.php' method='post' onSubmit=return validateForm();>";
echo "<input type ='hidden' name='recordID' value = '$strContents'>";

This function is called from within a function called displayEditRecords
 

garycocs

New Member
Right think that might be it.

function output_form_edit ($zid) {
echo "<form action='edit.php' method='post' onSubmit=return validateForm();>";
echo "<input type ='hidden' name='recordID' value = '$zid'>";


You have to use zid rather than $strContents as the value??
 

iorrasnet

New Member
Yahoo !!! It worked !!!

Thanks very much !!!!!

Gosh basic error but sure I have to make them to learn.

Thanks again
 

garycocs

New Member
Glad to hear, best of luck with it anyways, it seems like a long enough way to go about it but I'm sure you're learning as you go along.
 

iorrasnet

New Member
Yeah, when I wrote it out for you it struck me that it was a bit long winded. Now that i have it working I might tidy it up a bit.

Thanks again
 
Status
Not open for further replies.
Top