aal9_programmer Posted March 17, 2011 Share Posted March 17, 2011 Hey all, I'm currently working on a website/database system. What i'm trying to achieve atm is for a user to be able to edit current data in a database table, submit it, and for it to display the new data in the webpage. The table i'm reading from has 6 records and the fields ID(which is just auto_incremented) and courseName. Ideally, I would like the user to be able to delete a record too. The code I have written displays the courseName's in a table along with textareas and a submit button for each row. It seems to be doing nothing at the minute, so I was hoping someone could enlighten me as to what I've done wrong and if someone could provide what code i'm missing if any, i would be ever so grateful I think I might have structured it all wrong, I hope this isn't the case! My current code is below, hope to hear some feedback soon, many thanks, Aaron PS: feel free to ask any more question in case I've forgot to mention something <?php session_start(); $hostname_logon = "***.*.*.*" ; $database_logon = "aal9" ; $username_logon = "root" ; $password_logon = "" ; $coursenametext = ''; $id = ''; //error_reporting (E_ALL ^ E_NOTICE); $connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ( "Unable to connect to the database" ); mysql_select_db($database_logon) or die ( "Unable to select database!" ); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Edit Courses</title> <link href="Aaron.css" rel="stylesheet" type="text/css" /> </head> <div class="container"> <h1><img src="logo.gif" alt="Logo" width="224" height="62" id="logo" align="left"/></a>Update Courses</h1> </div> <div class="sidebar1"> <ul class="nav"> <li><a href="homepage.php">Homepage</a></li> <li><a href="modules.php">My Modules</a></li> <li><a href="update.php">Update Database</a></li> <li><a href="googles1.php">Sem 1 Google</a></li> <li><a href="timetable2.php">Sem 2 Timetable</a></li> <li><a href="googles2.php">Sem 2 Google</a></li> <li><a href="logout.php">Logout</a></li> </ul> <p><a href="javascript:window.print()">Print This Page</a></p> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. </p> <p>Welcome To Timetable Management. Please Find All Necessary Links Above.</p> </div> <div class="content"> <form action="editcourses.php" method="post"> <table border="1" cellspacing="1" align="center" bordercolor="#FFFFFF"> <tr> <td>Course Name</td> <td>Edit</td> <td>Submit</td> <td>Delete</td> </tr> <?php $query = "SELECT * FROM Courses "; $result = mysql_query ($query); if(mysql_num_rows($result)>0){ while($row = mysql_fetch_array($result)) { echo('<tr>' . '<td>' . $row['CourseName'] . '</td>' . '<td><input type="hidden" name="id" value="' . $row['ID'] . '"/><textarea name="coursenametext" rows="2"></textarea>' . '</td>' . '<td>' . '<input type="Submit" name="Submit" value="Submit">' . '</td>' . '</tr>'); } } if(isset($_POST['Submit'])) { $query1 = "UPDATE courses SET courseName = '".$_POST['coursenametext']."' WHERE ID = '".$_POST['id']."'"; $result1 = mysql_query ($query1); } ?> </table> </form> <br> </div> <div class="footer"> <p><a href="mailto:***@**.**.**" class="Email">Click Here To Email For Help With Faults</a></p> </div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/230911-newbie-in-need-of-assistance/ Share on other sites More sharing options...
fife Posted March 17, 2011 Share Posted March 17, 2011 try and pull the users ID from the database before you begin if(isset($_POST['Submit'])) { As i dont see where it is getting the ID from. Quote Link to comment https://forums.phpfreaks.com/topic/230911-newbie-in-need-of-assistance/#findComment-1188630 Share on other sites More sharing options...
fife Posted March 17, 2011 Share Posted March 17, 2011 ah sorry I see use your query to get the id and store it in a variable first then on update $query = mysql_query("UPDATE `courses` SET courseName = ".$_POST['coursenametext']." WHERE` ID` = '$id' "); Quote Link to comment https://forums.phpfreaks.com/topic/230911-newbie-in-need-of-assistance/#findComment-1188631 Share on other sites More sharing options...
fife Posted March 17, 2011 Share Posted March 17, 2011 I dont think putting the users ID into the form is a good idea and make sure you run the code through $course = mysql_real_escape_string(trim($_POST['coursenametext'])); before you enter it into the database. Quote Link to comment https://forums.phpfreaks.com/topic/230911-newbie-in-need-of-assistance/#findComment-1188633 Share on other sites More sharing options...
gristoi Posted March 17, 2011 Share Posted March 17, 2011 You are putting your textarea 'coursenametext' into a loop. therefore if you have 6 textareas they are all going to have the same name, so the form will more than likely only take the value from the last textarea ( number 6) and ignore the rest, regardless of which submit button you are pressing Quote Link to comment https://forums.phpfreaks.com/topic/230911-newbie-in-need-of-assistance/#findComment-1188636 Share on other sites More sharing options...
aal9_programmer Posted March 17, 2011 Author Share Posted March 17, 2011 thank you for your replies so far. i have just tested that gristoi and that is indeed what is happening, it is reading the 6th box only. how can i change it so it works correctly? sorry for being such a newbie pain! Quote Link to comment https://forums.phpfreaks.com/topic/230911-newbie-in-need-of-assistance/#findComment-1188638 Share on other sites More sharing options...
gristoi Posted March 17, 2011 Share Posted March 17, 2011 Hi, the hole you have fallen into is that you are dynamically genterating multiple Id's and textareas in a single form. So when you post the data you are posting 6 textareas and 6 seperate ids. The form will automatically grab the last id and textarea. so it will always submit ID6 and textarea 6. What you want to do in essence is to dynamically create 6 DIFFERENT FORMS. So all you need to do is move your form tags into the while loops. So, from this: <form action="editcourses.php" method="post"> <table border="1" cellspacing="1" align="center" bordercolor="#FFFFFF"> <tr> <td>Course Name</td> <td>Edit</td> <td>Submit</td> <td>Delete</td> </tr> <?php $query = "SELECT * FROM Courses "; $result = mysql_query ($query); if(mysql_num_rows($result)>0){ while($row = mysql_fetch_array($result)) { echo('<tr>' . '<td>' . $row['CourseName'] . '</td>' . '<td><input type="hidden" name="id" value="' . $row['ID'] . '"/><textarea name="coursenametext" rows="2"></textarea>' . '</td>' . '<td>' . '<input type="Submit" name="Submit" value="Submit">' . '</td>' . '</tr>'); } } if(isset($_POST['Submit'])) { $query1 = "UPDATE courses SET courseName = '".$_POST['coursenametext']."' WHERE ID = '".$_POST['id']."'"; $result1 = mysql_query ($query1); } ?> </table> </form> to this: <table border="1" cellspacing="1" align="center" bordercolor="#FFFFFF"> <tr> <td>Course Name</td> <td>Edit</td> <td>Submit</td> <td>Delete</td> </tr> <?php $query = "SELECT * FROM Courses "; $result = mysql_query ($query); if(mysql_num_rows($result)>0){ while($row = mysql_fetch_array($result)) { echo'<form action="editcourses.php" method="post">'; echo('<tr>' . '<td>' . $row['CourseName'] . '</td>' . '<td><input type="hidden" name="id" value="' . $row['ID'] . '"/><textarea name="coursenametext" rows="2"></textarea>' . '</td>' . '<td>' . '<input type="Submit" name="Submit" value="Submit">' . '</td>' . '</tr>' '</form>'; ); } } if(isset($_POST['Submit'])) { $query1 = "UPDATE courses SET courseName = '".$_POST['coursenametext']."' WHERE ID = '".$_POST['id']."'"; $result1 = mysql_query ($query1); } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/230911-newbie-in-need-of-assistance/#findComment-1188641 Share on other sites More sharing options...
aal9_programmer Posted March 17, 2011 Author Share Posted March 17, 2011 ahhh thank you so much! I see what the problem was now. I'd now like the user (admin for the database) to be able to delete and add a new record into the same table. Is this possible in the same webpage? could you provide me with guidelines if so? many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/230911-newbie-in-need-of-assistance/#findComment-1188671 Share on other sites More sharing options...
gristoi Posted March 17, 2011 Share Posted March 17, 2011 try: <table border="1" cellspacing="1" align="center" bordercolor="#FFFFFF"> <tr> <td>Course Name</td> <td>Edit</td> <td>Submit</td> <td>Delete</td> </tr> <?php $query = "SELECT * FROM Courses "; $result = mysql_query ($query); if(mysql_num_rows($result)>0){ while($row = mysql_fetch_array($result)) { echo'<form action="editcourses.php" method="post">'; echo('<tr>' . '<td>' . $row['CourseName'] . '</td>' . '<td><input type="hidden" name="id" value="' . $row['ID'] . '"/><textarea name="coursenametext" rows="2"></textarea>' . '</td>' . '<td>' . '<input type="Submit" name="Submit" value="Submit">' . '</td>' . '<td>' . '<input type="Submit" name="Delete" value="Delete">' . '</td>' . '</tr>' '</form>'; ); } } if(isset($_POST['Submit'])) { $query1 = "UPDATE courses SET courseName = '".$_POST['coursenametext']."' WHERE ID = '".$_POST['id']."'"; $result1 = mysql_query ($query1); } if(isset($_POST['Delete'])) { $query1 = "Delete FROM courses WHERE ID = '".$_POST['id']."'"; $result1 = mysql_query ($query1); } ?> </table> and as for adding a new one, you just need to create a new form outside of the loop and name the button create etc...... Quote Link to comment https://forums.phpfreaks.com/topic/230911-newbie-in-need-of-assistance/#findComment-1188673 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.