do you know how to?

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:

do you know how to?

Post by crazytopu »

this is a php file that takes call_no input by the libriran and then use this call_no to retrieve the book_name from the book table.

For less complexity i didnot paste the exact same code here..I have just copied part of the actual code...but the following code should make it clear what i want...



Code: Select all

<?php

<?php


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

mysql_select_db("library",$db); 

$sql = "SELECT * FROM book WHERE call_no=('{$_POST['call_no']}')";

$result = mysql_query($sql); 


?>



<form method="post" action="<?php echo $PHP_SELF?>"> 

<table border="0" width="75%"> 
  <tr> 
    <td width="15%">Book Name</td> 
    <td width="62%"><input type="text" name="book_name" size="20"></td> 
       
  </tr> 


<?
while ($myrow = mysql_fetch_row($result)) 
{ 

<input type="text" name="book_name" value=$myrow[1]; 
echo '<br>';
}
 
?>



?>
I do need help in assigning the $myr ... dvance...
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: do you know how to?

Post by TheBentinel.com »

If you change your code to:

Code: Select all

while ($myrow = mysql_fetch_row($result)) 
{ 

print ('<input type="text" name="book_name" value="' . $myrow[1] . '">'); 
echo '<br>';
}
I think that'll get you what you're looking for.
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

crazytopu,
Did you read my response in your last post? The ONLY way to do this on the same page is to decide wether or not there is a value in book_call_no and then check to see if it is in the database or not. If it is in the database then you need to do what Bentinel told you to do, otherwise do an insert. If there is no value in book_call_no then it is a blank form and you can use plain html. Look here again: viewtopic.php?t=19411

If I am missing your point, I apologise, but you cannot write the form in HTML and then later in your page decide to put values in it. You need an if statement and you have to create your form twice, but you can copy and paste it.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

johnperkins21,

Sorry for not being prompt in answering you. I do really appreciate your effort in helping me out and yes i did see the your response on another post. That was almost close to what i wanted.

But, i thought perhaps there would be a way that would help me organise my text fields better in the html page. You know, i used the code that TheBentinel.com gave me..and it works fine.

You are right..i tried to see if i could assign the value into my text fields and display them..but it was no success avail at the end.

I still got a question... look at this code:

Code: Select all

<?php

print ('<input type="text" name="call_no" value="' . $myrow[0] . '">'); 
echo '<br>'; 

print ('<input type="text" name="title" value="' . $myrow[1] . '">'); 
echo '<br>'; 

?>

The above code display two text fields aligned properly....but if i want to [which i have to] insert level infront of the text field like this:

Code: Select all

<?php


print ('Call No<input type="text" name="call_no" value="' . $myrow[0] . '">'); 
echo '<br>'; 

print ('Book Title<input type="text" name="title" value="' . $myrow[1] . '">'); 
echo '<br>'; 


?>

note levels are - Call No and Book Title...i get my text fields..not aligned properly anymore...

i get one text field.shorter than the other just becoz of the length of the level..Call No is shorter in length than Book Title..and hence the first text field looks shorter than the latter..'


i would appreciate if you can tell me how you align when you code.

Thousands thanks..
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

If you're just looking for alignment, that's an HTML question. What you would do is create a table and put your form in the table, that way it all lines up.

example:

Code: Select all

<table border=1 cellpadding=1 cellspacing=1>

<tr>
<td width=50 align=left>Call No</td>
<td align=left><<input type="text" name="call_no"></td>
</tr>

<tr>
<td width=50 align=left>Book Title</td>
<td align=left><<input type="text" name="title"></td>
</tr>

</table>
That's what you would do for the blank form. Copy the form code that you write and paste it to execute in the if statement for a found book and change your inputs to the code benitnel gave you (and remember to escape the " using \".

If you want to change the size of the text fields use size=20 where 20 represents the number of characters.

Good luck.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

johnperkins21

Thanks a lot.

I still got a simple question ..maybe you can answer..

okay...so this is how i put my code:

Code: Select all

<?php
<?php



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

mysql_select_db("library",$db); 

$sql = "SELECT * FROM book WHERE call_no=('{$_POST['call_no']}')";

