...

View Full Version : Basic PHP script help



tomraffe
01-07-2010, 05:37 PM
I needed a very basic table with add/edit/delete functionality and found one that only had 2 columns, I needed 3 however. I have added an extra field in the form and DB and can add new records without any problem. I do however have a problem when trying to edit records, I can edit successfully but when updating a record the value of column 3 will move to 2, 2 to 1, and 1 will disappear. 3 will then become blank. I'm sure this is a simple fix for someone that knows what they are doing.

Index file


<form action="" method="post" name="" id="">
<table width="600" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td><input name="topcheckbox" type="checkbox" class="check" id="topcheckbox" onClick="selectall();" value="ON">
Select All &nbsp;&nbsp;&nbsp;</td>
<td colspan="4" align="center"><a href="form.php?mode=add">Add New Order</a></td>
</tr>
<tr>
<td><strong><a href="javascript:goDel()">Delete</a></strong></td>
<td><strong>Order Number</strong></td>
<td><strong>Order Status</strong></td>
<td><strong>Payment Status</strong></td>
<td><strong>Update</strong></td>
</tr>
<?
include("conn.php");
$sql="select sn,branchname,shortname,payment from $branch order by sn";
$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
?>
<tr>
<td><input name="<? echo $row['sn']; ?>" type="checkbox" class="check"></td>
<td><? echo $row['branchname']; ?></td>
<td><? echo $row['shortname']; ?></td>
<td><? echo $row['payment']; ?></td>
<td><a href="<? echo "form.php?sn=".$row['sn']."&mode=update"; ?>">Update</a></td>
</tr>
<? } ?>
<tr>
<td><strong><a href="javascript:goDel()">Delete</a></strong></td>
<td><strong>Order Number</strong></td>
<td><strong>Order Status</strong></td>
<td><strong>Payment Status</strong></td>
<td><strong>Update</strong></td>
</tr>
</table>
</form></td>
</tr>
</table>


Form (add/edit)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Order Management</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td> <table width="700" border="0" align="center" cellpadding="2" cellspacing="2">
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>
<?
$mode=$_GET["mode"];
if($mode=="add") {
?>
<form name="form1" method="post" action="formsubmit.php?mode=add">
<table width="500" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td><strong>Add New Order</strong></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Order Number</td>
<td><input name="branchname" type="text" id="branchname"></td>
</tr>
<tr>
<td>Order Status</td>
<td><input name="shortname" type="text" id="shortname"></td>
</tr>
<tr>
<td>Payment Status</td>
<td><input name="payment" type="text" id="payment"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Save Data"></td>
<td>&nbsp;</td>
</tr>
</table>
</form>
<?
} else {
include("conn.php");
$sn=$_GET["sn"];
$sql="select sn,branchname,shortname,payment from $branch where sn='$sn'";

$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
$sn=$row['sn'];
$branchname=$row['branchname'];
$shortname=$row['shortname'];
$payment=$row['payment'];
}
?>
<form name="form1" method="post" action="formsubmit.php?mode=update">
<table width="500" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td><strong>Update Order</strong></td>
<td><input type="hidden" name="sn" value="<? echo $sn; ?>">
&nbsp;</td>
</tr>
<tr>
<td>Order Number</td>
<td><input name="branchname" type="text" id="branchname" value="<? echo $branchname; ?>"></td>
</tr>
<tr>
<td>Order Status</td>
<td><input name="shortname" type="text" id="shortname" value="<? echo $shortname; ?>"></td>
</tr>
<tr>
<td>Payment Status</td>
<td><input name="shortname" type="text" id="shortname" value="<? echo $payment; ?>"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Update Data"></td>
<td>&nbsp;</td>
</tr>
</table>
</form>

<?

}
?>

</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>

Form submit file


<?
include("conn.php");
$mode=$_GET["mode"];
if($mode=="add") {
$branchname=$_POST["branchname"];
$shortname=$_POST["shortname"];
$payment=$_POST["payment"];
$sql="insert into $branch(branchname,shortname,payment) values('$branchname','$shortname','$payment')";
$result=mysql_query($sql,$connection) or die(mysql_error());
header("location: admin-index.php");

} elseif($mode=="update") {
$branchname=$_POST["branchname"];
$shortname=$_POST["shortname"];
$payment=$_POST["payment"];
$sn=$_POST["sn"];
$sql="update $branch set branchname='$branchname',shortname='$shortname',payment='$payment' where sn='$sn'";
//echo $sql;
$result=mysql_query($sql,$connection) or die(mysql_error());
header("location: admin-index.php");
}
?>

angst
01-07-2010, 05:43 PM
your problem is here:




<form name="form1" method="post" action="formsubmit.php?mode=update">
<table width="500" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td><strong>Update Order</strong></td>
<td><input type="hidden" name="sn" value="<? echo $sn; ?>">
&nbsp;</td>
</tr>
<tr>
<td>Order Number</td>
<td><input name="branchname" type="text" id="branchname" value="<? echo $branchname; ?>"></td>
</tr>
<tr>
<td>Order Status</td>
<td><input name="shortname" type="text" id="shortname" value="<? echo $shortname; ?>"></td>
</tr>
<tr>
<td>Payment Status</td>
<td><input name="shortname" type="text" id="shortname" value="<? echo $payment; ?>"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Update Data"></td>
<td>&nbsp;</td>
</tr>
</table>
</form>


you have duplicate field names. "shortname"

should be:



<form name="form1" method="post" action="formsubmit.php?mode=update">
<table width="500" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td><strong>Update Order</strong></td>
<td><input type="hidden" name="sn" value="<? echo $sn; ?>">
&nbsp;</td>
</tr>
<tr>
<td>Order Number</td>
<td><input name="branchname" type="text" id="branchname" value="<? echo $branchname; ?>"></td>
</tr>
<tr>
<td>Order Status</td>
<td><input name="shortname" type="text" id="shortname" value="<? echo $shortname; ?>"></td>
</tr>
<tr>
<td>Payment Status</td>
<td><input name="payment" type="text" id="payment" value="<? echo $payment; ?>"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Update Data"></td>
<td>&nbsp;</td>
</tr>
</table>
</form>

Rowsdower!
01-07-2010, 05:47 PM
<form name="form1" method="post" action="formsubmit.php?mode=update">
<table width="500" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td><strong>Update Order</strong></td>
<td><input type="hidden" name="sn" value="<? echo $sn; ?>">
&nbsp;</td>
</tr>
<tr>
<td>Order Number</td>
<td><input name="branchname" type="text" id="branchname" value="<? echo $branchname; ?>"></td>
</tr>
<tr>
<td>Order Status</td>
<td><input name="shortname" type="text" id="shortname" value="<? echo $shortname; ?>"></td>
</tr>
<tr>
<td>Payment Status</td>
<td><input name="shortname" type="text" id="shortname" value="<? echo $payment; ?>"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Update Data"></td>
<td>&nbsp;</td>
</tr>
</table>
</form>

It looks like you are posting 'shortname' twice (which will only send the last shortname given) and completely skipping 'payment' in the edit form.

Whoops! angst beat me to it while I was scanning code...

angst
01-07-2010, 05:50 PM
Edit: Whoops! angst beat me to it while I was scanning code...

lol,, too slow! ;)

tomraffe
01-07-2010, 05:59 PM
Excellent, thanks to you both.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum