Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2003
    Posts
    46
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Basic PHP script help

    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

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

    PHP Code:
    <!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

    PHP Code:
    <?
              
    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");
              }
              
    ?>

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    your problem is here:


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

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

  • Users who have thanked angst for this post:

    tomraffe (01-07-2010)

  • #3
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Code:
            <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.

    Edit: Whoops! angst beat me to it while I was scanning code...
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    Edit: Whoops! angst beat me to it while I was scanning code...
    lol,, too slow!

  • Users who have thanked angst for this post:

    tomraffe (01-07-2010)

  • #5
    New Coder
    Join Date
    Oct 2003
    Posts
    46
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Excellent, thanks to you both.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •