Jump to content

Recommended Posts

Hey everyone.
I am farly new to PHP and MySQL so I do say sorry in advance for any stupid lines of code I have written.
But I am stuck at a point in a website im making for a friend.

I have a database with a unknown amount of entries.
I have currently found a way to make a list of entries appear in a table. and add a edit button to edit each entry separately on another page
however what I would like is a table with all the entries in so they can admin all the entries without having to go though phpmyadmin

I know it has something to do with arrays. But i cant seem to find a simple explanation for what im wanting to do 
So im hoping someone here can either give me a link to something that will help. or explain it to me in simple terms.

Thanks in advance
PomTom

I can provide code if required from my other working pages but i figured since its a new page im making theres no code written to provide
 

Link to comment
https://forums.phpfreaks.com/topic/282453-help-with-forms-arrays-and-bulk-editing/
Share on other sites

The code will be very similar to your edit script. 
 
And Example delete link would be

echo '<a href="delete.php?id="'.$row['your_entry_id_col'].'">DELETE </a>';

Example delete.php code

// connect to mysqli
$mysqli = msqli_query('localhost', 'usernmae', 'password', 'database');

// check that id is present in the url and id has only number characters
if(isset($_GET['id']) && preg_match('~[0-9]+~', $_GET['id']))
{
	// get the id
	$id = $_GET['id'];

	// execute the query
	mysqli_query('DELETE FROM table_name_here WHERE entry_id_col=' . $id);

	// Check that the query deleted the entry
	if(mysqli_affected_rows ($mysqli) == 1)
	{
		echo 'Entry deleted!';
                echo '<a href="admin.php">Go Back</a>';
	}
}

The above example can only delete one entry at a time.

 

If you want to be able to delete multiple entries at a time then you'd use checkboxes. Example code for this would be

echo '<iput type="checkbox" name="delete[]" value="'.$row['your_entry_id_col'].'" />';

Example code to process the checkboxes in your edit script 

// connect to mysqli
$mysqli = msqli_query('localhost', 'usernmae', 'password', 'database');

// check that any delete selections have been made
if(isset($_POST['delete']) && (is_array($_POST['delete']) && !empty($_POST['delete']))
{
	// this holds valid values to be used in your query
	$valid_is = array();

	// make sure the submitted ids only contain number characters
	// this is to make the query safe to use latter on
	foreach($_POST['delete'] as $id)
	{
		if(preg_match('~[0-9]+~', $id)
		{
			$valid_ids[] = $id;
		}
	}

	// if we have valid values
	if(is_array($valid_ids))
	{
		// prepare query format
		$sql = 'DELETE FROM table_name_here WHERE entry_id_col IN (%s)';

		// populate query with ids
		$sql = sprintf($sql, implode(',', $valid_ids));

		// execute query
		mysqli_query($sql);

		// check that query deleted any records
		if(mysql_affected_rows($mysqli)) {
			echo 'Seletected entries have been deleted!';
			echo '<a href="admin.php">Go Back</a>';// link back to admin page
		}
	}
	else
	{
		echo 'Sorry ids invalid cannot continue';
	}
}

Above code is untested and is to only serve as an example.

Edited by Ch0cu3r

you would use an array for the form field name='data[id][column]' attribute, where the column value is the database table column name and the id is the row's id. when the data is submitted, you would loop over the data for each id/row, then you would loop over the submitted column names/values for each id/row.

Edited by mac_gyver
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.