Jump to content
Stef's Coding Community
falkencreative

Basic Php System: View/edit/delete/add Records

Recommended Posts

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.webreference.com/programming/php/search/index.html, or do a web search for "php database search" or similar, and you should find some direction.

  • Upvote 1

Share this post


Link to post
Share on other sites

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/doc/refman/5.0/en/string-comparison-functions.html#operator_like), with "limit" for pagination (http://php.about.com/od/mysqlcommands/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.

Share this post


Link to post
Share on other sites

You'd need to use a SQL query that included "like", for search functionality (http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like), with "limit" for pagination (http://php.about.com/od/mysqlcommands/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

 

?>

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 ?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Hi all,

I have followed this tutorial and have a question,

I have a table with two rows

row 1 may not be altered row 2 may be changed

 

 

how do I get that row 1 not can not be changed but can be seen

 

 

sorry for my google translate.

 

Groeten Jan.

Share this post


Link to post
Share on other sites

Jan,

 

Probably the easiest way to handle that would be to add a new column in your database called "locked" or similar. It would contain either 1 (yes, the row is locked and cannot be changed), or 0 (no, the row isn't locked and is editable.) When you are creating your table and retrieving the data from the database, you use PHP to check the "locked" value, and choose to show or hide the editing controls based on that value.

 

Hope that helps get you started?

Share this post


Link to post
Share on other sites

Hi

 

I need bit of help on the edit.php page

 

I got the following errors come up and was just seeing if anyone could help with where I am going wrong please if ok

 

Warning: Missing argument 13 for renderForm(), called in /home/sites/broadwaymediadesigns.co.uk/public_html/sites/micromend/admin/edit.php on line 163 and defined in /home/sites/broadwaymediadesigns.co.uk/public_html/sites/micromend/admin/edit.php on line 26

 

Notice: Undefined variable: error in /home/sites/broadwaymediadesigns.co.uk/public_html/sites/micromend/admin/edit.php on line 41

 

Below is the coding from the edit.php page

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
?>

<?php
session_start();
    if(empty($_SESSION['loggedin']))
    {
        header('Location: http://' . $_SERVER['HTTP_HOST'] . '/admin/login.php');
        exit;
    }
 
    echo 'You will only see this if you are logged in.';
?>


<?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($visitor_id, $visitor_name, $visitor_name, $visitor_email, $visitor_firstline, $visitor_secondline, $visitor_town, $visitor_county, $visitor_postcode, $visitor_tel, $visitor_mobile, $visitor_newsletter, $error)
 {
 ?>
 
  <?php 

include ( 'includes/header.php' );

?>
<title>Admin edit customers data</title>
</head>
 <body>
 <div id='column-whole-inner'>
 <?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 $visitor_id; ?>"/>
 <div>
 <p><strong>Visitor ID:</strong> <?php echo $visitor_id; ?></p>
 <strong>Name: *</strong> <input type="text" name="visitor_name" value="<?php echo $visitor_name; ?>"/>
 <br/><br>
 <strong>Email: *</strong> <input type="text" name="visitor_email" value="<?php echo $visitor_email; ?>"/>
 <br/><br>
 <strong>Address Line 1: *</strong> <input type="text" name="visitor_firstline" value="<?php echo $visitor_firstline;?>"/>
 <br><br>
 <strong>Address Line 2: *</strong> <input type="text" name="visitor_secondline" value="<?php echo $visitor_secondline;?>"/>
 <br><br>
 <strong>Town: *</strong> <input type="text" name="visitor_town" value="<?php echo $visitor_town; ?>"/>
 <br><br>
 <strong>County: *</strong> <textarea name="visitor_county" rows="8" cols="30"><?php echo $visitor_county; ?></textarea>
 <br><br>
 <strong>Postcode: *</strong> <input type="text" name="visitor_postcode" value="<?php echo $visitor_postcode; ?>"/>
 <br><br>
 <strong>Telephone Number: *</strong> <input type="text" name="visitor_tel" value="<?php echo $visitor_tel; ?>"/>
 <br><br>
 <strong>Mobile Number: *</strong> <input type="text" name="visitor_mobile" value="<?php echo $visitor_mobile; ?>"/>
 <br>
 <label style="color: #FFFFFF;"><input type="radio" name="visitor_newsletter" value="Yes" <?php if($visitor_newsletter == Yes) echo 'checked="checked"'; ?> > Yes</label>
<br>
<label style="color: #FFFFFF;"><input type="radio" name="visitor_newsletter" value="No"<?php if($visitor_newsletter == No) echo 'checked="checked"'; ?> > No</label>
 <p>* Required</p>
 <input type="submit" name="submit" value="Submit">
 </div>
 </form> 
 </div>
 </body>
 </html> 
 <?php
 }



 // connect to the database
 include('config.php');
 
 // 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['visitor_id']))
 {
 // get form data, making sure it is valid
 $visitor_id = $_POST['visitor_id'];
 $visitor_name = mysql_real_escape_string(htmlspecialchars($_POST['visitor_name']));
 $visitor_email = mysql_real_escape_string(htmlspecialchars($_POST['visitor_email']));
 $visitor_firstline = mysql_real_escape_string(htmlspecialchars($_POST['visitor_firstline']));
 $visitor_secondline = mysql_real_escape_string(htmlspecialchars($_POST['visitor_secondline']));
 $visitor_town = mysql_real_escape_string(htmlspecialchars($_POST['visitor_town']));
 $visitor_county = mysql_real_escape_string(htmlspecialchars($_POST['visitor_county']));
 $visitor_postcode = mysql_real_escape_string(htmlspecialchars($_POST['visitor_postcode']));
 $visitor_tel = mysql_real_escape_string(htmlspecialchars($_POST['visitor_tel']));
 $visitor_mobile = mysql_real_escape_string(htmlspecialchars($_POST['visitor_mobile']));
 $visitor_newsletter = $_POST['visitor_newsletter'];
 
 // check that all fields are both filled in
 if ($visitor_name == '' || $visitor_email == '' || $visitor_firstline == '' || $visitor_secondline == '' || $visitor_town == '' || $visitor_county == '' || $visitor_postcode == '' || $visitor_tel == '' || $visitor_mobile == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';
 
 //error, display form
 renderForm($visitor_id, $visitor_name, $visitor_email, $visitor_firstline, $visitor_secondline, $visitor_town, $visitor_county, $visitor_postcode, $visitor_tel, $visitor_mobile, $visitor_newsletter, $error);
 }
 else
 {

 // save the data to the database
 mysql_query("UPDATE visitors SET visitor_name='$visitor_name', visitor_email='$visitor_email', visitor_firstline='$visitor_firstline', visitor_secondline='$secondline', visitor_town='$town', visitor_county='$visitor_county', visitor_postcode='$visitor_postcode', visitor_tel='$visitor_tel', visitor_mobile='$visitor_mobile', visitor_newsletter='$visitor_newsletter' WHERE visitor_id='$visitor_id'")
 or die(mysql_error());
 
 // once saved, redirect back to the view page
 header("Location: index.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['visitor_id']) && is_numeric($_GET['visitor_id']) && $_GET['visitor_id'] > 0)
 {
 // query db
 $visitor_id = $_GET['visitor_id'];
 $result = mysql_query("SELECT * FROM visitors WHERE visitor_id=$visitor_id")
 or die(mysql_error()); 
 $row = mysql_fetch_array($result);
 
 // check that the 'id' matches up with a row in the databse
 if($row)
 {
 
 // get data from db
 $visitor_id = $row['visitor_id'];
 $visitor_name = $row['visitor_name'];
 $visitor_email = $row['visitor_email'];
 $visitor_firstline = $row['visitor_firstline'];
 $visitor_secondline = $row['visitor_secondline'];
 $visitor_town = $row['visitor_town'];
 $visitor_county = $row['visitor_county'];
 $visitor_postcode = $row['visitor_postcode'];
 $visitor_tel = $row['visitor_tel'];
 $visitor_mobile = $row['visitor_mobile'];
 $visitor_newsletter = $row['visitor_newsletter'];
 
 // show form
 renderForm($visitor_id, $visitor_name, $visitor_email, $visitor_firstline, $visitor_secondline, $visitor_town, $visitor_county, $visitor_postcode, $visitor_tel, $visitor_mobile, $visitor_newsletter, '');
 }
 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!';
 }
 }
?>

Share this post


Link to post
Share on other sites

The error message is telling you the answer:

 

Undefined variable

 

... Basically, you are trying to use a variable that PHP cannot find. You need to be sure your variable names are correct or perhaps you have to declare the variable to begin with. Finally, it might be a scope issue - that means you may have declared the variable but it is in a place in your code that puts it out of reach of the code that is trying to use the variable.

 

Stef

Share this post


Link to post
Share on other sites


Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /opt/lampp/htdocs/p4/new.php on line 97

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /opt/lampp/htdocs/p4/new.php on line 99

ERROR: Please fill in all required fields!

Share this post


Link to post
Share on other sites

Hi i am using your code in edit.php but i get blank page, i have tried to change some parts of your code but nothing happends, could you please help me?

thanks

here is the code in edit.php

<?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, $firstname, $lastname, $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>First Name: *</strong> <input type="text" name="firstname" value="<?php echo $firstname; ?>"/><br/>

<strong>Last Name: *</strong> <input type="text" name="lastname" value="<?php echo $lastname; ?>"/><br/>

<p>* Required</p>

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

</div>

</form>

</body>

</html>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...