Results 1 to 5 of 5

Thread: Passing a value, then updating the DB

  1. #1

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Passing a value, then updating the DB

    Hey everyone.
    Thanks for the help on my other problems.

    I just cant seem to figure this one out.

    What I am trying to do is...
    This form gets passed a pasc_id value, I have a query where it takes that value, searched the DB, and displays the results in text box's so the user can change them, then hit "Update" and it will udpate those values in the DB.

    For some reason i cant get this to work

    I was trying to make it query the DB, then put the values of the query into selected text boxes, then if they hit "update" it would update the db, based on the pasc_id(they cant change that)

    The form displays the first query where i get the pasc_id and its values.
    Here is the error i get:


    PHP Code:
    <?php
    //adding header.php
    $page_title "Update Part Information";
    include(
    '../php/header.php');
    //connection to database
    include('../db_conn/mysql_conn.php');
    ?>

    <table cellspacing="2" cellpadding="2" border="0" width="900" height="50">
    <tr>
        <td rowspan="2">
        <fieldset>
        <table cellspacing="2" cellpadding="2" border="0">
    <tr>
        <td bgcolor="#c0c0c0" width="100"><b>PASC ID</b></td>
        <td bgcolor="#c0c0c0" width="100"><b>Part Number</b></td>
        <td bgcolor="#c0c0c0" width="150"><b>PO #</b></td>
        <td bgcolor="#c0c0c0" width="150"><b>Date Entered</b></td>
        <td bgcolor="#c0c0c0" width="150"><b>Part Notes</b></td>
    </tr>
    <tr>
    <?php
    $pasc_id 
    $_REQUEST['pasc_id'];
    //Start Form 
    echo '<form action="Update_Part.php" method="post">';
    ?>

    <?   
      $request = "SELECT DISTINCT pasc_id,part_number,return_po,date_entered,part_notes FROM dss_returns where pasc_id = '$pasc_id'";  
      $query = mysql_query($request);  
      $num = mysql_num_rows($query);  
    while($data = mysql_fetch_array($query)){   

    echo '<td valign="top"><a href="./Update_Part.php?pasc_id=' . $data['pasc_id'] . '">' .'   ' . $data['pasc_id'] . '</a>'. '</td>';
    //echo '<form action="Update_Part.php" method="post">';

    echo '<td valign="top"><input align="top" type="text" name="partnumber" size="20" maxlenght="20" value="'.  $data['part_number']  .'"/>' . '</td>';  
    echo '<td valign="top"><input align="top" type="text" name="return_po" size="20" maxlenght="20" value="'.  $data['return_po']  .'"/>' . '</td>';
    echo '<td valign="top"><input align="top" type="text" name="date_entered" size="20" maxlenght="20" value="'.  $data['date_entered']  .'"/>' . '</td>';
    echo '<td valign="top"><textarea name="partcomments" rows="6" cols="40">';
    echo $data['part_notes'];
    echo '</textarea> </td> </tr>';
    echo '<td></td><td></td><td></td><td></td><td align="right"><input type="submit" name="submit" value="Update Record" align="right"/></form>';

    ?>
    </table>

        </fieldset>
        
    <?php
    if(isset($_POST['submitted'])){$errors = array();
    }
    if(empty(
    $_POST['partnumber'])){$errors[] = 'Part Number';
    }else{
    $partnumber trim($_POST['partnumber']);
    }
    if(empty(
    $_POST['return_po'])){$errors[] = 'Return PO';
    }else{
    $return_po trim($_POST['return_po']);
    }
    if(empty(
    $_POST['date_entered'])){$errors[] = 'Date Entered';
    }else{
    $date_entered trim($_POST['date_entered']);
    }
    if(empty(
    $_POST['partcomments'])){$errors[] = 'Part Comments';
    }else{
    $partcomments trim($_POST['partcomments']);
    }

    //below is line 66
    $request "UPDATE dss_returns (part_number,return_po,date_entered,part_notes) VALUES ('$partnumber','$return_po','$date_entered','$partcomments') WHERE pasc_id = '$pasc_id'";  
    $query mysql_query($request);  
    $num mysql_num_rows($query);
    ?>

        </td>
    </tr>
    <tr>
    </tr>
    </table>

    <?php 
    //adding footer.php
    include('../php/footer.php');
    ?>

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Passing a value, then updating the DB

    uh.. you're doing an update query like you're trying to insert something.

    UPDATE syntax is as follows:
    UPDATE table_name SET field='value', field2='value2' WHERE field='value' LIMIT n

    other than that, some advice: you should not be doing what you're doing anyhow. you should have the actual update part after they've submitted either on a page that doesn't even display their information at all, or you should be updating that information BEFORE you're displaying it. If you display it and then update it afterward, the user will be confused as to why he just submitted a form and the things he submitted did not change on his actual form. It's logical to update, then display, so that results are displayed immediately. In my opinion, you're also handling the entire POST request wrong, which is most likely the cause of all your unknown variables. You're checking empty() with a variable that doesn't even exist; you should be making sure it does exist (isset()) beforehand. also, you shouldn't use a posted variable (yours was "submitted"), instead you should check the request method, it's much easier. one last thing, you should have an entire post session enclosed in an if statement, a structure like this works well:
    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == "POST"){
      
    //do your error checking now
      //--> check if each variable you want to use exists using isset(), and if it doesn't, make an error
      
    $errors = array(); //the error variable
      
    if(isset($_POST['partnumber']) && $_POST['partnumber'] != '')
        
    $partnumber $_POST['partnumber'];
      else
        
    $errors[] = 'part number';
      
    //you don't have to use != '', you can do is_numeric, !empty, etc

      //now, we won't do ANYTHING unless there are no errors
      
    if(count($errors) == 0){
        
    //submit your stuff
        
    mysql_query("UPDATE dss_returns SET field='value', field2='value2' WHERE some_unique_ID='value'") or die(mysql_error());
        echo 
    'Your information has been successfully updated.' "\n\n";
      }else{
        
    //there were errors, spit them all out
        
    echo 'The following fields were left blank:' "\n\n";
        foreach(
    $error as $errors){
          echo 
    $error "\n";
        }
        echo 
    "\n" 'Please fill out the form again';
      }
    }
    //now that the post request has been handled (if there was one in the first place), you can just display all the customer's updated information 
    Last edited by kows; Nov 13th, 2006 at 12:34 PM.

  3. #3

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Passing a value, then updating the DB

    Hi Kows, thanks for all your help.

    What I wanted to do, is display the data first, so they can review it and look at it, and make the changes, then hit "update" and it takes those values from thos box's and update the record in the db that corasponds to its pasc_id.

    How do you keep the pasc_id in scope from page to page?

    Here i what i did:


    PHP Code:
    <?php
    //adding header.php
    $page_title "Update Part Information";
    include(
    '../php/header.php');
    //connection to database
    include('../db_conn/mysql_conn.php');
    ?>

    <table cellspacing="2" cellpadding="2" border="0" width="900" height="50">
    <tr>
        <td rowspan="2">
        <fieldset>
        <table cellspacing="2" cellpadding="2" border="0">
    <tr>
        <td bgcolor="#c0c0c0" width="100"><b>PASC ID</b></td>
        <td bgcolor="#c0c0c0" width="100"><b>Part Number</b></td>
        <td bgcolor="#c0c0c0" width="150"><b>PO #</b></td>
        <td bgcolor="#c0c0c0" width="150"><b>Date Entered</b></td>
        <td bgcolor="#c0c0c0" width="150"><b>Part Notes</b></td>
    </tr>
    <tr>
    <?php
    $pasc_id 
    $_REQUEST['pasc_id'];
    //Start Form 
    echo '<form action="Update_Part.php" method="post">';
    ?>

    <?   
      $request = "SELECT DISTINCT pasc_id,part_number,return_po,date_entered,part_notes FROM dss_returns where pasc_id = '$pasc_id'";  
      $query = mysql_query($request);  
      $num = mysql_num_rows($query);  
    while($data = mysql_fetch_array($query)){   

    echo '<td valign="top"><a href="./Update_Part.php?pasc_id=' . $data['pasc_id'] . '">' .'   ' . $data['pasc_id'] . '</a>'. '</td>';
    echo '<td valign="top"><input align="top" type="text" name="partnumber" size="20" maxlenght="20" value="'.  $data['part_number']  .'"/>' . '</td>';  
    echo '<td valign="top"><input align="top" type="text" name="return_po" size="20" maxlenght="20" value="'.  $data['return_po']  .'"/>' . '</td>';
    echo '<td valign="top"><input align="top" type="text" name="date_entered" size="20" maxlenght="20" value="'.  $data['date_entered']  .'"/>' . '</td>';
    echo '<td valign="top"><textarea name="part_notes" rows="6" cols="40">';
    echo $data['part_notes'];
    echo '</textarea> </td> </tr>';
    echo '<td></td><td></td><td></td><td></td><td align="right"><input type="submit" name="submit" value="Update Record" align="right"/></form>';

    ?>
    </table>

        </fieldset>
        
    <?php
    if($_SERVER['REQUEST_METHOD'] == "POST"){ 
      
    //do your error checking now 
      //--> check if each variable you want to use exists using isset(), and if it doesn't, make an error 
      
    $errors = array(); //the error variable 
      
      
    if(isset($_POST['partnumber']) && $_POST['partnumber'] != ''
        
    $partnumber $_POST['partnumber']; 
        
    $return_po $_POST['return_po'];
        
    $date_entered $_POST['date_entered'];
        
    $part_notes $_POST['part_notes'];

      
    //you don't have to use != '', you can do is_numeric, !empty, etc 

      //now, we won't do ANYTHING unless there are no errors 
      
    if(count($errors) == 0){ 
        
    //submit your stuff 
        
    mysql_query("UPDATE dss_returns SET part_number='$partnumber' WHERE pasc_id='$pasc_id'") or die(mysql_error()); 
        echo 
    'Your information has been successfully updated.' "\n\n"
      }else{ 
        
    //there were errors, spit them all out 
        
    echo 'The following fields were left blank:' "\n\n"
        foreach(
    $error as $errors){ 
          echo 
    $error "\n"
        } 
        echo 
    "\n" 'Please fill out the form again'
      } 

    //now that the post request has been handled (if there was one in the first place), you can just display all the customer's updated information 
    ?>
    </td></tr><tr></tr></table>
    <?php 
    //adding footer.php
    include('../php/footer.php');
    ?>
    Last edited by joefox; Nov 13th, 2006 at 04:02 PM.

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Passing a value, then updating the DB

    looking at what you added in my code, you didn't do any addition error checking... you actually removed the part that raised errors for some reason. you should be checking every value, and if it isn't a value you want, then you pop up an error, like this:
    PHP Code:
      if(isset($_POST['partnumber']) && $_POST['partnumber'] != ''
        
    $partnumber $_POST['partnumber']; 
      else
        
    $errors[] = 'part number';

      if(isset(
    $_POST['return_po']) && $_POST['return_po'] != '')
        
    $return_po $_POST['return_po']; 
      else
        
    $errors[] = 'return po';

      
    //etc 
    anyway, more about why what you're doing isn't working: you are not passing a form variable that has the name "pasc_id." You should use a hidden input to assign that variable.
    Code:
    <input type="hidden" name="pasc_id" value="<?php echo $data['pasc_id']; ?>" />
    also, I don't know why you have a while loop when you only have one record.. but if you are going to make a form like you are, you either need to set it up using arrays so that you know which button was submitted, OR you need to create a new <form> every time your query is ran (ie: INSIDE the while loop). also, you should be building your table rows within the while loop as well as the TR's, if you're requesting more than one record.. otherwise, you will end up with a table that has like 10 cells across and is difficult to work with. if you do not expect more than one result from your query, you should not even be using a while() loop in the first place to loop through it.

    I'm in a hurry so I mostly scribbled that all out, but I hope that made sense to you.

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Passing a value, then updating the DB

    Quote Originally Posted by joefox
    Hey everyone.
    Thanks for the help on my other problems.

    I just cant seem to figure this one out.

    What I am trying to do is...
    This form gets passed a pasc_id value, I have a query where it takes that value, searched the DB, and displays the results in text box's so the user can change them, then hit "Update" and it will udpate those values in the DB.

    For some reason i cant get this to work

    I was trying to make it query the DB, then put the values of the query into selected text boxes, then if they hit "update" it would update the db, based on the pasc_id(they cant change that)

    The form displays the first query where i get the pasc_id and its values.
    Here is the error i get:


    PHP Code:
    <?php
    //adding header.php
    $page_title "Update Part Information";
    include(
    '../php/header.php');
    //connection to database
    include('../db_conn/mysql_conn.php');
    ?>

    <table cellspacing="2" cellpadding="2" border="0" width="900" height="50">
    <tr>
        <td rowspan="2">
        <fieldset>
        <table cellspacing="2" cellpadding="2" border="0">
    <tr>
        <td bgcolor="#c0c0c0" width="100"><b>PASC ID</b></td>
        <td bgcolor="#c0c0c0" width="100"><b>Part Number</b></td>
        <td bgcolor="#c0c0c0" width="150"><b>PO #</b></td>
        <td bgcolor="#c0c0c0" width="150"><b>Date Entered</b></td>
        <td bgcolor="#c0c0c0" width="150"><b>Part Notes</b></td>
    </tr>
    <tr>
    <?php
    $pasc_id 
    $_REQUEST['pasc_id'];
    //Start Form 
    echo '<form action="Update_Part.php" method="post">';
    ?>

    <?   
      $request = "SELECT DISTINCT pasc_id,part_number,return_po,date_entered,part_notes FROM dss_returns where pasc_id = '$pasc_id'";  
      $query = mysql_query($request);  
      $num = mysql_num_rows($query);  
    while($data = mysql_fetch_array($query)){   

    echo '<td valign="top"><a href="./Update_Part.php?pasc_id=' . $data['pasc_id'] . '">' .'   ' . $data['pasc_id'] . '</a>'. '</td>';
    //echo '<form action="Update_Part.php" method="post">';

    echo '<td valign="top"><input align="top" type="text" name="partnumber" size="20" maxlenght="20" value="'.  $data['part_number']  .'"/>' . '</td>';  
    echo '<td valign="top"><input align="top" type="text" name="return_po" size="20" maxlenght="20" value="'.  $data['return_po']  .'"/>' . '</td>';
    echo '<td valign="top"><input align="top" type="text" name="date_entered" size="20" maxlenght="20" value="'.  $data['date_entered']  .'"/>' . '</td>';
    echo '<td valign="top"><textarea name="partcomments" rows="6" cols="40">';
    echo $data['part_notes'];
    echo '</textarea> </td> </tr>';
    echo '<td></td><td></td><td></td><td></td><td align="right"><input type="submit" name="submit" value="Update Record" align="right"/></form>';

    ?>
    </table>

        </fieldset>
        
    <?php
    if(isset($_POST['submitted'])){$errors = array();
    }
    if(empty(
    $_POST['partnumber'])){$errors[] = 'Part Number';
    }else{
    $partnumber trim($_POST['partnumber']);
    }
    if(empty(
    $_POST['return_po'])){$errors[] = 'Return PO';
    }else{
    $return_po trim($_POST['return_po']);
    }
    if(empty(
    $_POST['date_entered'])){$errors[] = 'Date Entered';
    }else{
    $date_entered trim($_POST['date_entered']);
    }
    if(empty(
    $_POST['partcomments'])){$errors[] = 'Part Comments';
    }else{
    $partcomments trim($_POST['partcomments']);
    }

    //below is line 66
    $request "UPDATE dss_returns (part_number,return_po,date_entered,part_notes) VALUES ('$partnumber','$return_po','$date_entered','$partcomments') WHERE pasc_id = '$pasc_id'";  
    $query mysql_query($request);  
    $num mysql_num_rows($query);
    ?>

        </td>
    </tr>
    <tr>
    </tr>
    </table>

    <?php 
    //adding footer.php
    include('../php/footer.php');
    ?>
    http://www.vbforums.com/showpost.php...49&postcount=7

Posting Permissions

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



Click Here to Expand Forum to Full Width