Help a newbie to learn quicker

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:

Help a newbie to learn quicker

Post by crazytopu »

hi guys, u might wonder why am i posting these kinds of problem here where i can easily look them up in an appropriate book.

Well, the first reason is that, i feel if i post here my problem I get more accurate answer which fulfils my needs better. secondly, i do not have a library access where i can find all the relevant books. thirdy, i cannot afford buying all these books.


So, hope experts lend their support in helping me learn PHP better.

the following code works job fine as long as just insertion of data in concerned. but if i delete one row from it..the auto increment no doest not go back to its previous postion. what is mean is for example in my book table i have a primary key named call_no which is a auto increment int type.

If i insert a few row it starts from 1,2,3.........and so on.. but if i delete for example row no 5, and then insert another row....it skip the no 5 and move on to 6. My question is how can i over come this problem?

It looks like something :

Code: Select all

1
2
3
4
їafter i deleted it there is no row no 5!!]
6
My second question is [if u feel like helping me writing neat and clean code u can answer], how can i replace all those &nbsp from my form? i used all those spaces to align my fields so that they get displayed more properly.

here is the code:

Code: Select all

<html> 

<body> 
<?php 

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

echo "just a min..we will make it work soon "; 

}


else if (isset($_POST['submit'])) { 



// process form 

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

mysql_select_db("library",$db); 

$sql = "INSERT INTO book (title,author1,author2,publisher,isbn,price,no_of_page,edition) VALUES ('{$_POST['title']}','{$_POST['author1']}','{$_POST['author2']}','{$_POST['publisher']}','{$_POST['isbn']}','{$_POST['price']}','{$_POST['no_of_page']}','{$_POST['edition']}')"; 


$result = mysql_query($sql); 

echo "Your data was inserted successfuly\n"; 

} else{ 



// display form 



?>
<table border="0" width="35%">
  <tr>
    <td width="100%"><img border="0" src="f:\web\ibcslarge.png" width="317" height="30"></td>
  </tr>
  <tr>
    <td width="100%">&nbsp;</td>
  </tr>
</table>
<table border="0" width="35%">
  <tr>
    <td width="100%">&nbsp;</td>
  </tr>
  <tr>
    <td width="100%"></td>
  </tr>
</table>
<table border="0" width="100%">
  <tr>
    <td width="100%"><a href="http://">Home&nbsp;</a>&nbsp; <a href="http://">Account&nbsp;&nbsp;</a>&nbsp;
      <a href="file:///F:/my%20project%20work%20(final)/CD_ENTRY_FORM.htm">New
      CD Entry Form&nbsp;</a>&nbsp; <a href="http://">New Journal Entry
      Form&nbsp;</a>&nbsp; <a href="http://">Delete Record</a></td>
  </tr>
</table>
<table border="0" width="35%">
  <tr>
    <td width="100%">&nbsp;</td>
  </tr>
</table>
<table border="0" width="35%" bgcolor="#FFCC66">
  <tr>
    <td width="100%">
      <p align="center"><font size="3">NEW BOOK ENTRY FORM</font></td>
  </tr>
</table>
<table border="0" width="35%">
  <tr>
    <td width="100%">&nbsp;</td>
  </tr>
</table>



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

Book Title:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="Text" name="title" size="24"><br>

Author1:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="Text" name="author1" size="24"><br> 

Author2:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="Text" name="author2" size="24"><br> 

Publisher:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="Text" name="publisher" size="24"><br> 

ISBN:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="Text" name="isbn" size="24"><br>

Price:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="Text" name="price" size="8"><br> 

&nbsp;No Of Page:&nbsp; <input type="Text" name="no_of_page" size="8"><br> 

Edition:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="Text" name="edition" size="8">
<p> 

<input type="Submit" name="submit" value="Submit"> 
<input type="Submit" name="reset" value="Reset"> 


</p>


</form> 



<?php 



} // end if 



?> 



</body> 



</html>
Many many thanks

