ba2008 Posted October 26, 2009 Share Posted October 26, 2009 Hi All, I'm creating a page that pulls loan information from a MySQL database and displays the info for the user. The number of loans in the database is dependent upon the user. I am trying to add a delete button for each loan so the user can remove unwanted loans. The trick is that I don't know how many loans the user has, so I can't specify explicit $_POST to execute the MySQL delete query for each specific loan. My thought was to use an iteration that's based off of the number of loans currently in the database, then just pass a dynamic variable into $_POST with the same identifier as the loan (this sounds confusing, so see code below): // Grab the profile data from the database $query = "SELECT * FROM loaninfo WHERE user_id = '" . $_SESSION['user_id'] . "'"; $data = mysqli_query($dbc, $query); $loans = array(); $i = 0; if (mysqli_num_rows($data) <> 0) { // This section is to pull loan info from the database while ($row = mysqli_fetch_array($data)) { $tempc = isset($_POST['currency' . $i]) ? $_POST['currency' . $i] : $row['currency']; $tempb = isset($_POST['balance' . $i]) ? $_POST['balance' . $i] : $row['balance']; $tempr = isset($_POST['rate' . $i]) ? $_POST['rate' . $i] : $row['rate']; $tempt = isset($_POST['term' . $i]) ? $_POST['term' . $i] : $row['term']; $tempp = isset($_POST['period' . $i]) ? $_POST['period' . $i] : $row['period']; $tempd = isset($_POST['date' . $i]) ? $_POST['date' . $i] : $row['date']; array_push($loans,array($row['loan_id'], $tempc, $tempb, $tempr, $tempt, $tempp, $tempd)); $i = $i + 1; } // Closes WHILE loop } // Delete selected loans from database for ($i = 0; $i < sizeof($loans); $i++) { $delete = "'delete" . $i . "'"; if (isset($_POST[$delete])) { $query = "DELETE FROM loaninfo WHERE loan_id = " . $loans[$i][0] . ";"; mysqli_query($dbc, $query); } } // End FOR loop for ($i = 0; $i < sizeof($loans); $i++) { echo "<table border=\"1\" align=\"left\">"; // Echo database values to table echo "<tr><td><input class='text' name='currency" . $i . "' id='currency" . $i . "' type='text' value='" . $loans[$i][1] . "' /></td></tr>"; echo "<tr><td><input class='text' name='balance" . $i . "' id='balance" . $i . "' type='text' value='" . $loans[$i][2] . "' /></td></tr>"; echo "<tr><td><input class='text' name='rate" . $i . "' id='rate" . $i . "' type='text' value='" . $loans[$i][3] . "' /></td></tr>"; echo "<tr><td><input class='text' name='term" . $i . "' id='term" . $i . "' type='text' value='" . $loans[$i][4] . "' /></td></tr>"; echo "<tr><td><input class='text' name='period" . $i . "' id='period" . $i . "' type='text' value='" . $loans[$i][5] . "' /></td></tr>"; echo "<tr><td><input class='text' name='date" . $i . "' id='date" . $i . "' type='text' value='" . $loans[$i][6] . "' /></td></tr>"; echo "<tr><td align='center'><input class='text' type='submit' name='delete" . $i . "' value='Delete' /></td></tr>"; echo "</table>"; ) // End FOR loop Please help! I hope this is an interesting problem, otherwise I appreciate a quick fix. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/179090-solved-_post-loops-with-iterated-variables-and-mysql-functions/ Share on other sites More sharing options...
jpiercemaine Posted October 26, 2009 Share Posted October 26, 2009 Why can't you use the loan_id from the original select statement and use it as the key of the array? Maybe concat the name of the submit button with _$k. When form is posted split the name of the posted submit button? Quote Link to comment https://forums.phpfreaks.com/topic/179090-solved-_post-loops-with-iterated-variables-and-mysql-functions/#findComment-944961 Share on other sites More sharing options...
ba2008 Posted October 26, 2009 Author Share Posted October 26, 2009 I'm trying to do something similar to what you're saying... I've gotten it to work so that the concatenated submit name ($i appended to "delete" in my case) gets an explicitly defined $_POST to work (i.e. $_POST['delete0']). The problem is that when I pass a variable into $_POST, it doesn't work. It isn't recognizing the $delete variable I define before the iteration. Do you know why this doesn't work? If I'm not getting what you're saying, I apologize... I've learned PHP by brute force, so maybe I'm missing something conceptually. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/179090-solved-_post-loops-with-iterated-variables-and-mysql-functions/#findComment-944997 Share on other sites More sharing options...
jpiercemaine Posted October 27, 2009 Share Posted October 27, 2009 OK, try this: untested // Grab the profile data from the database $query = "SELECT * FROM loaninfo WHERE user_id = '" . $_SESSION['user_id'] . "'"; $data = mysqli_query($dbc, $query); $loans = array(); if (mysqli_num_rows($data) <> 0) { // This section is to pull loan info from the database while ($row = mysqli_fetch_array($data)) { //Use the unique loan id as the arrays key $loans[$row['loanid']]=$row; } } // Delete selected loans from database if($_POST["delete"]){ $posted=$_POST["delete"]; //loop through delete button array (in your case 1) foreach($posted as $k=>$v) { if (isset($loans[$k])) { //if loanid exists in the loans array $query = "DELETE FROM loaninfo WHERE loan_id = " . $k . ";"; mysqli_query($dbc, $query); } } foreach($loans as $k=>$v) { echo "<table border=\"1\" align=\"left\">"; // Echo database values to table echo "<tr><td><input class='text' name='currency" . $k . "' id='currency" . $k . "' type='text' value='" . $loans[$k]['currency'] . "' /></td></tr>"; echo "<tr><td><input class='text' name='balance" . $k . "' id='balance" . $k . "' type='text' value='" . $loans[$k]['balance'] . "' /></td></tr>"; echo "<tr><td><input class='text' name='rate" . $k . "' id='rate" . $k . "' type='text' value='" . $loans[$k]['rate'] . "' /></td></tr>"; echo "<tr><td><input class='text' name='term" . $k . "' id='term" . $k . "' type='text' value='" . $loans[$k]['term'] . "' /></td></tr>"; echo "<tr><td><input class='text' name='period" . $k . "' id='period" . $k . "' type='text' value='" . $loans[$k]['period'] . "' /></td></tr>"; echo "<tr><td><input class='text' name='date" . $k . "' id='date" . $k . "' type='text' value='" . $loans[$k]['date'] . "' /></td></tr>"; echo "<tr><td align='center'><input class='text' type='submit' name='delete[" . $k . "]' value='Delete' /></td></tr>"; echo "</table>"; ) // End FOR loop Quote Link to comment https://forums.phpfreaks.com/topic/179090-solved-_post-loops-with-iterated-variables-and-mysql-functions/#findComment-945588 Share on other sites More sharing options...
mikesta707 Posted October 27, 2009 Share Posted October 27, 2009 You could always just create a link, with a get variable attached to it, IE <a href="delete.php?id=12" >delete</a> instead of 12 you would put whatever the id for that specific loan is. Then on the delete page you could just use the GET variable to decide which loan to delete. Obviously you would want to check that the specific loan belonged to that specific user (other wise people could just delete another persons loans) and the normal sanitation stuff. Quote Link to comment https://forums.phpfreaks.com/topic/179090-solved-_post-loops-with-iterated-variables-and-mysql-functions/#findComment-945593 Share on other sites More sharing options...
ba2008 Posted November 2, 2009 Author Share Posted November 2, 2009 JPM - It worked! I modified the name of the submit button to be an array, and I updated the original delete code as follows: // Delete selected loans from database if($_POST["delete"]){ $posted = $_POST["delete"]; //loop through delete button array (in your case 1) foreach($posted as $k=>$v) { if (isset($loans[$k][0])) { //if loanid exists in the loans array $query = "DELETE FROM loaninfo WHERE loan_id = " . $loans[$k][0] . ";"; mysqli_query($dbc, $query); } } } // End FOR loop Thanks so much for your quick responses, saved me a lot of time and taught me something new! Quote Link to comment https://forums.phpfreaks.com/topic/179090-solved-_post-loops-with-iterated-variables-and-mysql-functions/#findComment-949513 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.