Jump to content

Edit database content


Go to solution Solved by Simpson,

Recommended Posts

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.