Wrox Programmer Forums
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 6th, 2004, 03:45 AM
Authorized User
 
Join Date: Apr 2004
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to junemo
Default user manager

Hi, I want create a user manager that have function --- list, edit , delete & add the data. I follow the code in book Beginning PHP4. But the page always link to default page each time I click the button (whatever i click).
So, what's wrong?
thanks.

<?php
//userman.php
include "./common_db.inc";
$link_id = db_connect();
mysql_select_db("invoice_db");
mysql_close($link_id);
function list_records() {
global $default_dbname, $bill_tablename;
global $default_sort_order, $default_order_by, $records_per_page;
global $sort_order, $order_by, $cur_page;
global $PHP_SELF;
$link_id = db_connect($default_dbname);
if(!$link_id) error_message(sql_error());
$query = "SELECT count(*) FROM $bill_tablename";
$result = mysql_query($query);
if(!$result) error_message(sql_error());
$query_data = mysql_fetch_row($result);
$total_num_user = $query_data[0];
if(!$total_num_user) error_message('No Invoice Found!');
$page_num = $cur_page + 1;

$total_num_page = $last_page_num
                 = ceil($total_num_user/$records_per_page);
html_header();
echo "<CENTER><H3>$total_num_user invoices found. Displaying the page
                     $page_num out of $last_page_num.</H3></CENTER>\n";

if(empty($order_by)) {
     $order_by_str = "ORDER BY $default_order_by";
     $order_by = $default_order_by;
}
else $order_by_str = "ORDER BY $order_by";
if(empty($sort_order)) {
     $sort_order_str = $org_sort_order = $default_sort_order;
     $sort_order = 'DESC';
}
else {
     $sort_order_str = $org_sort_order = $sort_order;
     if($sort_order == 'DESC') $sort_order = 'ASC';
     else $sort_order = 'DESC';
}
if(empty($cur_page)) {
     $cur_page = 0;
}
     $limit_str = "LIMIT ". $cur_page * $records_per_page .
                                     ", $records_per_page";
$query = "SELECT invnumber, invno, billto, shipto FROM bill
                                 $order_by_str $sort_order_str $limit_str";

$result = mysql_query($query);
if(!$result) error_message(sql_error());
?>
<DIV ALIGN="CENTER">
<TABLE BORDER="1" WIDTH="90%" CELLPADDING="2">
<TR>
     <TH WIDTH="10" NOWRAP>
         <A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_or der&order_by=invnumber"; ?>">
         Number
         </A>
     </TH>
     <TH WIDTH="10%" NOWRAP>
         <A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_or der&order_by=invno"; ?>">
         Invoice No
         </A>
     </TH>
     <TH WIDTH="30%" NOWRAP>
         <A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_or der&order_by=billto"; ?>">
            Bill To
         </A>
     </TH>
     <TH WIDTH="30%" NOWRAP>
         <A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_or der&order_by=shipto"; ?>">
            Ship To
         </A>
     </TH>
     <TH WIDTH="25%" NOWRAP>Action</TH>
</TR>
<?php
while($query_data = mysql_fetch_array($result)) {
     $invnumber = $query_data["invnumber"];
     $invno = $query_data["invno"];
     $billto = $query_data["billto"];
     $shipto = $query_data["shipto"];
     echo "<TR>\n";
     echo "<TD WIDTH=\"10%\" ALIGN=\"CENTER\">$invnumber</TD>\n";
     echo "<TD WIDTH=\"10%\" ALIGN=\"CENTER\">$invno</TD>\n";
     echo "<TD WIDTH=\"30%\" ALIGN=\"CENTER\">$billto</TD>\n";
     echo "<TD WIDTH=\"30%\" ALIGN=\"CENTER\">$shipto</TD>\n";
     echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">
            <A HREF=\"javascript:open_window('$PHP_SELF?action=vi ew_record&invno=$invno');\">View</A>
            <A HREF=\"$PHP_SELF?action=delete_record&invno=$invno \" onClick=\"return confirm('Are you sure?');\">Delete</A></TD>\n";
     echo "</TR>\n";
}
?>
</TABLE>
</DIV>
<?php
echo "<BR>\n";
echo "<STRONG><CENTER>";
if($page_num > 1) {
     $prev_page = $cur_page - 1;

     echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$o rg_sort_order&order_by=$order_by&cur_page=0\">[Top]</A>";

     echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$o rg_sort_order&order_by=$order_by&cur_page=$prev_pa ge\">[Prev]</A>";
}
if($page_num < $total_num_page) {
     $next_page = $cur_page + 1;
     $last_page = $total_num_page - 1;

     echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$o rg_sort_order&order_by=$order_by&cur_page=$next_pa ge\">[Next]</A>";

     echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$o rg_sort_order&order_by=$order_by&cur_page=$last_pa ge\">[Bottom]</A>";
}
echo "</STRONG></CENTER>";
html_footer();
}
function delete_record() {
global $default_dbname, $bill_tablename, $item_tablename;
global $invno;

if(empty($invno)) error_message('Empty Invoice No!');

$link_id = db_connect($default_dbname);
if(!$link_id) error_message(sql_error());

$query = "DELETE FROM $bill_tablename WHERE invno = '$invno'";
$result = mysql_query($query);
if(!$result) error_message(sql_error());

$num_rows = mysql_affected_rows($link_id);
if($num_rows != 1) error_message("No such invoice no: $invno");
$query = "DELETE FROM item WHERE invno = '$invno'";
$result = mysql_query($query);

user_message("All records regarding $invno have been trashed!");
}
function edit_record() {
global $default_dbname, $bill_tablename, $item_tablename;
global $invno, $new_invno, $invno, $billto, $shipto,
         $invdate, $pono, $dnno, $currency, $pricebase,
         $term;
if(empty($invno)) error_message('Empty Invoice No!');
$link_id = db_connect($default_dbname);
if(!$link_id) error_message(sql_error());
$field_str = '';
if($invno != $new_invno) $field_str = " invno = '$new_invno', ";
$field_str .= " billto = '$billto', ";
$field_str .= " shipto = '$shipto', ";
$field_str .= " invdate = '$invdate', ";
$field_str .= " pono = '$pono', ";
$field_str .= " dnno = '$dnno', ";
$field_str .= " currency = '$currency', ";
$field_str .= " pricebase = '$pricebase',";
$field_str .= " term = '$term'";
$query = "UPDATE $bill_tablename SET $field_str WHERE invno = '$invno'";
$result = mysql_query($query);
if(!$result) error_message(sql_error());
$num_rows = mysql_affected_rows($link_id);
if(!$num_rows) error_message("Nothing changed!");
if($invno != $new_invno) {
    $query = "UPDATE $item_tablename SET invno = '$new_invno'
                                         WHERE invno = '$invno'";
    $result = mysql_query($query);
    if(!$result) error_message(sql_error());

    user_message("All records regarding $invno have been changed!",
                 "$PHP_SELF?action=view_record&invno=$new_invno") ;
}
else {
    user_message("All records regarding $invno have been changed!");
}
}
function edit_log_record() {
global $default_dbname, $item_tablename;
global $invno, $org_total_pos, $new_total_pos, $stock, $qtydelivered, $uom, $unitprice, $amount;

if(empty($invno)) error_message('Empty Invoice No!');

$link_id = db_connect($default_dbname);
if(!$link_id) error_message(sql_error());

$field_str = '';
$field_str .= " total_pos = '$new_total_pos', ";
$field_str .= " stock = '$stock', ";
$field_str .= " qtydelivered = '$qtydelivered', ";
$field_str .= " uom = '$uom', ";
$field_str .= " unitprice = '$unitprice', ";
$field_str .= " amount = '$amount' ";
$query = "UPDATE $item_tablename SET $field_str
                                         WHERE invno = '$invno'
                                         AND total_pos = '$org_total_pos'";
$result = mysql_query($query);
if(!$result) error_message(sql_error());
$num_rows = mysql_affected_rows($link_id);
if(!$num_rows) error_message("Nothing changed!");

user_message("All records regarding $invno have been changed!");
}