[Edit: Added PHP and CODE tags for eyecandy. Use 'em. --JAM]
User avatar
swirlee
Forum Newbie
Posts: 7
Joined: Fri Dec 26, 2003 12:08 am

Re: Help a newbie to learn quicker

Post by swirlee »

crazytopu wrote:the following code works job fine as long as just insertion of data in concerned. but if i delete one row from it..the auto increment no doest not go back to its previous postion. what is mean is for example in my book table i have a primary key named call_no which is a auto increment int type.

If i insert a few row it starts from 1,2,3.........and so on.. but if i delete for example row no 5, and then insert another row....it skip the no 5 and move on to 6. My question is how can i over come this problem?
You don't. Think of it this way: You're probably using the AUTO_INCREMENT value for some internal row id. The end user doesn't care what these values are (nor should you expect them to), and having gaps in the sequence of numbers doesn't use extra memory or processes or cause you any difficulty when coding. On the other hand, changing your code (or MySQL's) to automatically make sure there are no such gaps would waste plenty of time and memory and programming hours. What would you gain by ensuring that the numbers are all sequential? Well, nothing. With 32-bit INTs, you have some 4 billion numbers to play with. Stop worrying about it.
how can i replace all those &nbsp from my form? i used all those spaces to align my fields so that they get displayed more properly.
Use a table or, better yet, CSS.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

It does effect my code tho (I didnt want to start a new topic since it relates to his question), if you look here:

Code: Select all

while($i<$rows){
      $i++;
      $csql = "SELECT `con` FROM `$table` WHERE `id` = '$i' LIMIT 1";
      $n2sql = "SELECT `con` FROM `$table` WHERE `id` = '$i' LIMIT 1";
      $lsql = "SELECT `link` FROM `$table` WHERE `id` = '$i' LIMIT 1";
      $nsql = "SELECT `name` FROM `$table` WHERE `id` = '$i' LIMIT 1";
      $dsql = "SELECT `description` FROM `$table` WHERE `id` = '$i' LIMIT 1";
      $c2sql = "SELECT `clicks` FROM `$table` WHERE `id` = '$i' LIMIT 1";
      $clicks = mysql_result(mysql_query($c2sql), 0,0);
      $con = mysql_result(mysql_query($csql), 0,0);
      $new = mysql_result(mysql_query($n2sql), 0,0);
      $link = mysql_result(mysql_query($lsql), 0,0);
      $name = mysql_result(mysql_query($nsql), 0,0);
      $description = mysql_result(mysql_query($dsql), 0,0);
      if($con=="1" && $new=="1"){
echo <<<EOT
<a href="click.php?id=$i">$name</a>&nbsp;&nbsp;&nbsp;<div class="clicks">[Clicks: $clicks]</div><br>
&nbsp;&nbsp;&nbsp;$description<br><br>
EOT;
      }
   }
removal of id 5 would cause a error in the result query, so what would you suggest on getting the values from the db?

i've used

Code: Select all

for($i=0;$i<$rows;$i++){
  $sql = "SELECT `name` FROM `table` WHERE `id` = '$i'";
  $name = mysql_result(mysql_query($sql), 0,0);
  echo $name . "<br>";
}
for the longest time, but they rely on a value in the DB that is auto incremented (such as an id number).. how else would i get the values from the DB?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

@LiLpunkSkateR:
You are using $i as a counter so of course this is an issue.

Consider the below as a faster and (ID problem saver) solution. Not tested so rewrite to fit your needs.

Code: Select all

$q = mysql_query("select ID, con, link, name, description, clicks FROM $table");
    while ($row = mysql_fetch_assoc($q)) {
        echo '<a href="click.php?id='.$row['ID'].'">'.$row['name'].'</a>
              <div class="clicks">[Clicks: '.$row['clicks'].'</div><br>
              '.$row['description'].'<br /><br />';
    }
auto_inc. id's are not supposed to be used as a mean to calculate the amount of something (number of rows for example). There are internal sql functions for that that is much better (count, min/max etc).
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

ok i understand it takes up a lot of proramming hour to sequencially put the data into the DB. But then how can i let the users delete a row that they might want to remove from the DB?

By specifiying which column the users will delete one row? Let say i put 100 rows. deleted a few of them and inserted some more. Now even though i have exactly 100 rows my last row id is 134. coz

1.2.3..................66.67.72.73...........98.99.103.106.109....134

something like this.

now what if a user wants to delete a row that he inserted before? As you said users are not aware of the internal primary key. But in this case my primary key is that auto increment int.

To identify a row uniqely i must know the primary key, right? then how come a user will be able to delete a row?

I wanna give them a GUI mode to delete row from DB. They will select a column (dont you feel this column should be the primary key?) from a drop down list..and will hit delete button.

Share your own experience or suggestion..please
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Example:

Code: Select all

<?php
 if (!empty($_POST)) {
    echo '<pre>';
    print_r($_POST);
    echo "Recommended query: "delete from test where Id = '$_POST[id]'"";
    echo '</pre>';
 }
 $result = mysql_query("select * from test limit 4");
 while ($r = mysql_fetch_assoc($result)) {
    echo '
<form method="post">
 Id: '.$r['Id'].'
<input type="hidden" name="id" value="'.$r['Id'].'" />
<input type="text" name="test" value="'.$r['test'].'" />
<input type="submit" name="delete" value="Delete" />
</form>';
 }
Here we use the auto_incr. id's as a guide knowing what row we want to delete... Here I have deleted row 4 and 5, leaving the auto icreasing field 'broken'. But as I put in the id's fetched from the queries into hidden fields, they are not visible for the user, yet usable when sending the form.

Image

Hope that helps. Just one way of doing things.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Thanks for the code JAM, I guess I'm just gonna have to go learn some more stuff about sql.. :p
Post Reply