PHP forms (sad face).

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):

<?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>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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');
}

?>
 
Associate
Joined
18 Sep 2003
Posts
903
Have you had a look at the update query that update_ac.php is generating? If not, take a look by printing it out or emailing it to yourself or something.

When you say nothing happens, I assume you must at least get one of your two messages of "Successful" or "There was a problem, error code". Which one do you get?

Also, I'm not really sure what you're trying to do with this line
Code:
echo (integer)ini_get('display_errors');

All that will do is tell you what the PHP display_errors setting is. It won't tell you anything about the query you have just run. Instead, you might want to try something like:
Code:
print mysql_error() . ' (' .mysql_errno() . ')';

I assume this is just something you're toying with rather than anything you're going to put into use since the code is very insecure and you're using the old MySQL libraries which are going to be removed from PHP.
 
Associate
Joined
12 Feb 2003
Posts
897
Try changing update_ac.php like so:

<?php

$host = "localhost";
$username = "#";
$password = "#";
$db_name = "#";
$tbl_name = "#";

//assign variables
$productName = mysql_real_escape_string($_POST['productName']);
$productDescription = mysql_real_escape_string($_POST['productDescription']);
$productPrice = mysql_real_escape_string($_POST['productPrice']);


// 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');
}

?>
 
Back
Top Bottom