function view_record() {
global $default_dbname, $bill_tablename, $item_tablename;
global $invno;
global $PHP_SELF;

if(empty($invno)) error_message('Empty Invoice No!');
$link_id = db_connect($default_dbname);

if(!$link_id) error_message(sql_error());
$query = "SELECT invnumber, invno, billto, shipto,
                 invdate, pono, dnno, currency,
         pricebase, term,
                 date_format(invdate, '%M, %e, %Y')
                     as formatted_invdate,
                 FROM $bill_tablename WHERE invno = '$invno'";
$result = mysql_query($query);

if(!$result) error_message(sql_error());
$query_data = mysql_fetch_array($result);
$invnumber = $query_data["invnumber"];
$invno = $query_data["invno"];
$billto = $query_data["billto"];
$shipto = $query_data["shipto"];
$invdate = $query_data["invdate"];
$formatted_invdate = $query_data["formatted_invdate"];
$pono = $query_data["pono"];
$dnno = $query_data["dnno"];
$currency = $query_data["currency"];
$pricebase = $query_data["pricebase"];
$term = $query_data["term"];

html_header();
echo "<CENTER><H3>
        Record for Invoice No.$invnumber - $invno ($billto)
        </H3></CENTER>";
?>
<FORM METHOD="POST" ACTION="<?php echo $PHP_SELF; ?>">
<INPUT TYPE="HIDDEN" NAME="action" VALUE="edit_record">
<INPUT TYPE="HIDDEN" NAME="invno" VALUE="<? echo $invno; ?>">
<DIV ALIGN="CENTER"><CENTER>
<TABLE BORDER="1" WIDTH="90%" CELLPADDING="2">
    <TR>
     <TH WIDTH="30%" NOWRAP>Invoice No</TH>
     <TD WIDTH="70%">
     <INPUT TYPE="TEXT" NAME="new_invno"
                         VALUE="<?php echo $_POST["invno"]; ?>"
                         SIZE="8" MAXLENGTH="13"></TD>
    </TR>
    <TR>
     <TH WIDTH="30%" NOWRAP>Bill To</TH>
     <TD WIDTH="70%">
    <TEXTAREA ROWS="5" COLS="30" NAME="billto">
    <?php echo $_POST["billto"]; ?>
    </TEXTAREA>
    </TD>
    </TR>
    <TR>
     <TH WIDTH="30%" NOWRAP>Ship To</TH>
     <TD WIDTH="70%">
    <TEXTAREA ROWS="5" COLS="30" NAME="shipto">
    <?php echo $_POST["shipto"]; ?>
    </TEXTAREA>
    </TD>
    </TR>
    <TR>
     <TH WIDTH="30%" NOWRAP>Invoice Date</TH>
     <TD WIDTH="70%">
        <INPUT TYPE="TEXT" NAME="invdate" SIZE="10" MAXLENGTH="10"
                         VALUE="<?php echo $_POST["invdate"]; ?>">
        <?php echo $formatted_invdate;?>
     </TD>
    </TR>
    TR>
     <TH WIDTH="30%" NOWRAP>Your PO No</TH>
     <TD WIDTH="70%">
     <INPUT TYPE="TEXT" NAME="pono"
                         VALUE="<?php echo $_POST["pono"]; ?>"
                         SIZE="8" MAXLENGTH="15"></TD>
    </TR>
    TR>
     <TH WIDTH="30%" NOWRAP>DN No</TH>
     <TD WIDTH="70%">
     <INPUT TYPE="TEXT" NAME="dnno"
                         VALUE="<?php echo $_POST["dnno"]; ?>"
                         SIZE="8" MAXLENGTH="15"></TD>
    </TR>
    TR>
     <TH WIDTH="30%" NOWRAP>Currency</TH>
     <TD WIDTH="70%">
     <INPUT TYPE="TEXT" NAME="currency"
                         VALUE="<?php echo $_POST["currency"]; ?>"
                         SIZE="8" MAXLENGTH="15"></TD>
    </TR>
    TR>
     <TH WIDTH="30%" NOWRAP>Price Base</TH>
     <TD WIDTH="70%">
     <INPUT TYPE="TEXT" NAME="pricebase"
                         VALUE="<?php echo $_POST["pricebase"]; ?>"
                         SIZE="8" MAXLENGTH="15"></TD>
    </TR>
    TR>
     <TH WIDTH="30%" NOWRAP>Term</TH>
     <TD WIDTH="70%">
     <INPUT TYPE="TEXT" NAME="term"
                         VALUE="<?php echo $_POST["term"]; ?>"
                         SIZE="8" MAXLENGTH="20"></TD>
    </TR>
    <TR>
     <TH WIDTH="100%" COLSPAN="2" NOWRAP>
        <INPUT TYPE="SUBMIT" VALUE="Change User Record">
        <INPUT TYPE="RESET" VALUE="Reset">
     </TH>
    </TR>
</TABLE>
</CENTER></DIV>
</FORM>
<?php
echo "\n";
$query = "SELECT total_pos, stock, qtydelivered, uom,
            unitprice, amount
            FROM $item_tablename WHERE invno = '$invno'";
$result = mysql_query($query);

if(!$result) error_message(sql_error());
if(!mysql_num_rows($result))
    echo "<CENTER>No Invoice record for $invno ($billto).</CENTER>";
else {
    echo "<CENTER>Invoice record(s) for $invno ($billto).</CENTER>";
?>
<DIV ALIGN="CENTER"><CENTER>
<TABLE BORDER="1" WIDTH="90%" CELLPADDING="2">
<TR>
    <TH WIDTH="10%" NOWRAP>Pos</TH>
    <TH WIDTH="30%" NOWRAP>Stock No/Description</TH>
    <TH WIDTH="10%" NOWRAP>Qty Delivered</TH>
    <TH WIDTH="10%" NOWRAD>UOM</TH>
    <TH WIDTH="10%" NOWRAD>Unit Price</TH>
    <TH WIDTH="10%" NOWRAD>Amount</TH>
    <TH WIDTH="25%" NOWRAP>Action</TH>
</TR>
<?php
    while($query_data = mysql_fetch_array($result)) {
     $total_pos = $query_data["total_pos"];
     $stock = $query_data["stock"];
     $qtydelivered = $query_data["qtydelivered"];
     $uom = $query_data["uom"];
     $unitprice = $query_data["unitprice"];
     $amount = $query_data["amount"];
    
     echo "<FORM METHOD=\"POST\" ACTION=\$PHP_SELF\">";
     echo "<INPUT TYPE=\"HIDDEN\" NAME=\"action\"
                                 VALUE=\"edit_log_record\">";
     echo "<INPUT TYPE=\"HIDDEN\" NAME=\"invno\" VALUE=\"$invno\">";
     echo "<INPUT TYPE=\"HIDDEN\" NAME=\"org_total_pos\" VALUE=\"$total_pos\">";
     echo "<TR>\n";
     echo "<TD WIDTH=\"10%\"><INPUT TYPE=\"TEXT\"
                NAME=\"new_total_pos\" SIZE=\"15\" VALUE=\"$total_pos\"></TD>\n";
     echo "<TD WIDTH=\"30%\">
             <INPUT TYPE=\"TEXT\" NAME=\"stock\" SIZE=\"30\"
                                 VALUE=\"$stock\"></TD>\n";
     echo "<TD WIDTH=\"10%\" ALIGN=\"CENTER\">
             <INPUT TYPE=\"TEXT\" NAME=\"qtydelivered\" SIZE=\"14\"
                                VALUE=\"$qtydelivered\"></TD>\n";
     echo "<TD WIDTH=\"15%\" ALIGN=\"CENTER\">
             <INPUT TYPE=\"TEXT\" NAME=\"uom\" SIZE=\"14\"
                                VALUE=\"$uom\"></TD>\n";
     echo "<TD WIDTH=\"15%\" ALIGN=\"CENTER\">
             <INPUT TYPE=\"TEXT\" NAME=\"unitprice\" SIZE=\"14\"
                                VALUE=\"$unitprice\"></TD>\n";
     echo "<TD WIDTH=\"15%\" ALIGN=\"CENTER\">
             <INPUT TYPE=\"TEXT\" NAME=\"amount\" SIZE=\"14\"
                                VALUE=\"$amount\"></TD>\n";
     echo "<TD WIDTH=\"30%\" ALIGN=\"CENTER\">
             <INPUT TYPE=\"SUBMIT\" VALUE=\"Change\">
             <INPUT TYPE=\"RESET\" VALUE=\"Reset\"></TD>\n";
     echo "</TR>\n";
     echo "</FORM>\n";
    }
?>
</TR>
</TABLE>
</CENTER></DIV>
<?php
}
html_footer();
}
switch($action) {
case "edit_record":
    edit_record();
break;
case "edit_log_record":
    edit_log_record();
break;
case "delete_record":
    delete_record();
break;
case "view_record":
    view_record();
break;
default:
    list_records();
break;
?>





Similar Threads
Thread Thread Starter Forum Replies Last Post
What DB manager to use? elde SQL Server ASP 2 January 5th, 2005 05:39 AM
User can not view Report Manager contents lidsurfer BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 November 23rd, 2004 01:33 PM
File Manager Javascript in User Control seanmayhew BOOK: ASP.NET Website Programming Problem-Design-Solution 0 September 16th, 2004 09:42 AM
Password Manager katsarosj VB.NET 2002/2003 Basics 4 January 30th, 2004 12:35 AM
Enterprise Manager Jan_Ma SQL Server ASP 1 October 17th, 2003 08:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.