Hi,
I've been through the forums and I've established the SQL I need to update my table rows, but I'm having a little trouble working out the PHP I need to use to implement it.
Basically I have a database of members, each member having 16 (for now!) pieces of information.
I can add members and delete them. I can also view them and delete them.
This is the code I use to delete entries.
<?php // Require the classes
require_once('../sypphp/includes/DbConnector.php');
require_once('../sypphp/includes/Validator.php'); // Create an object (instance) of the DbConnector and Validator
$connector = new DbConnector();
$validator = new Validator();
// DELETE SECTIONS ////////////////////////////////////////////////////////////////////
if ($HTTP_GET_VARS['action'] == 'delete'){
// Store the section ID to be deleted in a variable
$memberID = $HTTP_GET_VARS['ID'];
// Validate the memberID, and if it's ok delete the section
if ( $validator->validateGeneral($memberID,'ID') ){
// The validator returned true, so go ahead and delete the section
$connector->query('DELETE FROM members WHERE ID = '.$memberID);
echo 'Member Deleted.<br>';
}else{
// The validator returned false, meaning there was a problem
echo "Couldn't delete. There was a problem with: ".$validator->listErrors();
}
}
// LIST SECTIONS /////////////////////////////////////////////////////////////////////
$result = $connector->query('SELECT ID,surname,firstname,email FROM members ORDER BY surname DESC');
// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){
echo $row['ID']
.' -
'.$row['surname']
.' -
'.$row['firstname']
.' -
'.$row['email'];// Show the name of member
echo '<a href="memberdel.php?action=delete&ID='.$row['ID'].'"> Delete </a>';// Show the delete link
echo '<br>'; // Show a carriage return
}
?>
I'm fairly sure that I can use a similar method of passing variables in order to update them - I'm just not quite sure how or what the code is.
This is what I've come up with so far
<?php // Require the classes
require_once('../sypphp/includes/DbConnector.php');
require_once('../sypphp/includes/Validator.php'); // Create an object (instance) of the DbConnector and Validator
$connector = new DbConnector();
$validator = new Validator();
// UPDATE SECTIONS ////////////////////////////////////////////////////////////////////
if ($HTTP_GET_VARS['action'] == 'update'){
// Store the section ID to be updated in a variable
$memberID = $HTTP_GET_VARS['ID'];
// Validate the memberID, and if it's ok update the section
if ( $validator->validateGeneral($memberID,'ID') ){
// The validator returned true, so go ahead and delete the section
$result = $connector->query("SELECT * FROM members WHERE ID = $memberID");
$row = $connector->fetchArray($result);
//AT THE END OF THIS LINE WITH FN/SN IS WHERE I'D LIKE TO PUT A LINK TO UPDATE THIS ENTRY (I know it's two entries, but I'll clean up formatting later)
echo '<input name="firstname" type="text" id="firstname" value="'.$row['firstname'].'" size="20"> <input name="surname" type="text" id="surname" value="'.$row['surname'].'" size="20">'.'<br>';
echo 'D.o.B: '.$row['dobmonth'].' '.$row['dobday'].' '.$row['dobyear'].'</p>';
echo '<p>Address:<br>';
echo $row['address1'].'<br>';
echo $row['address2'].'<br>';
echo $row['address3'].'<br>';
echo $row['postcode'].'</p>';
echo '<p>Email: '.$row['email'].'</p>'; //35
echo '<p>Employment/Study: '.$row['jobrole'].' at '.$row['company'].'<br>';
echo 'Do they want to be on the Jobs Bulletin? '.$row['jobslist'].'<br>';
echo 'Do they want to be informed of SYP Events? '.$row['eventslist'].'<br>';
echo 'London or Oxford? '.$row['london'].'</p>';
echo '<p>Join Date: '.$row['joinmonth'].' '.$row['joinday'].' '.$row['joinyear'].'<br>';
echo 'Expiry Date: '.$row['expmonth'].' '.$row['expday'].' '.$row['expyear'].'</p>';
echo 'Membership Number: '.$row['memnum'];
}else{
// The validator returned false, meaning there was a problem
echo "Couldn't update. There was a problem with: ".$validator->listErrors();
}
}//28
?>
Basically you can see the information passed from clicking the 'update' link on the view page. You can also see that I've passed that information into a text input area for editing.
How can I pass that information from those text input fields into the SQL query and only for that particular field. It's isolating that field I'm slightly stumped on (although I'm happy to update the lot if need be).
So, I've successfully passed an ID from one page to the next via a link - what I need to know now, if you'd be so kind to assist, is how do I then pull the data from that ID into a form (I'm halfway there I think) and then re-post that data to update the table.
I'd be really grateful for any help you could offer on this. I'm aware there are better (and shorter!) ways of doing this, but for now I'd like to carry on using this method. I (hope) I'm learning a lot about the way PHP and SQL operate using this (vague) template and in time I hope to progress further. Got to start somewhere right?
Incidentally, can anyone advise on any good books I could refer to?