Simpson Posted January 29, 2014 Share Posted January 29, 2014 Hey, Im trying to make a page that will show the content from a database in a table, with an edit link to every row. So that when the user presses the link it goes to the edit.php file where he/she then can edit the content in the datebase with the specific id. I can get it to show the content in the table and to send the user to edit.php when edit is pressed, but I can't get it to transfer over the information to the edit.php. As i wannt my text input to have the value of the content currently in database, so its easier to edit. Here is the 2 codes. test_edit.php <!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>Untitled Document</title> </head> <body> <?php error_reporting (E_ALL ^ E_NOTICE); session_start(); $userid = $_SESSION['userid']; $username = $_SESSION['username']; ?> <?php require ('config.php'); $field = mysql_query("SELECT * FROM Field_Data")or die (mysql_error()); $dep = mysql_query("SELECT * FROM Department_Data")or die (mysql_error()); echo "<form action='test_edit.php' method='POST'> <label> Choose Week: <select name='week' size='1' id='week'>"; while ($row = mysql_fetch_array($field)){ echo '<option value=' . $row['week'] . '>' . $row['week'] . '</option>';} echo "</select> </label><br />"; echo "<label> Choose year: <input type='text' name='year' value='2014' /><br />"; echo "<label> Choose Department: <select name='department' size='1' id='department'>"; while ($row2 = mysql_fetch_array($dep)){ echo '<option value=' . $row2['name'] . '>' . $row2['name'] . '</option>';} echo "</select> </label><br />"; echo "<input type='submit' value='show' name='submit' /> </form>"; ?> <?php if (isset($_POST['submit'])){ $week_r = $_POST['week']; $dep_r = $_POST['department']; $sql=mysql_query("SELECT * FROM `Marketing` WHERE Week = '$week_r' and Department = '$dep_r'"); if (mysql_num_rows($sql) < 0){ $output = "Sorry there is no data with these values"; }else{ echo "<table id='marketing'> <thead> <th>Week</th> <th>Brand</th> <th>Product</th> <th>Department</th> <th>Group</th> <th>Number</th> <th>Product Type</th> <th>Display Type</th> <th>Count</th> <th>Price</th> <th>Goodsnr</th> <th>Username</th> </thead> <tbody>"; while ($rows = mysql_fetch_array($sql)){ echo '<tr>'; echo '<td>'. $rows['Week'] .'</td>'; echo '<td>'. $rows['Brand'] .'</td>'; echo '<td>'. $rows['Product'] .'</td>'; echo '<td>'. $rows['Department'] .''; echo '<td>'. $rows['Group'] .'</td>'; echo '<td>'. $rows['Number'] .'</td>'; echo '<td>'. $rows['ProductType'] .'</td>'; echo '<td>'. $rows['DisplayType'] .'</td>'; echo '<td>'. $rows['Count'] .'</td>'; echo '<td>'. $rows['Price'] .'</td>'; echo '<td>'. $rows['Goodsnr'] .'</td>'; echo '<td>'. $rows['Username'] .'</td>'; echo "<td><a href='edit.php?=". $rows['ID'] ."'>edit</a></td>"; echo "</tr>"; echo "</tbody>"; } } } echo $form; ?> </body> </html> edit.php <!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>Untitled Document</title> </head> <body> <?php require ('config.php'); if (isset($_GET['marketing'])) { $id = $_GET['marketing']; $res = mysql_query("SELECT * `Marketing` WHERE ID='$id'"); $row = mysql_fetch_array($res); } else { echo "Error no data was send"; } if (isset($_POST['newweek'])) { $newweek = $_POST['newweek']; $id = $_POST['id']; $sql = "UPDATE Marketing SET Week='$newweek' WHERE ID='$id'"; $res = mysql_query($sql); } ?> <form action="edit.php" method="post"> <label>Week<input type="text" name="newweek" value="<?php echo $row[1]; ?>"></label><br /> <input type="hidden" name="id" value="<?php echo $row[0]; ?>"> <input type="submit" value="Update"> </form> </body> </html> I hope you guys can see where the error is, as I can't seem to find it. Link to comment https://forums.phpfreaks.com/topic/285755-edit-database-content/ Share on other sites More sharing options...
cyberRobot Posted January 29, 2014 Share Posted January 29, 2014 You need to name the GET variables. For example: <a href='edit.php?id=". $rows['ID'] ."'>edit</a> Then in edit.php, you can access the variable using $_GET['id'] Link to comment https://forums.phpfreaks.com/topic/285755-edit-database-content/#findComment-1466896 Share on other sites More sharing options...
SalientAnimal Posted January 29, 2014 Share Posted January 29, 2014 There is a very nice tutorial available on this link which explains everything as you need. http://www.killersites.com/community/index.php?/topic/3064-basic-php-system-view-edit-add-delete-records-with-mysqli/ Link to comment https://forums.phpfreaks.com/topic/285755-edit-database-content/#findComment-1466904 Share on other sites More sharing options...
Simpson Posted January 31, 2014 Author Share Posted January 31, 2014 Thx guys solved it Link to comment https://forums.phpfreaks.com/topic/285755-edit-database-content/#findComment-1467152 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.