10 Replies - 1417 Views - Last Post: 20 August 2011 - 05:47 AM Rate Topic: -----

#1 mabaton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 09

Trying to display information already in database in a form[co

Posted 19 August 2011 - 10:25 AM

I am trying to get this to pull the current information in the database and display it in a form which can then be updated. The update function works fine, in fact everything works fine except it does not dispaly the current information. It shows up as blank fields so to update all of the fields you have to type in the information for the fields you don't want to update also otherwise it clears what is in there.

<?php
$host="xxxxxxx"; // Host name 
$username="xxxxxxx"; // Mysql username 
$password="xxxxxx"; // Mysql password 
$db_name="xxxxxx"; // Database name 
$tbl_name="xxxxxx"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

if(isset($_POST['Submit'])){
$id=$_GET['client_contract_id'];

$query=" SELECT client_name, client_address, client_city, client_state, client_zip_code, client_phone, contract_fee, contract_start_date, contract_end_date, contract_terms FROM $tbl_name WHERE client_contract_id='$id'";

$result=mysql_query($query);
$num=mysql_numrows($result);

$i=0;
while ($i < $num) {
	//$id=mysql_result($result,$i,"client_contract_id");
	$name=mysql_result($result,$i,"client_name");
	$add=mysql_result($result,$i,"client_address");
	$city=mysql_result($result,$i,"client_city");
	$state=mysql_result($result,$i,"client_state");
	$zipcode=mysql_result($result,$i,"client_zip_code");
	$phone=mysql_result($result,$i,"client_phone");
	$fee=mysql_result($result,$i,"contract_fee");
	$start=mysql_result($result,$i,"contract_start_date");
	$end=mysql_result($result,$i,"contract_end_date");
	$terms=mysql_result($result,$i,"contract_terms");
	
	++$i;
	}	
}
	?>
    <form action="updateclient.php" method="post">
    <input type="hidden" name="ud_request_number" value="<? echo $request_number; ?>">
    <table>
    	    
    <tr><td>Name :</td>	
    <td><input type="text" name="u_client_name" size="30" value='<?php echo ($name); ?>'></td></tr>	
    <tr><td>Address :</td>	
    <td><input type="text" name="u_client_address" size="30" value='<?php echo ($add); ?>'></td></tr>
    <tr><td>City :</td>	
    <td><input type="text" name="u_client_city" size="30" value='<?php echo ($city); ?>'></td></tr>
    <tr><td>State :</td>	
    <td><input type="text" name="u_client_state" size="30" value='<?php echo ($state); ?>'></td></tr>
    <tr><td>Zipcode :</td>
    <td><input type="text" name="u_client_zip_code" size="30" value='<?php echo ($zipcode); ?>'></td></tr>	
    <tr><td>Phone :</td>	
    <td><input type="text" name="u_client_phone" size="30" value='<?php echo ($phone); ?>'></td></tr>	
    <tr><td>Contract Fee :</td>	
    <td><input type="text" name="u_contract_fee" size="30" value='<?php echo ($fee); ?>' ></td></tr>
    <tr><td>Start Date :</td>	
    <td><input type="date" name="u_contract_start_date" size="30" value='<?php echo ($start); ?>' ></td></tr>
    <tr><td>End Date :</td>	
    <td><input type="date" name="u_contract_end_date" size="30" value='<?php echo ($end); ?>' ></td></tr>
    <tr><td>Terms :</td>	
    <td><input type="text" name="u_contract_terms" size="50" value='<?php echo ($terms); ?>' ></td></tr></table>    
    <p> <input type="Submit" value="Update"></html><html></form>


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to display information already in database in a form[co

#2 Boy67   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 28-August 08

Re: Trying to display information already in database in a form[co

Posted 19 August 2011 - 11:53 AM

Correct me if I'm wrong anyone, but i think it should be like this.

$query=" SELECT client_name, client_address, client_city, client_state, client_zip_code, client_phone, contract_fee, contract_start_date, contract_end_date, contract_terms FROM $tbl_name WHERE client_contract_id='$id'";

$result=mysql_query($query);
$row = mysql_fetch_array($result);

$name = $row['client_name'];
$add = $row['client_address'];


Was This Post Helpful? 0
  • +
  • -

#3 mabaton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 09

Re: Trying to display information already in database in a form[co

Posted 19 August 2011 - 02:58 PM

Thank you for your reply and your time looking at my code. That part of the code actually works...I think. Its this part that I am having trouble with:
<tr><td>Name :</td>     

43     <td><input type="text" name="u_client_name" size="30" value='<?php echo ($name); ?>'></td></tr>   


and more so this: value='<?php echo ($name); ?>'> I think this is supposed to display what is in the database currently so that it can be updated. Like a prefilled form to update.
Can anyone help?
Was This Post Helpful? 0
  • +
  • -

#4 qtex   User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 142
  • Joined: 24-October 09

Re: Trying to display information already in database in a form[co

Posted 19 August 2011 - 03:49 PM

if I'm right it should be like this :

<td><input type="text" name="u_client_name" size="30" value='<?php $name ?>'></td></tr>  


OR

echo "<td><input type='text' name='u_client_name' size='30' value='$name'></td></tr>";  


-Qtex
Was This Post Helpful? 0
  • +
  • -

#5 mabaton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 09

Re: Trying to display information already in database in a form[co

Posted 19 August 2011 - 04:07 PM

View Postqtex, on 19 August 2011 - 04:49 PM, said:

if I'm right it should be like this :

<td><input type="text" name="u_client_name" size="30" value='<?php $name ?>'></td></tr>  


OR

echo "<td><input type='text' name='u_client_name' size='30' value='$name'></td></tr>";  


-Qtex


I tried both, niether worked. Still trying different things but I think I'm just going in circles.
Was This Post Helpful? 0
  • +
  • -

#6 Ace26   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 186
  • Joined: 10-August 08

Re: Trying to display information already in database in a form[co

Posted 19 August 2011 - 04:53 PM

Your input elements were all missing the closing slash "/". This should work just find like so:
<tr><td>State :</td>	
    <td><input type="text" name="u_client_state" size="30" value=<?php echo "\"".$state."\""; ?>/>



Notice how value is set. Use this approach for all the others and all should be fine. Also like has been pointed out here times without number, use PDO for your database transactions and not the now deprecated mysql_* functions.

This post has been edited by Ace26: 19 August 2011 - 05:00 PM

Was This Post Helpful? 0
  • +
  • -

#7 Boy67   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 28-August 08

Re: Trying to display information already in database in a form[co

Posted 19 August 2011 - 04:56 PM

Hmm, ok how about this

<tr><td>Name :</td>  
    <td><input type="text" name="u_client_name" size="30" value='<?php echo $name ?>'></td></tr> 

This post has been edited by Boy67: 19 August 2011 - 04:56 PM

Was This Post Helpful? 0
  • +
  • -

#8 mabaton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 09

Re: Trying to display information already in database in a form[co

Posted 19 August 2011 - 06:21 PM

Thank you for your replies. I have tried them and it still doesn't work. I will figure out a work around for now. Can't look at the code anymore today...gotta get up from this chair! Enjoy your weekend.
Was This Post Helpful? 0
  • +
  • -

#9 qtex   User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 142
  • Joined: 24-October 09

Re: Trying to display information already in database in a form[co

Posted 19 August 2011 - 06:24 PM

That should work. Your older code had variable $request_number, but it didn't get value from nowhere. Also you had one error there "$num=mysql_numrows($result);". It should be "mysql_num_rows".
<?php
$host="xxxxxxx"; // Host name 
$username="xxxxxxx"; // Mysql username 
$password="xxxxxx"; // Mysql password 
$db_name="xxxxxx"; // Database name 
$tbl_name="xxxxxx"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

if(isset($_POST['Submit'])){
$id=$_GET['client_contract_id'];
$request_number = 0; // !!!!!
$query=" SELECT client_name, client_address, client_city, client_state, client_zip_code, client_phone, contract_fee, contract_start_date, contract_end_date, contract_terms FROM $tbl_name WHERE client_contract_id='$id'";

$result=mysql_query($query);
$row = mysql_fetch_array($result);

	$name=$row['client_name'];
	$add=$row['client_address'];
	$city=$row['client_city'];
	$state=$row['client_state'];
	$zipcode=$row['client_zip_code'];
	$phone=$row['client_phone'];
	$fee=$row['contract_fee'];
	$start=$row['contract_start_date'];
	$end=$row['contract_end_date'];
	$terms=$row['contract_terms'];
	
}
	?>
    <form action="updateclient.php" method="post">
    <input type="hidden" name="ud_request_number" value="<?PHP $request_number ?>">
    <table>
    	    
    <tr><td>Name :</td>	
    <td><input type="text" name="u_client_name" size="30" value='<?php $name ?>'></td></tr>	
    <tr><td>Address :</td>	
    <td><input type="text" name="u_client_address" size="30" value='<?php $add ?>'></td></tr>
    <tr><td>City :</td>	
    <td><input type="text" name="u_client_city" size="30" value='<?php $city ?>'></td></tr>
    <tr><td>State :</td>	
    <td><input type="text" name="u_client_state" size="30" value='<?php $state ?>'></td></tr>
    <tr><td>Zipcode :</td>
    <td><input type="text" name="u_client_zip_code" size="30" value='<?php $zipcode ?>'></td></tr>	
    <tr><td>Phone :</td>	
    <td><input type="text" name="u_client_phone" size="30" value='<?php $phone ?>'></td></tr>	
    <tr><td>Contract Fee :</td>	
    <td><input type="text" name="u_contract_fee" size="30" value='<?php $fee ?>' ></td></tr>
    <tr><td>Start Date :</td>	
    <td><input type="date" name="u_contract_start_date" size="30" value='<?php $start ?>' ></td></tr>
    <tr><td>End Date :</td>	
    <td><input type="date" name="u_contract_end_date" size="30" value='<?php $end ?>' ></td></tr>
    <tr><td>Terms :</td>	
    <td><input type="text" name="u_contract_terms" size="50" value='<?php $terms ?>' ></td></tr></table>    
    <p> <input type="Submit" value="Update"></html><html></form>

Was This Post Helpful? 0
  • +
  • -

#10 Ändrew   User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 312
  • Joined: 21-April 08

Re: Trying to display information already in database in a form[co

Posted 20 August 2011 - 02:37 AM

Ok remember ' is not the same as " and echo is not a function so you do not need to use parenthesis.

    <tr><td>Name :</td>	
    <td><input type="text" name="u_client_name" size="30" value="<?php echo $name; ?>"></td></tr>


This post has been edited by Ändrew: 20 August 2011 - 02:38 AM

Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Trying to display information already in database in a form[co

Posted 20 August 2011 - 05:47 AM

if(isset($_POST['Submit'])){
$id=$_GET['client_contract_id'];


That's puzzling, you're mixing POST and GET, which while possible, is perhaps an issue without seeing the page calling this script.

Also, you're using user-provided data without cleaning it. Search Google for SQL Injection for why this is bad.

Better code (assuming you really do want to mix GET and POST here):

if(isset($_POST['Submit'], $_GET['client_contract_id']) {

    $query=" SELECT client_name, client_address, client_city, client_state, client_zip_code, client_phone, contract_fee, contract_start_date, contract_end_date, contract_terms FROM $tbl_name WHERE client_contract_id=" . mysql_real_escape_string($_GET['client_contract_id']);

    $result=mysql_query($query) or die("Query {$query} failed: " . mysql_error());

    while ($row = mysql_fetch_array($result)) {
        $name = $row['client_contract_id'];
        $add = $row['client_name'];
        ...
    }


And from where is $request_number coming?

A better option overall would be to use PDO over the deprecated mysql functions. We have a ton of tutorials here on that by the eminent Dormilich.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1