Current location: Hot Scripts Forums » General Community » Script Requests » need php example


need php example

Reply
  #1 (permalink)  
Old 07-30-10, 10:01 PM
ckdoublenecks ckdoublenecks is offline
Wannabe Coder
 
Join Date: Jul 2010
Posts: 123
Thanks: 11
Thanked 0 Times in 0 Posts
need php example

I'm trying to read from a database, add fields together and write fields back to the database. My program currently echos the table fine. I just need a little example.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-31-10, 10:35 AM
bizzar528's Avatar
bizzar528 bizzar528 is offline
Community Liaison
 
Join Date: Sep 2004
Location: Pennsylvania, US
Posts: 1,551
Thanks: 2
Thanked 16 Times in 15 Posts
Showing us what you have so far would help...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 07-31-10, 04:17 PM
ckdoublenecks ckdoublenecks is offline
Wannabe Coder
 
Join Date: Jul 2010
Posts: 123
Thanks: 11
Thanked 0 Times in 0 Posts
The below program displays the record selected but I need to know how to use that table to update fields in it? In other words if the table that is displayed was input fields and I could then update the database?

<form method="post" action="selectapt.php">
<br/>
<input type="varchar" name="apt"/><br/><br/>
<input type="submit" name="submit" value="Select Apartment for Payment"/> </form>

<?php
$apt = $_POST['apt'];

$username="root";
$password="";
$database="prerentdb";

mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM payments Where apt='$apt'";
$result=mysql_query($query);
$num=mysql_numrows($result);

echo "<b><center>Prerent Report</center></b><br><br>";
echo "<table border='1'>
<tr>
<th>dep</th>
<th>apt</th>
<th>tenant</th>
<th>month</th>
<th>prev</th>
<th>misc</th>
<th>rent</th>
<th>hud</th>
<th>tenant</th>
<th>amt</th>
<th>date</th>
<tr>
<th colspan=2>num</th>
<th>name</th>
<th>chg</th>
<th>bal</th>
<th>amt</th>
<th>due</th>
<th colspan=2>pay</th>
<th colspan=2>paid</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['dep'] . "</td>";
echo "<td>" . $row['apt'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['rent'] . "</td>";
echo "<td>" . $row['prevbal'] . "</td>";
echo "<td>" . $row['misc'] . "</td>";
echo "<td>" . $row['rentdue'] . "</td>";
echo "<td>" . $row['hudpay'] . "</td>";
echo "<td>" . $row['tentpay'] . "</td>";
echo "<td>" . $row['amtpaid'] . "</td>";
echo "<td>" . $row['datepaid'] . "</td>";

echo "</tr>";
}
echo "</table>";

mysql_close();

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 08-01-10, 12:03 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Maybe something like this:
PHP Code:

<?php
$host
="localhost";
$username="root";
$password="";
$database="prerentdb";

mysql_connect($host,$username,$password);
mysql_select_db($database) or die( "Unable to select database");

if(!empty(
$_POST["submit"]))
{
 
$apt $_POST['apt'];
 
$query="SELECT * FROM payments Where apt='$apt'";
 
$result=mysql_query($query);
 if(
mysql_num_rows($result))
 {
  echo 
"<form action='#' method='post'><b><center>Present Report</center></b><br /><br />
         <table border='1'>
          <tr>
           <th>dep</th>
           <th>apt</th>
           <th>tenant</th>
           <th>month</th>
           <th>prev</th>
           <th>misc</th>
           <th>rent</th>
           <th>hud</th>
           <th>tenant</th>
           <th>amt</th>
           <th>date</th>
          </tr>
          <tr>
           <th colspan=2>num</th>
           <th>name</th>
           <th>chg</th>
           <th>bal</th>
           <th>amt</th>
           <th>due</th>
           <th colspan=2>pay</th>
           <th colspan=2>paid</th>
          </tr>"
;

  while(
$row mysql_fetch_assoc($result))
  {
   echo 
"<tr>
          <td><input type='text' name='dep' value='" 
$row['dep'] . "'></td>
          <td><input type='text' name='apt' value='" 
$row['apt'] . "' readonly style='border:none;' onmouseup='this.blur()'></td>
          <td><input type='text' name='name' value='" 
$row['name'] . "'></td>
          <td><input type='text' name='rent' value='" 
$row['rent'] . "'></td>
          <td><input type='text' name='prevbal' value='" 
$row['prevbal'] . "'></td>
          <td><input type='text' name='misc' value='" 
$row['misc'] . "'></td>
          <td><input type='text' name='rentdue' value='" 
$row['rentdue'] . "'></td>
          <td><input type='text' name='hudpay' value='" 
$row['hudpay'] . "'></td>
          <td><input type='text' name='tentpay' value='" 
$row['tentpay'] . "'></td>
          <td><input type='text' name='amtpaid' value='" 
$row['amtpaid'] . "'></td>
          <td><input type='text' name='datepaid' value='" 
$row['datepaid'] . "'></td>
         </tr>
         <tr>
          <td colspan='11' align='center' style='padding-top:20px;'><input type='submit' name='update' value='Update Record'></td>
         </tr>"
;
   }
  echo 
"</table>
        </form>"
;
  }
 else{echo 
"No listing for appartment $apt.<br />Please select another.<br />";}
 }
