Take a Loook.....I am just stucked

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Take a Loook.....I am just stucked

Post by crazytopu »

This is a form which lets users modify the value of DB table. There is one text box that takes the primary key value which is call_no, and a button called modify.

Code: Select all

<html> 
<body> 
<form method="post" action="mtest.php"> 
<table border="0" width="75%"> 
  <tr> 
    <td width="15%">
      <p align="right">Call No:</p>
    </td> 
    <td width="57%"><input type="text" name="call_no" size="20"></td> 
 
  </tr> 
 <input type="submit" value="Modify " name="Modify">
 
  </table> 
</form> 
</body> 
</html>
When the user suppies a call_no and hits the modify button mtest.php gets called.

mtest.php looks like:

Code: Select all

<html> 

<body> 






<?php 

$db = mysql_connect("localhost", "root"); 
mysql_select_db("library",$db) or die(mysql_error()); 
$sql = "Select * FROM book WHERE author1=('&#123;$_POST&#1111;'call_no']&#125;')"; 

$result = mysql_query($sql); 

?> 


<table border="0" width="75%"> 
  <tr> 
    <td width="15%">Call No</td> 
    <td width="62%"><input type="text" name="call_no" size="20"></td> 
    
  </tr> 
  <tr> 
    <td width="15%">Book Title</td> 
    <td width="62%"><input type="text" name="title" size="20"></td> 
    
  </tr> 
  <tr> 
    <td width="100%" colspan="3"></td> 
  </tr> 
 
<td height="38"><input type="submit" value="save new values" name="savenewvalues">
  <td height="61"> 
 
<?php 

while ($myrow = mysql_fetch_row($result)) 
&#123; 
   echo '<tr>'; 
   for($i=0; $i<10; $i++) 
   &#123; 
      echo '<td>'.$myrow&#1111;$i].'</td>'; 
   &#125; 
   echo '</tr>'; 
&#125; 

?> 

</table> 


</body> 

</html>

Call No and Book Title are two columns in my book table. So when user wants to modify call no= 1 and hits modify button mtest.php file retrives the specific row from the book table and displays it in two text field - call no and title.

User then changes the value and press save new value button. The value gets saved.

I have a number of questions:

1) I know how to display data in a table format but if do so it does not let me to change any value. It is read only. If i use text box instead to display my retrieved data will they let me to change value online?

2) what mistakes i made in the following code that does not display retrieved data in those two text fields?

Code: Select all

while ($myrow = mysql_fetch_row($result)) 
&#123; 
   echo '<tr>'; 
   for($i=0; $i<10; $i++) 
   &#123; 
      echo '<td>'.$myrow&#1111;$i].'</td>'; 
   &#125; 
   echo '</tr>'; 
&#125;

Where it works fine here in case of a table?

Code: Select all

<?php 

$db = mysql_connect("localhost", "root"); 
mysql_select_db("library",$db) or die(mysql_error()); 
$result = mysql_query("SELECT * FROM book",$db); 

?> 



<table border=2> 
  <tr> 
    <td>call_no</td> 
    <td>title</td> 
  </tr> 
<?php 

while ($myrow = mysql_fetch_row($result)) 
&#123; 
   echo '<tr>'; 
   for($i=0; $i<10; $i++) 
   &#123; 
      echo '<td>'.$myrow&#1111;$i].'</td>'; 
   &#125; 
   echo '</tr>'; 
&#125; 
?> 

</table>
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

modify.php file

Code: Select all

<html> 

<body> 

 
<form method="post" action="mtest2.php"> 

<table border="0" width="75%"> 
  <tr> 
    <td width="15%">
      <p align="right">Call No:</p>
    </td> 
    <td width="57%"><input type="text" name="call_no" size="20"></td> 
    
  </tr> 
  

 <input type="submit" value="Modify " name="Modify">
 
   
  </table> 


</form> 


</body> 

</html>
calls mtest2.php [action="mtest2.php"> ]

Code: Select all

<html> 
<head></head> 
<body> 


<?php 

$db = mysql_connect("localhost", "root"); 
mysql_select_db("library",$db) or die(mysql_error()); 

$sql = "Select call_no,title FROM book WHERE call_no=('&#123;$_POST&#1111;'call_no']&#125;')";

$result = mysql_query($sql); 


?> 


<form method="POST" action="update.php">


<table border=2> 
  <tr> 
    <td>call_no</td> 
    <td>title</td> 
    
  </tr> 
<?php 
while ($myrow = mysql_fetch_row($result)) 
&#123; 
   echo '<tr>'; 
   for($i=0; $i<10; $i++) 
   &#123; 
      echo '<td>'.$myrow&#1111;$i].'</td>'; 
   &#125; 
   echo '</tr>'; 
&#125; 
?> 

</table> 

<p><input type="submit" value="Save New Value" name="SNV"></p>

</form>

</body> 
</html>
update.php

Code: Select all

<html> 

<body> 

<?php

if (isset($_POST&#1111;'savenewvalues'])) &#123; 

// save changed data 


$db = mysql_connect("localhost", "root"); 

mysql_select_db("library",$db); 

$sql = "UPDATE BOOK SET call_no=('&#123;$_POST&#1111;'call_no']&#125;' , title='&#123;$_POST&#1111;'title']&#125;'";


$result = mysql_query($sql); 

echo "Your data was changed successfuly\n"; 

&#125;

</html> 

</body>

But i cannot change the data inside a table. How can i make my data modifiable? Does PHP let users to change the retrieved value inside a table?

Please shed some light
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

Little correction:

It should be

Code: Select all

if (isset($_POST&#1111;'SNV']))
And not

Code: Select all

if (isset($_POST&#1111;'savenewvalues']))
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

Ok guys, just tell me how would you like to modify your data in the client side and save it back again? Table? Does table let you to modify your value? or text box?
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

You could try these lines:

Code: Select all

<?php
while ($myrow = mysql_fetch_assoc($result)) {
   echo "<tr>\n";
   foreach ($myrow as $key => $value) {
      echo "\t".'<td><input name="'.$key.'" value="'.$value.'" /></td>'."\n";
   }
   echo "</tr>\n";
}
?>
Cheers,
Scorphus.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

Code: Select all

foreach ($myrow as $key => $value
what would be my $key and $value?

Is $key = $call_no
and $value = ('{$_POST['call_no']}')
??

Something like this?:

Code: Select all

foreach ($myrow as $call_no=> ('&#123;$_POST&#1111;'call_no']&#125;')
I tried this way but did not get any result
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

No no, not like that. For each makes a loop in this sense.

For each key and value from an array, loop asign the key $key and the value $value. Here's to experiment:

Code: Select all

<?php

$array = array("Nay" => "Rocks", "qads" => "Hairy");

foreach ($array AS $key => $value) {

      print $key . " => " . $value . "<br />";

}

?>
I didn't read your entire script though, will take a look later. Gota run right now.

-Nay
Post Reply