Jump to content


Photo

Basic PHP System: View/Edit/Delete/Add Records


  • Please log in to reply
188 replies to this topic

#181 Ben

Ben

    Administrator

  • Administrators
  • 5,556 posts
  • LocationChico, CA

Posted 18 December 2013 - 11:30 AM

Problem is when i click on edit file in view-page.php it shows all the data as "Array"


Check this section of your code, and compare it with my code -- specifically, the "get data from db" section.

// check that the 'id' matches up with a row in the databse
 if($row)
 {
 
 // get data from db
        $Volunteer=['Volunteer'];

My impression is that these lines: $Volunteer=['Volunteer']; need to be something like this: $Volunteer=$row['Volunteer']; (Check my code -- this is just off the top of my head.) The way you currently have it, you are passing in an array element, not giving those variables values from the database.

As for search, that's a little outside the scope of the tutorial. You might want to check out http://www.webrefere...arch/index.html, or do a web search for "php database search" or similar, and you should find some direction.
  • 1
Benjamin Falk
Falken Creative : Twitter

#182 farah

farah

    New member

  • New Members
  • 3 posts

Posted 19 December 2013 - 09:06 PM

how do combine function "search" and "view-paginated"??
  • 0

#183 Ben

Ben

    Administrator

  • Administrators
  • 5,556 posts
  • LocationChico, CA

Posted 20 December 2013 - 08:02 AM

how do combine function "search" and "view-paginated"??

You'd need to use a SQL query that included "like", for search functionality (http://dev.mysql.com...l#operator_like), with "limit" for pagination (http://php.about.com...g/Limit_sql.htm). Basically, you'd use the exact same pagination code you already have, just change the query to search using "like". See my last comment for direction regarding search.
  • 0
Benjamin Falk
Falken Creative : Twitter

#184 farah

farah

    New member

  • New Members
  • 3 posts

Posted 22 December 2013 - 09:28 PM

You'd need to use a SQL query that included "like", for search functionality (http://dev.mysql.com...l#operator_like), with "limit" for pagination (http://php.about.com...g/Limit_sql.htm). Basically, you'd use the exact same pagination code you already have, just change the query to search using "like". See my last comment for direction regarding search.


can you see my coding???i dnt knw how to use and where to put "limit" in my coding...

<?php

// connect to the database
mysql_connect("localhost","root","");
mysql_select_db("hospital");

// number of results to show per page
$per_page = 10;

// figure out the total pages in the database
if(isset($_POST['searchvalue2'])){
$searchtype=$_POST['searchtype2'];
$searchvalue=$_POST['searchvalue2'];

$result = mysql_query("SELECT patientname,RN,IC,age,gender,race,discipline,dateofincident,timeofincident,dateofreport,monthofreport,namastaff,brief,action,status,namelocation,category
FROM registerstaff,location,incident WHERE $searchtype LIKE '%$searchvalue%' and location.id=registerstaff.location_id and incident.id=registerstaff.incident_id";
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);

// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];

// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}

// display pagination

echo "<p><a href='view.php'>View All</a> | <b>View Page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='view-paginated.php?page=$i'>$i</a> ";
}
echo "</p>";

// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Patient's Name</th> <th>RN Number</th> <th>IC Number</th> <th>Age</th> <th>Gender</th> <th>Race</th> <th>Location</th><th>Discipline</th> <th>Date of Incident </th> <th>Time of Incident</th> <th>Date of Report</th> <th>Month of Report</th> <th>Staff Report</th><th>Incident Issue</th> <th>Brief</th> <th>Action</th> <th>Status</th></tr>";

// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
//$result = mysql_query($sqlStatement) or die(mysql_error());
//$count = mysql_num_rows($result) or die(mysql_error());

{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
//$result = mysql_query($sqlStatement) or die(mysql_error());
//$count = mysql_num_rows($result) or die(mysql_error());


// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . mysql_result($result, $i, 'patientname') . '</td>';
echo '<td>' . mysql_result($result, $i, 'RN') . '</td>';
echo '<td>' . mysql_result($result, $i, 'IC') . '</td>';
echo '<td>' . mysql_result($result, $i, 'age') . '</td>';
echo '<td>' . mysql_result($result, $i, 'gender') . '</td>';
echo '<td>' . mysql_result($result, $i, 'race') . '</td>';
echo '<td>' . mysql_result($result, $i, 'location_id') . '</td>';
echo '<td>' . mysql_result($result, $i, 'discipline') . '</td>';
echo '<td>' . mysql_result($result, $i, 'dateofincident') . '</td>';
echo '<td>' . mysql_result($result, $i, 'timeofincident') . '</td>';
echo '<td>' . mysql_result($result, $i, 'dateofreport') . '</td>';
echo '<td>' . mysql_result($result, $i, 'monthofreport') . '</td>';
echo '<td>' . mysql_result($result, $i, 'namastaff') . '</td>';
echo '<td>' . mysql_result($result, $i, 'incident_id') . '</td>';
echo '<td>' . mysql_result($result, $i, 'brief') . '</td>';
echo '<td>' . mysql_result($result, $i, 'action') . '</td>';
echo '<td>' . mysql_result($result, $i, 'status') . '</td>';
// echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
// echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
echo "</tr>";
}
// close table>
echo "</table>";

// pagination

?>
  • 0

#185 Ben

Ben

    Administrator

  • Administrators
  • 5,556 posts
  • LocationChico, CA

Posted 22 December 2013 - 10:12 PM

You need to update your SQL query:

$result = mysql_query("SELECT patientname,RN,IC,age,gender,race,discipline,dateofincident,timeofincident,dateofreport,monthofreport,namastaff,brief,action,status,namelocation,category
FROM registerstaff,location,incident WHERE $searchtype LIKE '%$searchvalue%' and location.id=registerstaff.location_id and incident.id=registerstaff.incident_id";

to include "limit". Take a look at my SQL query from my pagination example code and compare the two.
  • 0
Benjamin Falk
Falken Creative : Twitter

#186 farah

farah

    New member

  • New Members
  • 3 posts

Posted 23 December 2013 - 12:08 AM

You need to update your SQL query:

$result = mysql_query("SELECT patientname,RN,IC,age,gender,race,discipline,dateofincident,timeofincident,dateofreport,monthofreport,namastaff,brief,action,status,namelocation,category
FROM registerstaff,location,incident WHERE $searchtype LIKE '%$searchvalue%' and location.id=registerstaff.location_id and incident.id=registerstaff.incident_id";

to include "limit". Take a look at my SQL query from my pagination example code and compare the two.


Thank you very much for your time and reading my question..thank you =D
  • 0

#187 Lathisya

Lathisya

    New member

  • New Members
  • 1 posts

Posted 03 March 2014 - 10:11 AM

Hai... I am new on PHP & Mysql,,,

please help me, I'm trying to follow this tutorial using xampp, and I get errors like this;

Warning: mysqli_real_escape_string () expects exactly 2 parameters, 1 given in C: \ xampp \ htdocs \ go \ edit.php on line 62

Deprecated: mysql_real_escape_string (): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C: \ xampp \ htdocs \ go \ edit.php on line 63

and code

<?php
/* 
 EDIT.PHP
 Allows user to edit specific entry in database
*/

 // creates the edit record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($id, $date, $website1, $linkwebsite1, $website2, $linkwebsite2, $error)
 {
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
 <title>Edit Record</title>
 </head>
 <body>
 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 
 
 <form action="" method="post">
 <input type="hidden" name="id" value="<?php echo $id; ?>"/>
 <div>
 <p><strong>ID:</strong> <?php echo $id; ?></p>
 <strong>Date *: </strong> <input type="text" name="date" value="<?php echo $date; ?>"/><br/>
 <strong>website 1 *: </strong> <input type="text" name="website1" value="<?php echo $website1; ?>"/><br/>
 <strong>Link to website 1 *: </strong> <input type="text" name="linkwebsite1" value="<?php echo $linkwebsite1; ?>"/><br/>
 <strong>website 2 *: </strong> <input type="text" name="website2" value="<?php echo $website2; ?>"/><br/>
 <strong>Link to website 2 *: </strong> <input type="text" name="linkwebsite2" value="<?php echo $linkwebsite2; ?>"/><br/>
 <p>* Required</p>
 <input type="submit" name="submit" value="Submit">
 </div>
 </form> 
 </body>
 </html> 
 <?php
 }



 // connect to the database
 $con=mysqli_connect("localhost","root","","buat");
 // Connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
 
 // check if the form has been submitted. If it has, process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // confirm that the 'id' value is a valid integer before getting the form data
 if (is_numeric($_POST['id']))
 {
 // get form data, making sure it is valid
 $id = $_POST['id'];
 $date = mysqli_real_escape_string(htmlspecialchars($_POST['date']));
 $website1 = mysql_real_escape_string(htmlspecialchars($_POST['website1']));
 $linkwebsite1 = mysql_real_escape_string(htmlspecialchars($_POST['linkwebsite1']));
 $website2 = mysql_real_escape_string(htmlspecialchars($_POST['website2']));
 $linkwebsite2 = mysql_real_escape_string(htmlspecialchars($_POST['linkwebsite2']));
 
 // check that firstname/lastname fields are both filled in
 if ($date == '' || $website1 == '' || $linkwebsite1 == '' || $website2 == '' || $linkwebsite2 == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';
 
 //error, display form
 renderForm($id, $date, $website1, $linkwebsite1, $website2, $linkwebsite2, $error);
 }
 else
 {
 // save the data to the database
 mysqli_query($con,"UPDATE proof SET date='$date', website1='$website1', linkwebsite1='$linkwebsite1', website2='$website2', linkwebsite2='$linkwebsite2' WHERE id='$id'")
 or die(mysqli_connect_error()); 
 
 // once saved, redirect back to the view page
 header("Location: edit.php"); 
 }
 }
 else
 {
 // if the 'id' isn't valid, display an error
 echo 'Error!';
 }
 }
 else
 // if the form hasn't been submitted, get the data from the db and display the form
 {
 
 // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
 if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
 {
 // query db
 $id = $_GET['id'];
 $result = mysqli_query($con,"SELECT * FROM site WHERE id=$id")
 or die(mysql_error()); 
 $row = mysqli_fetch_array($result);
 
 // check that the 'id' matches up with a row in the databse
 if($row)
 {
 
 // get data from db
 $date = $row['date'];
 $website1 = $row['website1'];
 $linkwebsite1 = $row['linkwebsite1'];
 $website2 = $row['website2'];
 $linkwebsite2 = $row['linkwebsite2'];
 
 // show form
 renderForm($id, $date, $website1, $linkwebsite1, $website2, $linkwebsite2, '');
 }
 else
 // if no match, display result
 {
 echo "No results!";
 }
 }
 else
 // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
 {
 echo 'Error!';
 }
 }
?>

and how to remove Required form and view-paginated.php ?
  • 0

#188 hassanzaz

hassanzaz

    New member

  • New Members
  • 1 posts

Posted 06 March 2014 - 04:02 AM

This is what I get and its not saving in the database

ERROR: Could not prepare SQL statement.
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\pension\records.php:175) in C:\xampp\htdocs\pension\records.php on line 179
  • 0

#189 Stefan

Stefan

    Stefan Mischook

  • Administrators
  • 4,164 posts
  • LocationMontreal Canada

Posted 26 March 2014 - 10:17 AM

An easy way to do it, is to use JavaScript to trap for a click event (onClick) and ask for a confirmation there via a simple alert function call.

 

Stef


  • 0
StudioWeb makes teaching web design and programming easy: StudioWeb




8 user(s) are reading this topic

0 members, 8 guests, 0 anonymous users