if(!empty(
$_POST["update"]))
{
 
$sql "UPDATE payments SET dep='".mysql_real_escape_string($_POST["dep"])."',apt='".mysql_real_escape_string($_POST["apt"])."',name='".mysql_real_escape_string($_POST["name"])."',rent='".mysql_real_escape_string($_POST["rent"])."',prevbal='".mysql_real_escape_string($_POST["prevbal"])."',misc='".mysql_real_escape_string($_POST["misc"])."',rentdue='".mysql_real_escape_string($_POST["rentdue"])."',hudpay='".mysql_real_escape_string($_POST["hudpay"])."',tentpay='".mysql_real_escape_string($_POST["tentpay"])."',amtpaid='".mysql_real_escape_string($_POST["amtpaid"])."',datepaid='".mysql_real_escape_string($_POST["datepaid"])."'";
 
mysql_query($sql) or die("Update query failed.");
 echo 
"Record for appartment ".$_POST["apt"]." has been updated ...";
 }
?>
<form method="post" action="#">
 <br />
 <input type="text" name="apt"/>
 <p>
<input type="submit" name="submit" value="Select Apartment for Payment"/>
</form>
__________________
Jerry Broughton

Last edited by job0107; 08-01-10 at 12:19 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to job0107 For This Useful Post:
ckdoublenecks (08-01-10)
  #5 (permalink)  
Old 08-01-10, 11:53 AM
ckdoublenecks ckdoublenecks is offline
Wannabe Coder
 
Join Date: Jul 2010
Posts: 123
Thanks: 11
Thanked 0 Times in 0 Posts
Now that's what I'm talking about. Only thing is, all the records in my database are now a duplicate of the updated one?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 08-02-10, 09:03 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by ckdoublenecks View Post
Now that's what I'm talking about. Only thing is, all the records in my database are now a duplicate of the updated one?
I'm sorry, I forgot to include the WHERE clause in the UPDATE query.
This should fix the problem.
PHP Code:

<?php
$host
="localhost";
$username="root";
$password="";
$database="prerentdb";
$database="test";
mysql_connect($host,$username,$password);
mysql_select_db($database) or die( "Unable to select database");

