Pomtom44 Posted September 26, 2013 Share Posted September 26, 2013 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 pagehowever what I would like is a table with all the entries in so they can admin all the entries without having to go though phpmyadminI 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 advancePomTomI 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 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 26, 2013 Share Posted September 26, 2013 Are you wanting to delete from your admin page? Quote Link to comment Share on other sites More sharing options...
Pomtom44 Posted September 26, 2013 Author Share Posted September 26, 2013 Are you wanting to delete from your admin page? It would be nice to be able to. but its not required if it has alot of code behind it Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 26, 2013 Share Posted September 26, 2013 (edited) 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 September 26, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Pomtom44 Posted September 26, 2013 Author Share Posted September 26, 2013 Oh no that sort of code i have fine.Im looking for a way to list the whole database in a table of form entries using php.A front end way to edit the whole database (apart from row ID) Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 26, 2013 Share Posted September 26, 2013 (edited) 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 September 26, 2013 by mac_gyver Quote Link to comment 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.