$result = mysql_query($sql); 


?>

<?
while ($myrow = mysql_fetch_row($result)) 
{ 

echo
'<form method="post" action="<?php echo $PHP_SELF?>"> 

<table border="0" width="75%"> 
  <tr> 
    <td width="15%">Call No</td> 
    <td width="62%"><input type="text" name="call_no" value ="'.$myrow[0].'" size="20"></td> 
    <td width="23%" bgcolor="#FFFFFF"><a href="http://localhost/deletebook.php"><font color="#0000FF">Delete
      Entry</font></a></td> 
  </tr> 
  <tr> 
    <td width="15%">Book Title</td> 
    <td width="62%"><input type="text" name="title" value ="'.$myrow[1].'"size="20"></td> 
    <td width="23%" bgcolor="#FFFFFF"><a href="http://localhost/modify.php"><font color="#0000FF">Modify
      Entry</font></a></td> 
  </tr> 
  <tr> 
    <td width="15%">Author1</td> 
    <td width="62%"><input type="text" name="author1" value ="'.$myrow[2].'" size="20"></td> 
    <td width="23%"><font color="#000000"><a href="http://localhost/search.php">Search For Entry</a></font></td> 
  </tr> 
  <tr> 
    <td width="15%">Author2</td> 
    <td width="62%"><input type="text" name="author2" value= "'.$myrow[3].'"size="20"></td> 
    <td width="23%"><font color="#0000FF"><a href="http://localhost/showdata.php">Show
      All Entries</a></font></td> 
  </tr> 
  <tr> 
    <td width="15%">Publisher</td> 
    <td width="85%" colspan="2"><input type="text" name="publisher" value ="'.$myrow[4].'"size="20"></td> 
  </tr> 
  <tr> 
    <td width="15%">ISBN</td> 
    <td width="85%" colspan="2"><input type="text" name="isbn" value = "'.$myrow[5].'"size="20"></td> 
  </tr> 
  <tr> 
    <td width="15%">Price</td> 
    <td width="85%" colspan="2"><input type="text" name="price" value ="'.$myrow[6].'"size="20"></td> 
  </tr> 
  <tr> 
    <td width="15%">No of Page</td> 
    <td width="85%" colspan="2"><input type="text" name="no_of_page" value="'.$myrow[7].'" size="20"></td> 
  </tr> 
  <tr> 
    <td width="15%">Edition</td> 
    <td width="85%" colspan="2"><input type="text" name="edition" value="'.$myrow[8].'"size="20"></td> 
  </tr> 
  <tr> 
    <td width="15%">Category</td> 
    <td width="85%" colspan="2"><input type="text" name="category" value="'.$myrow[9].'" size="20"></td> 
  </tr> 
</table> 

<input type="submit" value="Update" name="Update"> 


</form>'; 

    

echo '<br>';


}
?>
As you can see there is a update button.

If i set the following code in top of the page or inside the while loop i get an warning from Apache server saying that Access is Forbidden.

Code: Select all

<?php

if (isset($_POST['Update'])) {

echo "Old data has been updated"; }

?>

Can you tell me where should i put the above snippet of code in order to get the messege output ["Old data has been updated"] once i hit the update button?

If i can output the messege ..the rest of the job is pretty simple i guess. Just taking the value from the text field and update by book table.

Thanks!
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

I don't know about that error, as I am not that well versed in php. But I would probably want to put that bit of code after your mysql_query("INSERT blah, blah"); section.

Also, you don't need a while loop since you should only get one record back with the call_no.

And, this
echo
'<form method="post" action="<?php echo $PHP_SELF?>">

shouldn't it be this
echo
'<form method="post" action="$PHP_SELF?">
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

Okay...i put the bit of code where you suggested but it didnot work..i still get the same error.

Code: Select all

Forbidden
You don't have permission to access /&lt; on this server.


--------------------------------------------------------------------------------

Apache/1.3.28 Server at localhost Port 80


And i dont understand why dont i need a while loop? It is right that I am receiving only one row. but without using mysql_fetch_row how do I expect to input all the columns? i got 10 columns you see and that's why i m using the myrow array to display them one after another..