if(!empty(
$_POST["submit"]))
{
 
$apt $_POST['apt'];
 
$query="SELECT * FROM payments Where apt='$apt'";
 
$result=mysql_query($query);
 if(
mysql_num_rows($result))
 {
  echo 
"<form action='#' method='post'><b><center>Present Report</center></b><br /><br />
         <table border='1'>
          <tr>
           <th>dep</th>
           <th>apt</th>
           <th>tenant</th>
           <th>month</th>
           <th>prev</th>
           <th>misc</th>
           <th>rent</th>
           <th>hud</th>
           <th>tenant</th>
           <th>amt</th>
           <th>date</th>
          </tr>
          <tr>
           <th colspan=2>num</th>
           <th>name</th>
           <th>chg</th>
           <th>bal</th>
           <th>amt</th>
           <th>due</th>
           <th colspan=2>pay</th>
           <th colspan=2>paid</th>
          </tr>"
;

  while(
$row mysql_fetch_assoc($result))
  {
   echo 
"<tr>
          <td><input type='text' name='dep' value='" 
$row['dep'] . "'></td>
          <td><input type='text' name='apt' value='" 
$row['apt'] . "' readonly style='border:none;' onmouseup='this.blur()'></td>
          <td><input type='text' name='name' value='" 
$row['name'] . "'></td>
          <td><input type='text' name='rent' value='" 
$row['rent'] . "'></td>
          <td><input type='text' name='prevbal' value='" 
$row['prevbal'] . "'></td>
          <td><input type='text' name='misc' value='" 
$row['misc'] . "'></td>
          <td><input type='text' name='rentdue' value='" 
$row['rentdue'] . "'></td>
          <td><input type='text' name='hudpay' value='" 
$row['hudpay'] . "'></td>
          <td><input type='text' name='tentpay' value='" 
$row['tentpay'] . "'></td>
          <td><input type='text' name='amtpaid' value='" 
$row['amtpaid'] . "'></td>
          <td><input type='text' name='datepaid' value='" 
$row['datepaid'] . "'></td>
         </tr>
         <tr>
          <td colspan='11' align='center' style='padding-top:20px;'><input type='submit' name='update' value='Update Record'></td>
         </tr>"
;
   }
  echo 
"</table>
        </form>"
;
  }
 else{echo 
"No listing for appartment $apt.<br />Please select another.<br />";}
 }
if(!empty(
$_POST["update"]))
{
 
$sql "UPDATE payments SET dep='".mysql_real_escape_string($_POST["dep"])."',apt='".mysql_real_escape_string($_POST["apt"])."',name='".mysql_real_escape_string($_POST["name"])."',rent='".mysql_real_escape_string($_POST["rent"])."',prevbal='".mysql_real_escape_string($_POST["prevbal"])."',misc='".mysql_real_escape_string($_POST["misc"])."',rentdue='".mysql_real_escape_string($_POST["rentdue"])."',hudpay='".mysql_real_escape_string($_POST["hudpay"])."',tentpay='".mysql_real_escape_string($_POST["tentpay"])."',amtpaid='".mysql_real_escape_string($_POST["amtpaid"])."',datepaid='".mysql_real_escape_string($_POST["datepaid"])."' WHERE apt='".$_POST["apt"]."'";
 
mysql_query($sql) or die("Update query failed.");
 echo 
"Record for appartment ".$_POST["apt"]." has been updated ...";
 }
?>
<form method="post" action="#">
 <br />
 <input type="text" name="apt"/>
 <p>
<input type="submit" name="submit" value="Select Apartment for Payment"/>
</form>
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 08-02-10, 12:55 PM
ckdoublenecks ckdoublenecks is offline
Wannabe Coder
 
Join Date: Jul 2010
Posts: 123
Thanks: 11
Thanked 0 Times in 0 Posts
I got this message: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\hofiles\prerep.php on line 15
No listing for appartment a2.
Please select another.

Below is a printout of the database:

Prerent listing

dep apt tenant month prev misc rent hud tenant amt date
num name chg bal amt due pay paid
0 A1 testing Roth 549 10 0 0 0 0 0
0 A2 Roth 530 0 0 0 0 0 530

Last edited by ckdoublenecks; 08-02-10 at 12:58 PM. Reason: to clarify
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 08-02-10, 07:59 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
These are the fields and the data that I put into the table "payments":
Code:
===Database test

== Dumping data for table payments

|------
|dep|apt|name|rent|prevbal|misc|rentdue|hudpay|tentpay|amtpaid|datepaid
|------
|0|A1|testing Roth|549|10|0|0|0|0|0| 
|0|A2|Roth|530|0|0|0|0|0|530|
And this is the code I used to access the table:
PHP Code:

<?php
$host
="localhost";
$username="root";
$password="";
$database="prerentdb";
//$database="test";
mysql_connect($host,$username,$password);
mysql_select_db($database) or die( "Unable to select database");

