Associate
- Joined
- 22 Aug 2011
- Posts
- 240
Yet again I am back for some of your superior knowledge.
I am making a form or forms rather that can edit current tables and edit/change them to the users needs.
e.g.
id: 1
name: bob.
description: bob hates lettuce.
price: 1337.0
and the ability to change that to whatever, I've got it all complete and the code (from what I can see and determine, it seems perfectly fine) but upon submitting my changes? nothing happens.
I have three pages which are as follows:
list_records.php (which contains):
update.php - (this is the page where the previous chosen table would be updated/changes).
and finally, update_ac.php (which basically just tells the user if there is an error of if the update was a success pretty much.)
I am making a form or forms rather that can edit current tables and edit/change them to the users needs.
e.g.
id: 1
name: bob.
description: bob hates lettuce.
price: 1337.0
and the ability to change that to whatever, I've got it all complete and the code (from what I can see and determine, it seems perfectly fine) but upon submitting my changes? nothing happens.
I have three pages which are as follows:
list_records.php (which contains):
<?php
$host ="localhost";
$username ="#";
$password ="#";
$db_name ="#";
$tbl_name ="products";
// connect to server and select the db.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Admin Panel</title>
<link href="css/editstyle.css" rel="stylesheet" type="text/css">
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel=
'stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Playfair+Display' rel=
'stylesheet' type='text/css'>
<!-- scripts -->
<script>
function setbg(color)
{
document.getElementById("styled").style.background=color
}
</script>
</head>
<body>
<div class="header">
# - Admin
</div>
<div class="container">
<div class="sidebar1">
<br>
<div id="admin_options">
Admin Options
</div><br>
<ul class="nav">
<li><a href="new.php">New</a></li>
<li><a href="list_records.php">Edit</a></li>
<li><a href="#">Delete</a></li>
<li><a href="Logout.php">Logout</a></li>
</ul>
</div>
<div class="content">
<br>
<table border="0" class="products">
<tr>
<td colspan="10"><strong>List data from mysql </strong> </td>
</tr>
<tr>
<td align="center"><strong>Product Name</strong></td>
<td align="center"><strong>Product Description</strong></td>
<td align="center"><strong>Product Price</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['productName']; ?></td>
<td><? echo $rows['productDescription']; ?></td>
<td><? echo $rows['productPrice']; ?></td>
// link to update.php and send value of id
<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>
</div>
</div>
</body>
</html>
update.php - (this is the page where the previous chosen table would be updated/changes).
<?php
$host = "localhost";
$username = "#";
$password = "#";
$db_name = "#";
$tbl_name = "products";
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Product Name</strong></td>
<td align="center"><strong>Product Description</strong></td>
<td align="center"><strong>Product Price</strong></td>
</tr>
<tr>
<td> </td>
<td align="center">
<input name="productName" type="text" id="productName" value="<? echo $rows['productName']; ?>">
</td>
<td align="center">
<input name="productDescription" type="text" id="productDescription" value="<? echo $rows['productDescription']; ?>" size="15">
</td>
<td>
<input name="productPrice" type="text" id="productPrice" value="<? echo $rows['productPrice']; ?>" size="15">
</td>
</tr>
<tr>
<td> </td>
<td>
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
and finally, update_ac.php (which basically just tells the user if there is an error of if the update was a success pretty much.)
<?php
$host = "localhost";
$username = "#";
$password = "#";
$db_name = "#";
$tbl_name = "#";
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET productName='$productName', productDescription='$productDescription', productPrice='$productPrice' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}
else {
echo "There was a problem, error code: ";
echo (integer)ini_get('display_errors');
}
?>