And one last thing..what is the difference between

Code: Select all

<?php

<form method="post" action="<?php echo $PHP_SELF?>"> 


?>
and

Code: Select all

<?php

<form method="post" action="$PHP_SELF?">
?>
??


thanks..
?>
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

You need to use a while loop when you return multiple rows. Do this when you only have one row:

Code: Select all

<?php
$myrow = mysql_fetch_row($result);

?>
This will fetch the whole row. So $myrow[0] is the first column in that row, $myrow[1] is the second, etc. When you do a while loop it does the fetch_row for as many rows that are returned. Thus:

Code: Select all

<?php
while ($myrow = mysql_fetch_row($result)) {

echo $myrow[0];  //first column in current row

}  //when you get here, it will go back to the while loop and look for the next row.

?>
Since you only have one row, there is no need for the while loop. You can still use it if you really want to, but if you get 2 row for some reason, it will execute the code twice and you definitely don't want that.

As far as why you're getting the error? I don't know, try replacing $PHP_SELF with a an actual link (i.e. http//yoursite.com/yourpage.php) and see what happens.

The difference between:

Code: Select all

<?php
<form method="post" action="<?php echo $PHP_SELF?>"> 

and

<form method="post" action="$PHP_SELF"> 


?>
is that since you are already in an open php tag, when you do the first one you should receive an error since you have two <?php in a row. You should never have <?php ?> inside of a php echo statement.

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

Post by crazytopu »

hi,

Just tell me if I am thinking in the right direction.


since i am displaying the values into the text fields using echo function [inside the php block] I cant use this code inside the echo quote ('')

Code: Select all

<?php
<?php 

if (isset($_POST['Update'])) { 

echo "Old data has been updated"; } 

?>

?>
..and since i cant use this code inside the echo quote ('') [or from the php block i wanted to mean]I am unable to show the messege (Old data has been updated) when i hit the update button. It is becuse what i think is ...the form is only recognised inside the echo quote

Code: Select all

..and so is its action.

So, this line 

 [syntax=php]<?php

<form method="post" action="$PHP_SELF?"> 

?>[/syntax]

is unable to display the messege when the update button is hit coz..it does not know where to look for. If the code were outside of the php block and so did the first piece of the code it would have no problem in displaying the desire messege..

Am I right?
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

I'm not sure what your question is this time around. What I was getting at is that the code you originally wrote:

Code: Select all

<?php
echo
'<form method="post" action="<?php echo $PHP_SELF?>">

<table border="0" width="75%">
  <tr>
    <td width="15%">Call No</td>
    <td width="62%"><input type="text" name="call_no" value ="'.$myrow[0].'" size="20"></td>
    <td width="23%" bgcolor="#FFFFFF"><a href="http://localhost/deletebook.php"><font color="#0000FF">Delete
      Entry</font></a></td>
  </tr> 
</form>';
?>
wouldn't work because of the line

Code: Select all

<?php
'<form method="post" action="<?php echo $PHP_SELF?>">


should be:
'<form method="post" action="$PHP_SELF">
?>
since you are already echoing that whole statement, you don't want to put <?php echo $PHP_SELF ?> in there, just use $PHP_SELF.

You can use the isset code, but you have to close your echo statment, but you shouldn't say that the data has been updated unless it actually has. Just because someone pushed a button doesn't mean that everything worked.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

Well, I think you did not see what i wrote before. I said:

If i can output the messege ..the rest of the job is pretty simple i guess. Just taking the value from the text field and update my book table.
Here, what i meant was simply take the data from the text field and then issue update command to update the specific row. I am just trying to work on outputting the messege so that i know where the if statement should be placed. If i can successfully output the messege I can surely replace the messege with real code which is connecting to database, taking the data from the text field...issuing update command..etc..etc.


And i did change to what you suggested..now my form looks like:

Code: Select all

<?php

'<form method="post" action="$PHP_SELF"> 

?>


I am not missing any of your suggestion ..am I?
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

You're right. Sorry about that. It looks like you're on the right track. Good luck.
Post Reply