Stephie22 Posted October 25, 2017 Share Posted October 25, 2017 Good Day Guys, I am busy creating a simple interactive website using php and i need some help. I have two forms. One that acts with all my actions like Add, Delete, Edit etc. My forms looks like this: Dealer_transaction.php: <!DOCTYPE HTML> <?php require 'db_connect.php'; //connect to mySQL $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die('Unable to connect. Please check connection parameters!'); //make sure that the correct database is chosen mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db)); switch($_POST['action']){ case 'Add Dealer': //escape incoming values to protect the db $dealer_code = mysql_real_escape_string($_POST['dealer_code'], $db); $dealer_name = mysql_real_escape_string($_POST['dealer_name'], $db); $dealer_brand = mysql_real_escape_string($_POST['dealer_brand'], $db); $active = mysql_real_escape_string($_POST['active'], $db); $registered_name = mysql_real_escape_string($_POST['registered_name'], $db); $date_opened = mysql_real_escape_string($_POST['date_opened'], $db); $date_closed = mysql_real_escape_string($_POST['date_closed'], $db); $year_registered = mysql_real_escape_string($_POST['year_registered'], $db); //add dealer information into the tables $query = 'INSERT IGNORE INTO dealers(dealer_code, dealer_name, dealer_brand, active, registered_name, date_opened, date_closed, year_registered) VALUES("'.$dealer_code.'", "'.$dealer_name.'", "'.$dealer_brand.'", "'.$active.'", "'.$registered_name.'", "'.$date_opened.'", "'.$date_closed.'", "'.$year_registered.'")'; mysql_query($query, $db) or die(mysql_error($db)); $redirect = '../index.php'; break; case 'Delete Dealer': //delete the dealer from the db $query = 'DELETE FROM dealers'; mysql_query($query, $db) or die(mysql_error($db)); $redirect = '../index.php'; break; default: $redirect = '../index.php'; break; } header('Location: ' . $redirect); ?> Delete.php: <!DOCTYPE HTML> <?php require 'db_connect.php'; //connect to mySQL $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die('Unable to connect. Please check connection parameters!'); //make sure that the correct database is chosen mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db)); ?> <html lang="en"> <head> <meta charset="UTF-8"> <title>Nissan Dealer Editing</title> <link rel="shortcut icon" href="../images/Group_of_Africa.ico"> <link rel="stylesheet" href="../Styles/add_delete_dealer.css"> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <script src="https://code.jquery.com/jquery-3.1.1.js"></script> <script src="js/dealer.js"></script> </head> <body> <header> <a href="../index.php"><img src="../images/Group_of_Africa_Logo.jpg" alt="Nissan Logo" height="80"></a> <hgroup> <h1>NISSAN</h1> <h2>Innovation that excites</h2> </hgroup> </header> <nav id="dealer_nav"> <ul> <li><a href="../index.php">Home</a></li> <li><a href="../php/add_dealer.php">Add Dealer</a></li> <li><a href="../php/delete_dealer.php" class="current">Delete Dealer</a></li> <li><a href="../php/about_us.php">About Us</a></li> <li><a href="../php/contact_us.php">Contact Us</a></li> </ul> </nav> <section> <form action="dealer_transactions.php" method="post"> <h2>Deleting A Dealer:</h2> <hr> <figure> <figcaption><h2 id="dealers_heading"><span style="color: red">NISSAN </span> <span style="color: #003F65">DATSUN </span><span style="color: silver">INFINITI </span>Dealerships</h2></figcaption> </figure> <fieldset> <legend>Dealer Information:</legend> <table> <?php //select the information from the database that you will be deleting $query = 'SELECT * from dealers'; $result = mysql_query($query, $db) or die(mysql_error($db)); $total_dealers = mysql_num_rows($result); $odd = true; if(mysql_num_rows($result) > 0){ while($row = mysql_fetch_assoc($result)){ echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">'; $odd = !$odd; echo '<td style="width: 100%">'; echo $row['dealer_name']; echo '</td><td>'; echo '<input type="submit" class="button" name="action" value="Delete Dealer">'; echo '</td></tr>'; } echo '<tfoot>'; echo '<th colspan="9"></p>Total Dealers:'.$total_dealers.'</p></th>'; echo '</tfoot>'; echo '</table>'; }else{ echo '<table>'; echo '<hr>'; echo '<p><strong>No Dealers Has Been Added Yet...</strong></p>'; echo '<hr>'; echo '<tfoot>'; echo '<th colspan="9"><p>Total Dealers:'.$total_dealers.'</p></th>'; echo '</tfoot>'; echo '</table>'; } ?> </table> </fieldset> </form> </section> <footer> <p>© 2017 Accolade Consulting, Partners with Nissan</p> </footer> </body> what i am currently struggling with is the following. When i click on delete now, it will delete every record as per code ive written. But when i try to delete only a specific record. it does not want to work. I know i am suppose to pass the id to delete_transaction.php in order for it to delete the record, but i am only a novice for now. Can any1 help me to pass the id to the form that will action the delete query? Quote Link to comment https://forums.phpfreaks.com/topic/305452-responsive-php-website/ Share on other sites More sharing options...
benanamen Posted October 25, 2017 Share Posted October 25, 2017 Your code is obsolete, dangerous and complete junk. You need to toss it and the tutorial you got it from. You need to use PDO with prepared statements. There is no fixing this code. Quote Link to comment https://forums.phpfreaks.com/topic/305452-responsive-php-website/#findComment-1553025 Share on other sites More sharing options...
Stephie22 Posted October 25, 2017 Author Share Posted October 25, 2017 thank you for the response. any tutorials where i can go and learn the PDO side? 1 Quote Link to comment https://forums.phpfreaks.com/topic/305452-responsive-php-website/#findComment-1553026 Share on other sites More sharing options...
Barand Posted October 25, 2017 Share Posted October 25, 2017 phpdelusions.net Quote Link to comment https://forums.phpfreaks.com/topic/305452-responsive-php-website/#findComment-1553027 Share on other sites More sharing options...
Psycho Posted October 26, 2017 Share Posted October 26, 2017 To answer your question about "how" to accomplish this, there are a few options. The easiest to implement is to use a hyperlink to delete a record instead of a form. So, in the while loop to display the records create a delete link something like this echo '<a href="delete_dealer?id={$row['dealer_id']}">Delete</a>'; If you want to use a form (as you are currently doing), you could create a separate form for each button. Right now you start the form, then output the records (along with the delete buttons, then you close the form. So, when a user clicks a button, you don't know which one they clicked. The problem with an input type=button is that the value is the label of the button. So, you can't put the ID as the value. So, as as you create the output for each record, create a "mini" form for each delete button and use a hidden field for the ID echo '<form action="delete_dealer.php" method="post"><input type="hidden" name="dealer_id" value="{$row['dealer_id']}"><button type="submit">Delete</button></form>'; Although you *could* use one form for the entire page you have now and make each delete button a submit button with a javascript event to populate a hidden field with the selected ID to be deleted. But, I personally don't like relying upon javascript for that type of behavior. Another option is to create one form and add a checkbox for each record with a single delete button for the page. Then, a user can select one or more records for deletion and then submit the entire page to delete all of them. Quote Link to comment https://forums.phpfreaks.com/topic/305452-responsive-php-website/#findComment-1553058 Share on other sites More sharing options...
benanamen Posted October 26, 2017 Share Posted October 26, 2017 @Psyco, this is not going to work echo '<a href="delete_dealer?id={$row['dealer_id']}">Delete</a>'; This will though echo "<a href='delete_dealer?id={$row['dealer_id']}'>Delete</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/305452-responsive-php-website/#findComment-1553064 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.