How to Customized Data in PHP

Deleting rows  for Customized Data in PHP

So far we have only entered new information in our database and viewed it to Customized Data in PHP. Where’s the fun if we can’t trash some of those data, at least the useless ones? Our delete.php will do just that. It works exactly like view.php. The only difference is the SQL command “DELETE FROM personnel WHERE id=$id”, which tell MySQL to delete the row that contains the id corresponding to the variable $id. Generally, the SQL command for deleting a row is DELETE FROM database_name WHERE field_name=somevalue for Customized Data in PHP.

<HTML>  <?php  $db = mysql_connect("localhost", "root", "");  mysql_select_db("learndb",$db);  mysql_query("DELETE FROM personnel WHERE id=$id",$db);  echo "Information Deleted";  ?>  </HTML>

Once again we modify our previous viewdb2.php script How to  viewdb3.php to add this new feature  Customized Data in PHP. The additions should be obvious.

<HTML>  <?php  $db = mysql_connect("localhost", "root", "");  mysql_select_db("learndb",$db);  $result = mysql_query("SELECT * FROM personnel",$db);  echo "<TABLE BORDER=2>";  echo"<TR><TD><B>Full Name</B><TD><B>Nick Name</B><TD><B>Options</B></TR>";  while ($myrow = mysql_fetch_array($result))  {  echo "<TR><TD>".$myrow["firstname"]." ".$myrow["lastname"]."</a><TD>".$myrow["nick"];  echo "<TD><a href=\"view.php?id=".$myrow[id]."\">View</a> ";  echo "<a href=\"delete.php?id=".$myrow[id]."\">Delete</a>";  }  echo "</TABLE>";  ?>  </HTML>

Try clicking on delete and then view the database again with viewdb3.php to verify that the row was really deleted. You may have to refresh your browser for How to Customized Data in PHP.

Editing data to Customized Data in PHP

So far we have viewed and deleted database content. But sometimes we need to edit database content to Customized Data in PHP. For this we will modify our previously coded input.php file. By now you are familiar with the concept of passing variables by URL. We will call this modified script addedit.php:

Customized Data in PHP

<HTML>  <?php  if($submit)  {  $db = mysql_connect("localhost", "root","");  mysql_select_db("learndb",$db);  $sql = "INSERT INTO personnel (firstname, lastname, nick, email, salary)  VALUES ('$first','$last','$nickname','$email','$salary')";  $result = mysql_query($sql);  echo "Thank you! Information entered.\n";  }  else if($update)  {  $db = mysql_connect("localhost", "root","");  mysql_select_db("learndb",$db);  $sql = "UPDATE personnel SET firstname='$first',  lastname='$last',nick='$nickname',email='$email',  salary='$salary' WHERE id=$id";  $result = mysql_query($sql);  echo "Thank you! Information updated.\n";  }  else if($id)  {  $db = mysql_connect("localhost", "root", "");  mysql_select_db("learndb",$db);  $result = mysql_query("SELECT * FROM personnel WHERE id=$id",$db);  $myrow = mysql_fetch_array($result);  ?>  <form method="post" action="<?php echo $PHP_SELF?>">  <input type="hidden" name="id" value="<?php echo $myrow["id"]?>">  First name:<input type="Text" name="first"  value="<?php echo $myrow["firstname"]?>"><br>  Last name:<input type="Text" name="last"  value="<?php echo $myrow["lastname"]?>"><br>  Nick Name:<input type="Text" name="nickname"  value="<?php echo $myrow["nick"]?>"><br>  E-mail:<input type="Text" name="email" value="<?php echo $myrow["email"]?>"><br>  Salary:<input type="Text" name="salary" value="<?php echo $myrow["salary"]?>"><br>  <input type="Submit" name="update" value="Update information"></form>  <?  }  else  {  ?>  <form method="post" action="<?php echo $PHP_SELF?>">  First name:<input type="Text" name="first"><br>  Last name:<input type="Text" name="last"><br>  Nick Name:<input type="Text" name="nickname"><br>  E-mail:<input type="Text" name="email"><br>  Salary:<input type="Text" name="salary"><br>  developerWorks® ibm.com/developerWorks  Creating dynamic Web sites with PHP and MySQL Trademarks  © Copyright IBM Corporation 2001. All rights reserved. Page 14 of 20  <input type="Submit" name="submit" value="Enter information"></form>  <?  }  ?>  </HTML>

Hmmm…the code looks quite complex. But really it isn’t. Previously input.php had two features: it could add information to the database or could show the form. We’ll add two more features to it: the ability to show the same form but with values of a particular person already there and the ability to update records for that person.  For Customized Data in PHP The SQL commands for entering new information and updating existing information are different, so we can’t use our previous code for entering information. The script searches for the $submit variable. If it contains some value, then someone submitted new data and the information is entered into the database. If $submit does not contain any value, then someone might have just posted their updated information, so we check $update for How to Customized Data in PHP. If it contains a value, then we update that person’s record with the SQL statement “UPDATE personnel SET

fieldname1=’$variablename1′,fieldname2=’$variablename2′ ……… WHERE id=$id”;”. Otherwise, if someone provided the id in the query string, we show that person’s information, but this time in a form so he may change it. If all these are not the case, we simply have to show the old form. Experiment with the script. Open it with your browser to see what comes up. Then call it providing query string ?id=1. Change the information and click update. Verify whether the database is updated by viewing the database with viewdb3.php. Another new element was just introduced. It is the global PHP variable $PHP_SELF. To Customized Data in PHP This variable always contains the name of the script it is in and its location. We have used this variable in a ‘form action’ so no matter what you name this file, this script will always post information to itself to How to Customized Data in PHP. Once again we modify our viewing script incorporating this feature. Here’s the listing for viewdb4.php:

Customized Data in PHP

<HTML>  <?php  $db = mysql_connect("localhost", "root", "");  mysql_select_db("learndb",$db);  $result = mysql_query("SELECT * FROM personnel",$db);  echo "<TABLE BORDER=2>";  echo"<TR><TD><B>Full Name</B><TD><B>Nick Name</B><TD><B>Options</B></TR>";  while ($myrow = mysql_fetch_array($result))  {  echo "<TR><TD>".$myrow["firstname"]."  ".$myrow["lastname"]."</a><TD>".$myrow["nick"];  echo "<TD><a href=\"view.php?id=".$myrow[id]."\">View</a> ";  echo "<a href=\"delete.php?id=".$myrow[id]."\">Delete</a> ";  echo "<a href=\"addedit.php?id=".$myrow[id]."\">Edit</a>";  }  echo "</TABLE>";  ibm.com/developerWorks developerWorks®  Creating dynamic Web sites with PHP and MySQL Trademarks  © Copyright IBM Corporation 2001. All rights reserved. Page 15 of 20  ?>  </HTML>

Searching our data to Customized Data in PHP

Information is useless if you can’t find the data you require from a wealth of information. We need a way to search our database, so let’s implement a search function. The page will show a static form initially and will show the search result when we have something submitted to How to Customized Data in PHP.

Customized Data in PHP

<HTML>  <?php  if ($searchstring)  {  $sql="SELECT * FROM personnel WHERE $searchtype  LIKE '%$searchstring%' ORDER BY firstname ASC";  $db = mysql_connect("localhost", "root", "");  mysql_select_db("learndb",$db);  $result = mysql_query($sql,$db);  echo "<TABLE BORDER=2>";  echo"<TR><TD><B>Full Name</B><TD><B>Nick Name</B><TD><B>Options</B></TR>";  while ($myrow = mysql_fetch_array($result))  {  echo "<TR><TD>".$myrow["firstname"]." ".$myrow["lastname"]."<TD>".$myrow["nick"];  echo "<TD><a href=\"view.php?id=".$myrow["id"]."\">View</a>";  }  echo "</TABLE>";  }  else  {  ?>  <form method="POST" action="<?php $PHP_SELF ?>">  <table border="2" cellspacing="2">  <tr><td>Insert you search string here</td>  <td>Search type</td></tr>  <tr>  <td><input type="text" name="searchstring" size="28"></td>  <td><select size="1" name="searchtype">  <option selected value="firstname">First Name</option>  <option value="lastname">Last Name</option>  <option value="nick">Nick Name</option>  <option value="email">Email</option>  </select></td>  </tr>  </table>  <p><input type="submit" value="Submit" name="B1">  <input type="reset" value="Reset" name="B2"></p>  </form>  <?php  }  ?>  </HTML>

The script checks whether a search string exists. If $searchstring contains a value, then we have something to search; otherwise, we just show an HTML form. The part of code that searches is similar to our viewdb2.php. The SQL command deserves a bit of explanation here. Let’s look at it closely. The SQL command is to Customized Data in PHP:

"SELECT * FROM personnel WHERE $searchtype LIKE '%$searchstring%' ORDER BY firstname ASC"

if we Customized Data in PHP ,Two news things are introduced here, “LIKE” and “ORDER BY”. LIKE simply means ‘sounds like’. The ‘%’ sign represents any possible combination of characters (numbers or letters). So to find people whose first name starts with ‘J’ we would use the SQL command “SELECT * FROM personnel WHERE firstname LIKE ‘J%'” To find those people with a name ending with J we have to use ‘%J’. If we wish find people with ‘J’ anywhere in their name (first, middle, or last) we have to use ‘%J%’. ‘ORDER BY’ simply orders the records in ascending or descending order. The syntax is: “ORDER BY fieldname order_method” where order_method is ASC or DESC allowing the ordering to be done in ASCending or DESCending order here to How to Customized Data in PHP.

andcombd