The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
HELP - Updating Database through TEXT Boxes
Discuss HELP - Updating Database through TEXT Boxes in the PHP Development forum on Dev Shed. HELP - Updating Database through TEXT Boxes PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 17th, 2013, 02:15 PM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 19
Time spent in forums: 3 h 41 m 54 sec
Reputation 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>
|

March 17th, 2013, 03:47 PM
|
|
|
|
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.
|

March 17th, 2013, 04:30 PM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 19
Time spent in forums: 3 h 41 m 54 sec
Reputation Power: 0
|
|
Quote: | 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.
|

March 17th, 2013, 04:44 PM
|
|
|
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.
|

March 17th, 2013, 05:31 PM
|
|
Contributing User
|
|
Join Date: Aug 2011
Location: Sydney Australia
|
|
Quote: | 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.
|

March 17th, 2013, 05:35 PM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 19
Time spent in forums: 3 h 41 m 54 sec
Reputation Power: 0
|
|
Quote: | 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.
|

March 17th, 2013, 05:37 PM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 19
Time spent in forums: 3 h 41 m 54 sec
Reputation Power: 0
|
|
Quote: | 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...
|

March 17th, 2013, 05:44 PM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 19
Time spent in forums: 3 h 41 m 54 sec
Reputation 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?
|

March 17th, 2013, 05:46 PM
|
|
|
|
How do you identify the rows in your database for each property?
|

March 17th, 2013, 06:06 PM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 19
Time spent in forums: 3 h 41 m 54 sec
Reputation Power: 0
|
|
Quote: | 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?
|

March 17th, 2013, 09:32 PM
|
|
Contributing User
|
|
Join Date: Aug 2011
Location: Sydney Australia
|
|
Quote: | 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.
|

March 18th, 2013, 01:12 PM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 19
Time spent in forums: 3 h 41 m 54 sec
Reputation 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]'");
}
|

March 18th, 2013, 01:58 PM
|
|
|
|
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.
|

March 18th, 2013, 04:08 PM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 19
Time spent in forums: 3 h 41 m 54 sec
Reputation Power: 0
|
|
Quote: | 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.
|

March 18th, 2013, 06:13 PM
|
|
Contributing User
|
|
Join Date: Aug 2011
Location: Sydney Australia
|
|
Quote: | 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
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|