if(!empty(
$_POST["submit"]))
{
 
$apt $_POST['apt'];
 
$query="SELECT * FROM payments Where apt='$apt'";
 
$result=mysql_query($query);
 if(
mysql_num_rows($result))
 {
  echo 
"<form action='#' method='post'><b><center>Present Report</center></b><br /><br />
         <table border='1'>
          <tr>
           <th>dep</th>
           <th>apt</th>
           <th>tenant</th>
           <th>month</th>
           <th>prev</th>
           <th>misc</th>
           <th>rent</th>
           <th>hud</th>
           <th>tenant</th>
           <th>amt</th>
           <th>date</th>
          </tr>
          <tr>
           <th colspan=2>num</th>
           <th>name</th>
           <th>chg</th>
           <th>bal</th>
           <th>amt</th>
           <th>due</th>
           <th colspan=2>pay</th>
           <th colspan=2>paid</th>
          </tr>"
;

  while(
$row mysql_fetch_assoc($result))
  {
   echo 
"<tr>
          <td><input type='text' name='dep' value='" 
$row['dep'] . "'></td>
          <td><input type='text' name='apt' value='" 
$row['apt'] . "' readonly style='border:none;' onmouseup='this.blur()'></td>
          <td><input type='text' name='name' value='" 
$row['name'] . "'></td>
          <td><input type='text' name='rent' value='" 
$row['rent'] . "'></td>
          <td><input type='text' name='prevbal' value='" 
$row['prevbal'] . "'></td>
          <td><input type='text' name='misc' value='" 
$row['misc'] . "'></td>
          <td><input type='text' name='rentdue' value='" 
$row['rentdue'] . "'></td>
          <td><input type='text' name='hudpay' value='" 
$row['hudpay'] . "'></td>
          <td><input type='text' name='tentpay' value='" 
$row['tentpay'] . "'></td>
          <td><input type='text' name='amtpaid' value='" 
$row['amtpaid'] . "'></td>
          <td><input type='text' name='datepaid' value='" 
$row['datepaid'] . "'></td>
         </tr>
         <tr>
          <td colspan='11' align='center' style='padding-top:20px;'><input type='submit' name='update' value='Update Record'></td>
         </tr>"
;
   }
  echo 
"</table>
        </form>"
;
  }
 else{echo 
"No listing for appartment $apt.<br />Please select another.<br />";}
 }
if(!empty(
$_POST["update"]))
{
 
$sql "UPDATE payments SET dep='".mysql_real_escape_string($_POST["dep"])."',apt='".mysql_real_escape_string($_POST["apt"])."',name='".mysql_real_escape_string($_POST["name"])."',rent='".mysql_real_escape_string($_POST["rent"])."',prevbal='".mysql_real_escape_string($_POST["prevbal"])."',misc='".mysql_real_escape_string($_POST["misc"])."',rentdue='".mysql_real_escape_string($_POST["rentdue"])."',hudpay='".mysql_real_escape_string($_POST["hudpay"])."',tentpay='".mysql_real_escape_string($_POST["tentpay"])."',amtpaid='".mysql_real_escape_string($_POST["amtpaid"])."',datepaid='".mysql_real_escape_string($_POST["datepaid"])."' WHERE apt='".$_POST["apt"]."'";
 
mysql_query($sql) or die("Update query failed.");
 echo 
"Record for appartment ".$_POST["apt"]." has been updated ...";
 }
?>
<form method="post" action="#">
 <br />
 <input type="text" name="apt"/>
 <p>
<input type="submit" name="submit" value="Select Apartment for Payment"/>
</form>
The only thing that is different in my code is the database name "test".
In the code your database should be "prerentdb".

This code works perfectly with my database.
Also my database is not case sensitive, so I can use "a1' or "A1" in my search.
Maybe yours is case sensitive, or you changed the code somehow.
__________________
Jerry Broughton

Last edited by job0107; 08-02-10 at 08:05 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 11:17 AM
help with error messages.. please APuppyDog PHP 2 10-06-06 12:09 AM
PHP Downside--Solutions? Amulet PHP 10 07-15-05 09:26 AM


All times are GMT -5. The time now is 06:45 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.