Jump to content

[SOLVED] $_POST loops with iterated variables and MySQL functions


ba2008

Recommended Posts

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!

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.