tracy Posted November 30, 2006 Share Posted November 30, 2006 This is the connection language required on my server:dollarsign dbh=mysql_connect ("localhost", "invent_user", "<PASSWORD HERE>") or die ('I cannot connect to the database because: ' . mysql_error());mysql_select_db ("invent_inventory"); The table name is inventory and the database name is also inventory...just to keep it simple. username is user, which is converted to invent_user by phpmyadmin.This is the table info in mysql:id (this is an auto incremented unique id not shown to the browser)stock (this is the stock number of the item)year (this is a year, max two digits)make (this is an auto make)model (this is the model)price (this is the price, max five digits with no dollar sign)photo (this is a jpg, which I would like displayed as an auto thumbnailed image)description (this is 200 characters max.)Suppose I have created the above table in mysql. I'd like to display it to the browser using php. I can do that, no problem.But when I want to display it for editing (with an edit and delete option displayed beside each row) I can not do that. Any suggestions? This page for editing will be password protected, yes. I know how to do all that. I just don't know how to best edit and delete items from the table using php.Any tutorial that you KNOW works would also be fine. Thanks.Kind of new to php so please keep your language simple. I know quite a bit about html, web hosting, etc. but not all the php jargon yet. And once again, I appreciate your help. Quote Link to comment https://forums.phpfreaks.com/topic/28978-edit-delete-mysql-records-with-php/ Share on other sites More sharing options...
craygo Posted November 30, 2006 Share Posted November 30, 2006 Can you give me your code to display each row so I can add the function in the right place.Ray Quote Link to comment https://forums.phpfreaks.com/topic/28978-edit-delete-mysql-records-with-php/#findComment-132742 Share on other sites More sharing options...
AV1611 Posted November 30, 2006 Share Posted November 30, 2006 What you ask can be done, but takes a bit of coding.One possibility would be this:Display your table in blocks of records. When you do, simply add a couple of form buttons, named editx and deletex where x=the number of row return. That way, when the button is pressed, you can be launched to that exact record in a pre-filled out form that you can edit, or you can click the other button, and just delete that record... better still, move the delete to the edit form screen. Quote Link to comment https://forums.phpfreaks.com/topic/28978-edit-delete-mysql-records-with-php/#findComment-132743 Share on other sites More sharing options...
tracy Posted November 30, 2006 Author Share Posted November 30, 2006 Thank you both. I think I'd like the edit page just like you describe, with the delete button as an option on the bottom of that page. I would post the code but the actual code I wrote was for a table with many more entries and I tried to simplify my example here for the forum. I'm not trying to get the actual code written for me, just an understanding or example of it so that I can modify it. I appreciate all your help, both of you.When you speak of adding this edit form, would the form be in php or html? Quote Link to comment https://forums.phpfreaks.com/topic/28978-edit-delete-mysql-records-with-php/#findComment-132746 Share on other sites More sharing options...
tracy Posted November 30, 2006 Author Share Posted November 30, 2006 could you give an examle of that form button you mentioned?i guess you mean to write it into the php display page that contains the table data with the edit button at the beginning of each rowand when the button/icon is selected the command from php will be to select the data in that line and display to the edit page?thanks. Quote Link to comment https://forums.phpfreaks.com/topic/28978-edit-delete-mysql-records-with-php/#findComment-132758 Share on other sites More sharing options...
craygo Posted November 30, 2006 Share Posted November 30, 2006 OK so you have a form which displays all your entries in the table. add a radio button in the last row[code]<td align=center><input type=radio name=rowid value=<?=xxxxxx?>>[/code]Replace xxxxxx with the data from your query for the field idthen on the page that shows the form to edit you will get the row to edit with this query[code]<?phpif(isset($_POST['delete'])){// put delete query here} else { if(isset($_POST['edit'])){// put update query here } else {$sql = "SELECT * FROM inventory WHERE id = '".$_POST['rowid']."'";$res = mysql_query($sql) or die (mysql_error());$row = mysql_fetch_assoc($res); // since you are only editing one row no need for loop// start your form hereecho "<form action=\"\" method=POST>";echo "<input type=hidden name=id value=\"".$_POST['rowid']."\">";// blah blah blah// Submit buttonsecho "<input type=submit name=delete value=Delete>";echo "<input type=submit name=edit value=Edit>"; }}?>[/code]This will display the form when you select the row from previous page then when you submit it will either delete or edit. Quote Link to comment https://forums.phpfreaks.com/topic/28978-edit-delete-mysql-records-with-php/#findComment-132762 Share on other sites More sharing options...
tracy Posted November 30, 2006 Author Share Posted November 30, 2006 Thank you. I'll try that. I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/28978-edit-delete-mysql-records-with-php/#findComment-132766 Share on other sites More sharing options...
craygo Posted November 30, 2006 Share Posted November 30, 2006 I modified my post. If you copied that replace the <form> line I forgot to escape the quotes Quote Link to comment https://forums.phpfreaks.com/topic/28978-edit-delete-mysql-records-with-php/#findComment-132768 Share on other sites More sharing options...
tracy Posted November 30, 2006 Author Share Posted November 30, 2006 Not gotten to it yet, but thanks.I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/28978-edit-delete-mysql-records-with-php/#findComment-132771 Share on other sites More sharing options...
tracy Posted December 4, 2006 Author Share Posted December 4, 2006 here is what I made and the error is listed in the code, about line 20...thanks. The form is different than at the beginning of this post...<html><title>Add Data Form</title><body><?phpif(isset($_POST['delete'])){// put delete query here} else { if(isset($_POST['edit'])){// put update query here } else {$sql = "SELECT * FROM inventory WHERE id = '".$_POST['rowid']."'";$res = mysql_query($sql) or die (mysql_error());$row = mysql_fetch_assoc($res); // since you are only editing one row no need for loop// start your form hereecho "<form action=\"\" method=POST>";echo "<input type=hidden name=id value=\"".$_POST['rowid']."\">";///here is where it says the error is.......unexpected < error on the next line...any thoughts?<table><tr><td>Stock Number:</td><td>input type="varchar" name="stock" /></td></tr><td>Year:</td><td>input type="tinyint" name="year" /></td></tr><td>Make:</td><td>input type="text" name="make" /></td></tr><td>Model:</td><td>input type="varchar" name="model" /></td></tr><td>Price:</td><td>input type="tinyint" name="price" /></td><input type="submit" /></form>// Submit buttonsecho "<input type=submit name=delete value=Delete>";echo "<input type=submit name=edit value=Edit>"; }}?><?php<form action="insert.php" method="post>?></body></html> Quote Link to comment https://forums.phpfreaks.com/topic/28978-edit-delete-mysql-records-with-php/#findComment-135191 Share on other sites More sharing options...
craygo Posted December 5, 2006 Share Posted December 5, 2006 That;s because you are trying to print html code inside of your php tags without an echo. If you just want to print out html code close your php tags then reopen them[code]<html><title>Add Data Form</title><body><?phpif(isset($_POST['delete'])){// put delete query here} else { if(isset($_POST['edit'])){// put update query here } else {$sql = "SELECT * FROM inventory WHERE id = '".$_POST['rowid']."'";$res = mysql_query($sql) or die (mysql_error());$row = mysql_fetch_assoc($res); // since you are only editing one row no need for loop// start your form hereecho "<form action=\"\" method=POST>";echo "<input type=hidden name=id value=\"".$_POST['rowid']."\">";?><table><tr><td>Stock Number:</td><td>input type="text" name="stock" /></td></tr><td>Year:</td><td>input type="text" name="year" /></td></tr><td>Make:</td><td>input type="text" name="make" /></td></tr><td>Model:</td><td>input type="text" name="model" /></td></tr><td>Price:</td><td>input type="text" name="price" /></td><input type="submit" /></form><?php// Submit buttonsecho "<input type=submit name=delete value=Delete>";echo "<input type=submit name=edit value=Edit>"; }}?><form action="insert.php" method="post"></body></html>[/code]Notice the closing php tag after the hidden value. Also if you have 2 submit buttons there is no need to have the first submit so I took it out. Also there is no such thing as form types of varchar or interger. only types are text, password checkbox, radio submit, reset, file, hidden, image and button. So for anything you want a user to type something in you have to use text or text area. text area is used for thing like quotes and paragraphs and such. one line inputs should be just text.Ray Quote Link to comment https://forums.phpfreaks.com/topic/28978-edit-delete-mysql-records-with-php/#findComment-135440 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.