Jump to content

Form not updating database


mapletech

Recommended Posts

I have aform that is not updating the database. When filling out the form all seems to work but it is not updating the database. I am new to php and any help would be appreciated.

 

<?php

//require once
require_once("../config/config.php");
require_once("../config/user_authentication.php");
?>

<html>
    <head>
        <title>Edit Member Record</title>
    <style>
<!--
 .red {
     background-color: #FF0000;
 }
-->
</style>
<link rel="stylesheet" type="text/css" href="../../../scripts/maple_main.css">
    </head>
<body>
<a href="../index.php">
<img src="../../images/home.gif" width="850" height="100" border="0"></a>
<?

$i = $_GET;
$edit = $_POST[edit];

//Edit Record page
if ($_SESSION[logged_in]) {

if (!$edit) {
   $sql = "SELECT * FROM $mysql_table WHERE id = '$i' ";
   $result = mysql_query($sql, $connection) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
      $i = $row['id'];
      $firstName = $row['firstName'];
      $lastName = $row['lastName'];
      $address = $row['address'];
      $city = $row['city'];
      $state = $row['state'];
      $zip = $row['zip'];
      $eMail = $row['eMail'];
      $phone1 = $row['phone1'];
      $phone2 = $row['phone2'];
      $county = $row['county'];
      $dues = $row['dues'];
      $listing = $row['listing'];
      $producer_type = $row['producer_type'];
      $other = $row['other'];
      $business_name = $row['business_name'];
      $taps = $row['taps'];
      $member_status = $row['member_status'];
      $year_tap = $row['year_tap'];
      $tours_visitor = $row['tours_visitor'];
      $comments = $row['comments'];
      $website = $row['website'];
}

$state_array = array("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY");
    $state_count = count($state_array);
    $c = 0;
    $select = "select_";
    while ($c < $state_count) {
           if ($state == $state_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }

$dues_array = array("Dues of $25 (Single member)", "Dues of $30 (Two family or business members)");
    $dues_count = count($dues_array);
    $c = 0;
    $select = "dues_";
    while ($c < $dues_count) {
           if ($dues == $dues_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }
    
    $year_tap_array = array("2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018");
    $year_tap_count = count($year_tap_array);
    $c = 0;
    $select = "year_tap_";
    while ($c < $year_tap_count) {
           if ($year_tap == $year_tap_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }
    
$listing_array = array("yes", "no");
    $listing_count = count($listing_array);
    $c = 0;
    $select = "listing_";
    while ($c < $listing_count) {
           if ($listing == $listing_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }
    
$tours_visitor_array = array("yes", "no");
    $tours_visitor_count = count($listing_array);
    $c = 0;
    $select = "tours_visitor_";
    while ($c < $tours_visitor_count) {
           if ($tours_visitor == $tours_visitor_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }
    
$producer_type_array = array("Hobby Producer (Non Licensed)", "Commercial Producer (Licensed by MN Dept. of Ag)", "Other");
    $producer_type_count = count($producer_type_array);
    $c = 0;
    $select = "producer_type_";
    while ($c < $producer_type_count) {
           if ($producer_type == $producer_type_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }

$member_status_array = array("Active", "Non Active");
    $member_status_count = count($member_status_array);
    $c = 0;
    $select = "member_status_";
    while ($c < $member_status_count) {
           if ($member_status == $member_status_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }

    $hide = 1;
    include("user_form.php");
    
} else {

if ($taps != "" && $year_tap == "") {
        $required_array = array("firstName", "lastName", "address", "city", "state", "zip", "phone1", "county", "dues", "listing", "producer_type", "business_name", "member_status", "tours_visitor", "year_tap", "taps");
        
        } elseif ($taps == "" && $year_tap != "") {
        $required_array = array("firstName", "lastName", "address", "city", "state", "zip", "phone1", "county", "dues", "listing", "producer_type", "business_name", "member_status", "tours_visitor", "year_tap", "taps");
        
        } else {
$required_array = array("firstName", "lastName", "address", "city", "state", "zip", "phone1", "county", "dues", "listing", "producer_type", "business_name", "member_status", "tours_visitor");
        }

    $required_count = count($required_array);
    $c = 0;
    $error = "_error";
    while ($c < $required_count) {
    $$required_array[$c] = $_POST[$required_array[$c]];
    
       if ($$required_array[$c] == "") {
          ${$required_array[$c].$error} = "class = 'red'";
          $error_message .= "Please fill in the field $required_array[$c]<br>";
          $edit_record = "no";
       }
       $c++;
    }
    
    $state_array = array("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY");
    $state_count = count($state_array);
    $c = 0;
    $select = "select_";
    while ($c < $state_count) {
           if ($state == $state_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }

$dues_array = array("Dues of $25 (Single member)", "Dues of $30 (Two family or business members)");
    $dues_count = count($dues_array);
    $c = 0;
    $select = "dues_";
    while ($c < $dues_count) {
           if ($dues == $dues_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }
    
$year_tap_array = array("2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018");
    $year_tap_count = count($year_tap_array);
    $c = 0;
    $select = "year_tap_";
    while ($c < $year_tap_count) {
           if ($year_tap == $year_tap_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }
    
$listing_array = array("yes", "no");
    $listing_count = count($listing_array);
    $c = 0;
    $select = "listing_";
    while ($c < $listing_count) {
           if ($listing == $listing_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }
    
$tours_visitor_array = array("yes", "no");
    $tours_visitor_count = count($listing_array);
    $c = 0;
    $select = "tours_visitor_";
    while ($c < $tours_visitor_count) {
           if ($tours_visitor == $tours_visitor_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }
    
$producer_type_array = array("Hobby Producer (Non Licensed)", "Commercial Producer (Licensed by MN Dept. of Ag)", "Other");
    $producer_type_count = count($producer_type_array);
    $c = 0;
    $select = "producer_type_";
    while ($c < $producer_type_count) {
           if ($producer_type == $producer_type_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }

$member_status_array = array("Active", "Non Active");
    $member_status_count = count($member_status_array);
    $c = 0;
    $select = "member_status_";
    while ($c < $member_status_count) {
           if ($member_status == $member_status_array[$c]) {
             ${$select.$c} = "selected";
           }
           $c++;
    }

if (!$edit_record) {
//Mysql Insert and success message here
   $_POST[firstName] = addslashes($_POST[firstName]);
   $_POST[lastName] = addslashes($_POST[lastName]);
   $_POST[address] = addslashes($_POST[address]);   
   $_POST[city] = addslashes($_POST[city]);   
   $_POST[state] = addslashes($_POST[state]);
   $_POST[zip] = addslashes($_POST[zip]);
   $_POST = addslashes($_POST);   
   $_POST[phone1] = addslashes($_POST[phone1]);   
   $_POST[phone2] = addslashes($_POST[phone2]);   
   $_POST[county] = addslashes($_POST[county]);
   $_POST[dues] = addslashes($_POST[dues]);   
   $_POST[listing] = addslashes($_POST[listing]);    
   $_POST[producer_type] = addslashes($_POST[producer_type]);
   $_POST[other] = addslashes($_POST[other]);    
   $_POST[business_name] = addslashes($_POST[business_name]);   
   $_POST[taps] = addslashes($_POST[taps]);
   $_POST[member_status] = addslashes($_POST[member_status]);
   $_POST[year_tap] = addslashes($_POST[year_tap]);
   $_POST[tours_visitor] = addslashes($_POST[tours_visitor]);   
   $_POST[comments] = addslashes($_POST[comments]);
   $_POST[website] = addslashes($_POST[website]);   


   $sql = "UPDATE $mysql_table SET firstName = '$_POST[firstName]', lastName = '$_POST[lastName]', address = '$_POST[address]', city = '$_POST[city]', state = '$_POST[state]', zip = '$_POST[zip]', eMail = '$_POST', phone1 = '$_POST[phone1]', phone2 = '$_POST[phone2]', county = '$_POST[county]', dues = '$_POST[dues]', listing = '$_POST[listing]', producer_type = '$_POST[producer_type]', other = '$_POST[other]', business_name = '$_POST[business_name]', taps = '$_POST[taps]', member_status = '$_POST[member_status]', year_tap = '$_POST[year_tap]', tours_visitor = '$_POST[tours_visitor]', comments = '$_POST[comments]', website = '$_POST[website]' WHERE id = '$i'";
$result = mysql_query($sql, $connection) or die(mysql_error());

echo "<br>You have successfully edited member ". $_POST[firstName] ." ". $_POST[lastName] ." in the database.<br>
      <a href=\"../list.php\">Click here to go back to the member list</a> Or <a href=\"../search.php\">Search Members</a><br>
      <a href=\"../index.php\">Home</a>";
 
 } else {

    $hide = 1;
    include("user_form.php");
 
  }
  $edit = $_POST[edit];
}

} else {
    echo "<h1><font face=\"Arial, Helvetica, sans-serif\" color=\"#006600\">You are not authorized to be here</font></h1>
    <a href=\"\">Home</a>";
}

?>
</body>
</html>

 

 

Link to comment
Share on other sites

any help would be appreciated.

General advice:

1) Don't post your entire page unless asked for it, people don't want to wade through a bunch of code. Only post the problematic area.

2) Use


tags if you want people to look at your code.

 

Regarding problem:

1) Do you get any error message back from mysql_error()?

2) echo $sql and try running the query manually in phpMyAdmin or similar.

3) Make sure your WHERE condition matches rows by using a SELECT query with the same clause.

Link to comment
Share on other sites

Thanks kicken and Psycho for your response. Sorry about posting the whole page but i had spent hours looking at the page and had no idea where the problem was. I don't think i am getting any errors from the database because I made a slight change in the code after receiving your reply and got a database error then. There is an add_user page that uses the same configuration and authentication file and the add_user works. I am not a programer and was asked to try and figure this out so I am not quite sure how to do what you have suggested.

Link to comment
Share on other sites

have you checked if the $edit_record variable in the following statement is a true value so that the update logic is even running -

if (!$edit_record) {

    the update query logic is here ...

}

where in your code is the $edit_record variable being set to indicate that the update query logic should be executed? the only occurrence of $edit_record in the code is setting it to the string "no", but the string "no" is a true value and the update query logic would run.

 

where did you find this code at?

Edited by mac_gyver
Link to comment
Share on other sites

It is on a website that I volunteered to manage for a non profit. Not sure who wrote the code but the person that was managing the website quit and I am not a programer just trying to fix the many problems with this site until I get a new one running for them. Should $edit_record be set "yes" or how do I set the variable? Sorry I know this is basic programing.

Link to comment
Share on other sites

generally, edit means to read, modify, update something, so the intent is probably for $edit_record to indicate that an update should be performed. however, i just looked closer at that code and it is using !(NOT)$edit_record to run the update logic. this would require that $edit_record evaluate to a false value (empty, not set, or a zero...) in order to run the update logic.

 

so the question still is -

 

have you checked if the $edit_record variable in the following statement is a true value so that the update logic is even running -

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.