Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    HELP - Updating Database through TEXT Boxes




    the user will change the price here (text boxes) hit update and it will update all rows (no javascript please - we can just update all rows).

    Need help on Process Page code. or ideas on how to go about it.


    ---- code on current page ----
    PHP Code:

     $result2 = mysql_query("SELECT * 
                              FROM available
                              JOIN land
                              ON available.building = land.building
                              WHERE land.landlord = '$landlordselect'
                              ORDER by available.price ASC");
                             
    echo "<table border='1'>
    <tr>
    <th>Building</th>
    <th>Apartment #</th>
    <th>Price</th>
    <th># of Bedrooms</th>
    <th>Move in</th> 
    <th>Rented</th>
    </tr>";
    while($row = mysql_fetch_array($result2))
      {
      echo "<tr>";
      echo "<td>" . $row['building'] . "</td>";
      echo "<td>" . $row['apt'] . "</td>";
      ?>
      <form action="processpage.php" method="post">
       <td><input name="uprice[]" type="text" size="5" value="<?php echo( htmlspecialchars$row['price'] ) ); ?>" /> </td>
      <?php
      
    echo "<td>" $row['bedroom'] . "</td>";
      echo 
    "<td>" $row['movein'] . "</td>"?>
      <td><input type="checkbox" name="rent[]" value="yes"></td>
      <?php
      
    echo "</tr>";
      }
    echo 
    "</table>";
    ?>
       <p><input type="submit" value="Update"><p>
      </form>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,470
    Rep Power
    594
    First enclose your code in [ PHP ] tags. See the sticky at the top of this forum.

    Second, DO NOT use the deprecated MySQL extensions. Switch to PDO and prepared statements.

    Third, you don't show any code where you try to update the database. I am assuming that is where you are stuck. When processpage.php is invoked, $_POST will contain the form fields. Use that to update your database.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0
    Originally Posted by gw1500se
    First enclose your code in [ PHP ] tags. See the sticky at the top of this forum.

    Second, DO NOT use the deprecated MySQL extensions. Switch to PDO and prepared statements.

    Third, you don't show any code where you try to update the database. I am assuming that is where you are stuck. When processpage.php is invoked, $_POST will contain the form fields. Use that to update your database.
    exactly where i'm stuck.

    I'm stuck at the logistical part I want it to go and through and update each price box. to the corresponding apartment.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,470
    Rep Power
    594
    You set the arrays from $_POST and process them accordingly:
    PHP Code:
    $uprice=$_POST['urpice'];
    $rent=$_POST['rent']; 
    Then '$uprice' and '$rent' will be arrays containing the data from the form. You can use a 'foreach' or just a 'for' to process them.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    183
    Rep Power
    84
    Originally Posted by Loren646
    $result2 = mysql_query("SELECT *
    FROM available
    JOIN land
    ON available.building = land.building
    WHERE land.landlord = '$landlordselect'
    ORDER by available.price ASC");
    Your code has no identity for which rows of your database you want to update.
    You'll need to pull say a property_id for each row, and put it in the table in say a <hidden> input type. The <hidden> inputs will need to be inside the <form> </form> data so that it will be posted for processing.
    Then the processing script can step through the arrays of property_id, and price and update the database as appropriate.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0
    Originally Posted by gw1500se
    You set the arrays from $_POST and process them accordingly:
    PHP Code:
    $uprice=$_POST['urpice'];
    $rent=$_POST['rent']; 
    Then '$uprice' and '$rent' will be arrays containing the data from the form. You can use a 'foreach' or just a 'for' to process them.
    exactly. i understand that. I just don't understand how i tell the table to update that specific row.

    maybe i can a order by price ASC again and then have it loop each one and update each row.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0
    Originally Posted by BarryG
    Your code has no identity for which rows of your database you want to update.
    You'll need to pull say a property_id for each row, and put it in the table in say a <hidden> input type. The <hidden> inputs will need to be inside the <form> </form> data so that it will be posted for processing.
    Then the processing script can step through the arrays of property_id, and price and update the database as appropriate.
    hmm interesting... i'll try to wrap my head around this... still a bit confusing...
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0
    What about this...

    I have a check box next to each row. The value will be saved as the Primary ID. Then on the update processing page it will search WHERE = PRIMARY ID and update that row?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,470
    Rep Power
    594
    How do you identify the rows in your database for each property?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0
    Originally Posted by gw1500se
    How do you identify the rows in your database for each property?
    auto incrementing primary id? now i have to figure out a way to to associate the new price with the primary id.

    maybe a loop?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    183
    Rep Power
    84
    Originally Posted by Loren646
    now i have to figure out a way to to associate the new price with the primary id.

    maybe a loop?
    Your lazy SELECT * will be retrieving the auto_increment id of the property. Say for argument's sake, it's building_id.
    Write that to the row in the table like this.
    PHP Code:
    <?php
    while ($row mysql_fetch_array($result2)){
    echo 
    "<form action=\"processpage.php\" method=\"post\">";
    echo 
    "<tr>";
    echo 
    "<td><input type=\"hidden\" value=\"{$row['building_id']}\" name=\"building_id[]\">$row['building']</td>";
    echo 
    "<td>{$row['apt']}</td>";
    echo 
    "<td><input name=\"uprice[]\" type=\"text\" size=\"5\" value=\""htmlspecialchars($row['price'] ) ."\" /> </td>";
    echo 
    "<td>{$row['bedroom']}</td>";
    echo 
    "<td>{$row['movein']}</td>"
    echo 
    "<td><input type=\"checkbox\" name=\"rent[]\" value=\"yes\"></td>";
    echo 
    "</tr>";
    }
    echo 
    "</table>";
    ?>
    <p><input type="submit" value="Update"><p>
    </form>
    In your processing script, you'll get an array of building_id's in $_POST['building_id'] that will be the id of the row you need to update with the data from the corresponding $_POST['uprice'] array.
    IOW, the first building_id will use the first uprice, the second building _id will use the second uprice ......

    Get it?

    You also need to be aware that the "rent" checkbox will only be present if it is checked. If it's not checked, it's not sent.
    Last edited by BarryG; March 17th, 2013 at 09:37 PM.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0
    BarryG. Works perfect! thanks for the help.

    What are your thoughts on the check marks? so it might be an array of only 2 items when there are 5 items. I think it'll put the information in the wrong column. I could change the checkboxes to text boxes and just have it say 0 or 1 if i must.


    So this is what i have now.

    Update page:
    PHP Code:
    $countadd=0;
    while($row = mysql_fetch_array($result2))
      {

      echo "<tr>";
      echo "<td>" . $row['building'] . "</td>";
      ?>
      <form action="updateprocess.php" method="post">
       <td><input name="uapt[]" type="text" size="5" value="<?php echo( htmlspecialchars$row['apt'] ) ); ?>" /> </td>
       <td><input name="uprice[]" type="text" size="5" value="<?php echo( htmlspecialchars$row['price'] ) ); ?>" /> </td>
       <td><input name="ubedroom[]" type="text" size="5" value="<?php echo( htmlspecialchars$row['bedroom'] ) ); ?>" /> </td>
       <td><input name="umovein[]" type="text" size="25" value="<?php echo( htmlspecialchars$row['movein'] ) ); ?>" /> </td>
       <td><input type="checkbox" name="urent[]" value="yes"></td>
       <td><input type="checkbox" name="uupdate[]" value="<?php echo $row['prim']; ?>"></td>
       <td><input type="hidden" value="<?php echo $row['prim']; ?>" name="prim_id[]"><?php $row['prim'?></td>
       <td><input type="hidden" value="<?php echo $countadd ?>" name="countadd"><?php $countadd ?></td>
       <?php $countadd++; ?>
       
      <?php
      
    echo "</tr>";
      }
    echo 
    "</table>";
    ?>
       <p><input type="submit" value="Update"><p>
      </form>
    Process page
    PHP Code:
    $rent=$_POST['urent'];
    $update=$_POST['uupdate'];
    $apt=$_POST['uapt'];
    $price=$_POST['uprice'];
    $bedroom=$_POST['ubedroom'];
    $movein=$_POST['umovein'];
    $prim_id=$_POST['prim_id'];
    $count=$_POST['countadd'];

      
        if (!
    get_magic_quotes_gpc())
      {
        
    $apt addslashes($apt);
        
    $price doubleval($price);
        
    $bedroom addslashes($bedroom);
        
    $movein addslashes($movein);
      }


    $conn mysql_connect("localhost","user","password");
    mysql_select_db("listing");

    for (
    $i2 0$i2 <= $count$i2++) {
    mysql_query("UPDATE available
            SET apt='
    $apt[$i2]', price='$price[$i2]', bedroom='$bedroom[$i2]', movein='$movein[$i2]'
            WHERE prim='
    $prim_id[$i2]'");

  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,470
    Rep Power
    594
    As requested in the beginning wrap your code in [ PHP ] tags. It is too hard to read unformatted. See the sticky at the top of this forum. Edit your previous post to fix it.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0
    Originally Posted by gw1500se
    As requested in the beginning wrap your code in [ PHP ] tags. It is too hard to read unformatted. See the sticky at the top of this forum. Edit your previous post to fix it.
    sorry about that. done.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    183
    Rep Power
    84
    Originally Posted by Loren646
    BarryG. Works perfect! thanks for the help.

    What are your thoughts on the check marks?
    You could use the building_id as the index of your array of building_id, price and rent instead of letting php assign them as 0,1,2 ....

    Then if the rent checkbox with the building id index exists, then the box was checked.
    PHP Code:
    <?php
    while ($row mysql_fetch_array($result2)){
    $b_id $row['building_id'];
    echo 
    "<form action=\"processpage.php\" method=\"post\">";
    echo 
    "<tr>";
    echo 
    "<td><input type=\"hidden\" value=\"$b_id\" name=\"building_id[$b_id]\">$row['building']</td>";
    echo 
    "<td>{$row['apt']}</td>";
    echo 
    "<td><input name=\"uprice[$b_id]\" type=\"text\" size=\"5\" value=\""htmlspecialchars($row['price'] ) ."\" /> </td>";
    echo 
    "<td>{$row['bedroom']}</td>";
    echo 
    "<td>{$row['movein']}</td>"
    echo 
    "<td><input type=\"checkbox\" name=\"rent[$b_id]\" value=\"yes\"></td>";
    echo 
    "</tr>";
    }
    echo 
    "</table>";
    ?>
    <p><input type="submit" value="Update"><p>
    </form>
    You get the value and key of each element with a
    PHP Code:
    foreach ($_POST['building_id'] as $building => $b_id) {
       
    // $b_id is the index for the $_POST['price'][$b_id] and the rent checkbox
    // do stuff with each element

Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo