Friday, July 3, 2009

searching and updating a database

search.html is the search form. passes the data to dbsearch.php
dbsearch.php  searches the database and presents the found records. passes the records to view.php for viewing or editinfo.php to edit the record.
dbsearch.php takes the record Id, adds it to a link and passes it thus to the required programs.
For a full explanation of the programs editinfo and update info, check it’s source


---search.html-------
<body>     
<link href="style.css" type="text/css" rel="stylesheet" />     
<link rel="stylesheet" type="text/css" href="style.css" /> 
<form action="dbsearch.php" method="get">    
Keywoord uit Acteur of Titel: <input type="text" name="q" />     
<br>    
<input type="submit" />     
</form>     
<body> 
------dbsearch.php----------------
<?php     
    include 'db.php';    // Include the database     
    // Set the page, use one if the get page is not a number or is negative     
    if(!isset($_GET['page']) || !ctype_digit($_GET['page']))     
        $page = 1;     
    else     
        $page = $_GET['page'];     
    // Set the maximum number of results     
    $max = 10;     
    // Set the start location (when viewing the next page)     
    $limit = ($page * $max) - $max;     
    $q = trim(mysql_real_escape_string($_GET['q'])); // Make a safe string     
    // Make a query, (change "code" and "name" to your column names)     
    $query = "SELECT SQL_CALC_FOUND_ROWS *,     
            MATCH(Titel) AGAINST ('$q' IN BOOLEAN mode) AS score1,     
            MATCH(Acteur) AGAINST ('$q' IN BOOLEAN mode) AS score2     
            FROM DVD     
            WHERE  
            MATCH(Titel,Acteur) AGAINST ('$q' IN BOOLEAN mode)     
            ORDER BY score1 DESC, score2 DESC LIMIT $limit, $max";     
    // Perform the query     
    $sql = mysql_query($query); 
    // Find how many results would have been returned if there wasn't a limit     $result_count = mysql_query("SELECT FOUND_ROWS()")or die(mysql_error());     // Get the number     $total = mysql_fetch_array($result_count);     // Search the array for the total     $totalrows = $total[0];     // Calculate the number of pages, if it is a decimal, then there are     // more reusults, but that number is less than our $max (total number of results     // to display on the page)     $pages = ceil($totalrows / $max);     // Display the results...     if(mysql_num_rows($sql) > 0){         echo '<p>Found <b>'.$totalrows.'</b> results for <b>"'.htmlentities($_GET['q']).'"</b></p>';         $i = $limit + 1;         while($row = mysql_fetch_array($sql)){             echo '<p>'.$i.'. <a href="view.php?id='.$row['x'].'">'.$row['Titel'].'</a>';             echo ' <b ><a href="editinfo.php?id='.$row['x'].'">Edit Data</a></b></p>';             $i++;         }     }else{         // No results were found         echo '<h2>No Results Found!</h2>';     }     // Display the page numbers (if there is more than one page)     if($pages > 1){         echo '<div style="padding:10px;">';             for($i = 1;$i<$pages+1;$i++){                 if($i == $page)                     echo '<span class="page" style="padding:10px;">'.$i.'</span>';                 else                     echo '<a style="padding:10px;" href="'.$_SERVER['PHP_SELF'].'?q='.$_GET['q'].'&amp;page='.$i.'">'.$i.'</a>';             }             echo '<span style="clear:both;display:block;font-size:1px;">&nbsp;</span>';         echo '</div>';     } // Sluit de database echo mysql_error(); mysql_close($db); ?>
-------view.php-------------------------
<?     
$id = trim(mysql_real_escape_string($_GET['id'])); // Make a safe string     
//echo $id;     
include 'db.php';    // Include the database     
$query=("SELECT * FROM DVD WHERE x=$id ORDER BY Titel ASC ")  or die (mysql_error());     
//echo $query;     
$result=mysql_query($query);     
//echo $result;     
$row = mysql_fetch_array($result);     
$x= $row[0];     
$titel= $row[1];     
$acteur = $row[2];     
$taal= $row[3];     
$jaar= $row[4];     
$imdb = $row[5];     
$summary=$row[6];     
$foto=$row[7];     
?> 
<div class="example1">    
<?php     
echo '<b>';     
echo $titel;     
echo '</b><br /><em>';     
echo $acteur;     
echo '</em><br />';     
echo $taal;     
echo '<br />';     
echo $jaar;     
echo '<br />';     
echo "<a href='{$row['IMDB']}'>$titel at imdb</a>";     
echo '<br />';     
echo $summary;     
echo '<br />';     
echo "<img src='{$row['Foto']}' >"; 
// Sluit de database    
echo mysql_error();     
mysql_close($db); 
?>    
</div> 
-------------- editinfo.php---------
This file is a slight alteration from the file found Here
<?php    
$id = trim(mysql_real_escape_string($_GET['id'])); // Make a safe string     
//replace usernaem,password, and yourdb with the information for your database     
mysql_connect("localhost","zz","zz") or die("Error: ".mysqlerror());     
mysql_select_db("DVD"); 
//replace TestTable with the name of your table    
//also in a real app you would get the id dynamically     
$sql = "select * from `DVD` where x = $id";     
$query = mysql_query($sql); 
while ($row = mysql_fetch_array($query)){ 
    $id = $row['x'];    
    $titel = $row['Titel'];     
    $acteur = $row['Acteur'];  
    $taal = $row['Taal'];     
    $jaar = $row['Jaar'];     
    $imdb = $row['IMDB'];  
    $summary =  $row['Summary'];     
    $foto = $row['Foto']; 
    //we will echo these into the proper fields 
}    
mysql_free_result($query);     
?> 
<html>    
<head>     
<title>Edit DVD Info</title>     
</head> 
<body> 
<form action="updateinfo.php" method="post"> 
id:<br/>    
<input type="text" value="<?php echo $id;?>" name="x" disabled />     
<input type="hidden" value="<?php echo $id;?>" name="x"  />     
<br/> 
-------------updateinfo.php-----------
see here for full description
Titel:<br/>     
<input type="text" value="<?php echo $titel;?>" name="Titel" size=60/> 
<br/> 
Acteur:<br/>    
<input type="text" value="<?php echo $acteur;?>" name="Acteur" size=60/> 
<br/> 
Taal:<br/>    
<input type="text" value="<?php echo $taal;?>" name="Taal"/> 
<br/> 
Jaar:<br/>    
<input type="text" value="<?php echo $jaar;?>" name="Jaar" size=10/> 
<br/> 
IMDB:<br/>    
<input type="text" value="<?php echo $imdb;?>" name="IMDB" size=38/> 
<br/>    
Summary:<br/>     

<TEXTAREA NAME="Summary"  ROWS=6 COLS=45>     
<?php echo $summary;?>     
</TEXTAREA>     
<br/> 
Foto locatie:<br/>    
<input type="text" value="<?php echo $foto;?>" name="Foto" size=59/> 
</br> 
<input type="submit" value="submit changes"/> 
</form>    
</body>     
</html>

5 comments:

  1. Just a warning. This code is an example and is taken from working code. In using it you need to of course put your own settings in. I have used an "include db.php" statement several times to make the connection to the database. This is quite standard. You of course need to make your own db.php file, if you don't already have one, or make the connection in the listing above.

    ReplyDelete
  2. Hey It's me Dino, Im sorry about the late reply but my house is a mess as they are painting the walls so I had some internet connection difficulties. As for line 40 I gave u the code on the other page, and I have the connection file with it, that works cos Its the same as on some of my other php pages and it connects to the database on those.

    ReplyDelete
  3. I need to think this over coz it all points to your sql not being correct

    ReplyDelete
  4. Ok I dunno how to give u my whole code and Im not claiming its impossible my code is wrong, but I did check it and it seems ok to me. If u figure out a way for me to post the whole code let me know

    ReplyDelete
  5. If your code is OK there should not be an error message.
    Having said that. You do not really need that code as I understand you already have code that is working for you and the only thing you then have to follow is the same principle that I use, to add the Id to a link that brings you to the edit program.

    ReplyDelete