Calling PHP experts

Associate
Joined
13 Jan 2007
Posts
2,424
Location
Belfast,Northern Ireland
Got myself a bit stuck right now. Basically the code is outputting a table, with the data coming from my database. It essentially outputs a list of top ten songs. Each also has its own delete and update button. My problem is whenever I click delete it is not recognising which record I am clicking delete on, it simply takes the value of $pid to be what it was when it was last output.

In my mind I thought putting them into an array would solve it but I dont think it will. How can I get these buttons to recognise to which record they're associated or change the delete button to see this.

The code that concerns this is line 36 to 45 or so.

http://pastebin.com/eZwAmNH2
 
All your inputs have the same name, you have no idea which you clicked.

Put a form round each button or use a link (rather than a form button).
 
Yeah was thinking of a link myself but anyway to do a confirm message of sorts on a link? Dont want a user to be able to hit delete and thats it removed straight off
 
Thanks Telescopi got the delete working now, problem is the update now :(

Below are the two pieces of code which relate, the submit button itself links correctly now (pid is sent to url.) However nothing seems to be executed in the POST save part. Because its just hitting a link and not submitting?

Code:
$menuDisplay .= '<tr><td>' . $artist . '</td><td>' . $song . '</td><td><input type="text" id="position" name="position" maxlength="2" value=" ' . $position . '"</td><td><a href="edit_toptunes.php?pid='.$pid.'"><input type="submit" name="save" value="Update"></a></td><td><a href="edit_toptunes.php?pid=' . $pid . '&todelete=true">Remove</a></td> ';


if (isset($_POST['save']))
{
	$updatesong = mysqli_real_escape_string($myconnection,$_GET['pid']);	
	$positionupdate = mysqli_real_escape_string($myconnection,$_POST['position']);
	$date = mysqli_real_escape_string($myconnection,date('F j, Y, g:i a'));
	$error_msg = $updatesong;
	echo $positionupdate;
	echo $date;
	
	if($newposition < 1 || $newposition > 10)
			{
				$error_msg = '<font color="#FF0000">*Position must be between 1 and 10 for top tunes.</font>';
			}
			else
			{
				 
				$sql = "SELECT * FROM songs WHERE position='$positionupdate'";
				$result = mysqli_query($myconnection, $sql);
				// Mysql_num_row is counting table row
				$count = mysqli_num_rows($result);					
				if($count >=1)
				{
					$error_msg = '<font color="#FF0000">*A song with that position already exists.</font>';
				}
				else
				{
					$querysong = "UPDATE songs SET position='$positionupdate', date_updated='$date' WHERE song_id='$updatesong'";
					$updatesong = mysqli_query($myconnection, $querysong);
					header("LOCATION: edit_toptunes.php?updated=true"); 
					exit;
				}
			}
	
}
 
looks like i may rework it entirely to use 10 seperate forms, will post solution when im done or perhaps will have commited suicide, wish me luck for either!
 
Pass the 'pid' as a function parameter to your ConfirmDelete function, assign a variable to the confirm(), do an IF condition on that 'confirm' variable, if true (Yes/OK been selected) use location replace with the 'pid' as part of the URL (ie: index.php?delete_pid=<pid>). Then the PHP is basically what you've done.

As for the update, i'd use $_SERVER['REQUEST_METHOD'] to check for the POST data. I'd also remove the href tags surrounding the submit button and replace it with an OnClick event that triggers a location href/replace.
Also unless the submit button is part of a form, which the above doesn't look like it is, then you really should use the button type rather than submit type.

Also $newposition variable doesn't appear to being assigned to anything :confused:

I'd also perhaps look at Ajax'ing some of it.
 
took me less than 10minutes this morning. Just created a new form for each row that is output, my problem was that I didnt realise just by having forms with different names, it could tell when a button was pressed that despite several having the same name, it knew which button belonged to which form. Hours wasted on that yesterday...nuts
 
Back
Top Bottom