KMC1499 Posted March 7, 2012 Share Posted March 7, 2012 First off thank you for opening my issue and taking the time out to try and help. I have created a php page that will display a list of people from a mysql database. What i would like to do is have a button that will update a field in the database when pressed. My question is if i make the fields into a form will it only update that one person? Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/ Share on other sites More sharing options...
batwimp Posted March 7, 2012 Share Posted March 7, 2012 PHP is versatile and can pretty much whatever you want it to do, form-wise. If you could post some code and give more detailed explanation, we can probably help you do whatever it is you want to do. Make sure your code is surrounded by code tags. Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325000 Share on other sites More sharing options...
KMC1499 Posted March 7, 2012 Author Share Posted March 7, 2012 <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_judges, $judges); $query_Recordset1 = "SELECT idjudge, firstname, lastname, email, mailaddress, city, state, zipcode, party, phone, transportation, travel, distance, class, pickup, dropoff, bilingual, dateentered, mysqldate, contacted FROM judge WHERE contacted='No' ORDER BY mysqldate ASC"; $Recordset1 = mysql_query($query_Recordset1, $judges) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Judges</title> </head> <body> <?php // Free memory //mysql_free_result($check); ?> <table border="1" style="border:1px solid black;"> <tr> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Signup Date</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>First Name</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Last Name</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Email Address</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Mailing Address</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>City</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>State</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Zipcode</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Party</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Phone Number</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Transportation</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Travel</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Distance</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Class</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Pickup</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Dropoff</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Bilingual</FONT></td> <TD BGCOLOR=#C7C5BC ALIGN=CENTER ><FONT style=FONT-SIZE:9pt FACE="Arial" COLOR=#f9f9f7>Contacted?</FONT></td> </tr> <?php do { ?> <tr> <TD ALIGN=CENTER ><?php echo $row_Recordset1['dateentered']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['firstname']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['lastname']; ?></td> <TD ALIGN=CENTER ><a href="mailto:<?php echo $row_Recordset1['email'];?>"><?php echo $row_Recordset1['email']; ?></a></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['mailaddress']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['city']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['state']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['zipcode']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['party']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['phone']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['transportation']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['travel']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['distance']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['class']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['pickup']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['dropoff']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['bilingual']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['contacted']; ?></td> </tr> <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?> </table> </body> </html> <?php mysql_free_result($Recordset1); ?> What we want to do is to be able to update each person as we contact them here is a picture showing the form online. Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325018 Share on other sites More sharing options...
cyberRobot Posted March 8, 2012 Share Posted March 8, 2012 It doesn't look like your contact database has a primary key. First, I would recommend adding one so you can reference an individual record. A button can then be added which passes that ID to the script performing the database update. Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325188 Share on other sites More sharing options...
KMC1499 Posted March 8, 2012 Author Share Posted March 8, 2012 It does have a primary key, this page was just used to pull all records. I could pull the primary key and not display it. Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325216 Share on other sites More sharing options...
Muddy_Funster Posted March 8, 2012 Share Posted March 8, 2012 so you just want to add a button per contact then? and reload the page to show changes when the button is clicked? Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325218 Share on other sites More sharing options...
cyberRobot Posted March 8, 2012 Share Posted March 8, 2012 It does have a primary key, this page was just used to pull all records. I could pull the primary key and not display it. Ah yes, good point. So you should have everything you need to update individual records? Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325219 Share on other sites More sharing options...
KMC1499 Posted March 8, 2012 Author Share Posted March 8, 2012 I believe so i was just unsure how to go about it since im fairly new to php and i wasnt sure how to go about it. My idea was to create a form around each record that would have a "update" button on the end that would run a function that would update the database and reload the webpage. Sound good??? Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325225 Share on other sites More sharing options...
cyberRobot Posted March 8, 2012 Share Posted March 8, 2012 My preference would be to use plain HTML links. <a href="update_script.php?<?php print $current_record_id; ?>">Update Current Record</a> Then on the PHP side, you could retrieve the ID with $_GET. Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325232 Share on other sites More sharing options...
Muddy_Funster Posted March 8, 2012 Share Posted March 8, 2012 I'd go with cyber on that, it's needless bloating to the script if you are coding a form for each record. If you want it to look like a button you can use an image as the link, but a simple hyperlink rendered from the select query is, I think, the best way. you can even contitionalise the hyperlink/image to not link if the Contacted field is already set to Yes. Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325240 Share on other sites More sharing options...
KMC1499 Posted March 8, 2012 Author Share Posted March 8, 2012 I plan on adding a where clause to my select so only my people who have "no" in the contacted field are shown. So i added the form tags to the code. <form action="update_script.php" method="get"> <tr> <TD ALIGN=CENTER ><?php echo $row_Recordset1['dateentered']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['firstname']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['lastname']; ?></td> <TD ALIGN=CENTER ><a href="mailto:<?php echo $row_Recordset1['email'];?>"><?php echo $row_Recordset1['email']; ?></a></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['mailaddress']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['city']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['state']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['zipcode']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['party']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['phone']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['transportation']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['travel']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['distance']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['class']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['pickup']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['dropoff']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['bilingual']; ?></td> <TD ALIGN=CENTER ><?php echo $row_Recordset1['contacted']; ?></td> <TD ALIGN=CENTER ><a href="update_script.php?<?php print $row_Recordset1['idjudge']; ?>">Update</a></td> </tr> <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?> </table> </form> </body> </html> <?php mysql_free_result($Recordset1); ?> And in my update script page i add the php info to update the record? Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325264 Share on other sites More sharing options...
cyberRobot Posted March 8, 2012 Share Posted March 8, 2012 Sorry, I mistyped the code. You need a name/value pair for each variable. <a href="update_script.php?id=<?php print $current_record_id; ?>">Update Current Record</a> So your code would be changed to: <TD ALIGN=CENTER ><a href="update_script.php?id=<?php print $row_Recordset1['idjudge']; ?>">Update</a></td> Of course, "id" would be whatever you want to name the variable. Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325309 Share on other sites More sharing options...
cyberRobot Posted March 8, 2012 Share Posted March 8, 2012 Also note that you don't need the <form> tag if you're using links. Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325310 Share on other sites More sharing options...
KMC1499 Posted March 8, 2012 Author Share Posted March 8, 2012 Ok no form tags and i added that code to me page buuuuuut im missing where the page actually updates the database. What i want to do is when the people click the update button have the page update the contacted field to Yes and reload the page this time without the contacts with yes in the contacted column (page will filter these out in the select query automatically) Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325324 Share on other sites More sharing options...
cyberRobot Posted March 8, 2012 Share Posted March 8, 2012 If you're looking for how to update the database, there are many tutorials out there: https://www.google.com/search?q=update+a+mysql+database+with+php The UPDATE syntax for MySQL can be found here: http://dev.mysql.com/doc/refman/5.0/en/update.html Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325327 Share on other sites More sharing options...
cyberRobot Posted March 8, 2012 Share Posted March 8, 2012 For displaying the list after an update has been made, you could look into using the same page that displays the list to also process the update. For example, you page could be modified to do the following: [*]If user clicked one of the edit links get the ID to edit make sure the ID is valid update the database, if the ID was valid If the update was successful / unsuccessful, display corresponding message to user [*]Get the list of contacts [*]Display the list of contacts Note that steps 2 and 3 happen whether or not an edit link was clicked. Quote Link to comment https://forums.phpfreaks.com/topic/258489-dynamic-update-form/#findComment-1325329 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.