Jump to content

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


falkencreative

Recommended Posts

The point of the is_numeric() line is to ensure that someone isn't trying to mess with the website by inputting code into the URL. However, if the value you are expecting in the URL that you are using isn't numeric, then you will need to remove that if statement.

Hi Ben,

THANKS FOR YOUR REPLY... :)

what and how much portion is to be removed?? plz be specific... i am newbie... when i remove if loop it gives me blank page when i click on edit.... or tell me what is to be written in place of is_numeric... if i want user to be enter characters only..

P.S i removed

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

{

// confirm that the 'id' value is a valid integer before getting the form data

if (is_string($_POST['signum']))

{

// get form data, making sure it is valid

$signum =$_POST['signum'];

$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));

$emp_id = mysql_real_escape_string(htmlspecialchars($_POST['emp_id']));

 

// check that firstname/lastname fields are both filled in

if ($name == '' || $emp_id == '')

{

// generate error message

$error = 'ERROR: Please fill in all required fields!';

 

//error, display form

renderForm($signum, $name, $emp_id, $error);

}

else

{

// save the data to the database

mysql_query("UPDATE EMP SET name='$name', emp_id='$emp_id' WHERE signum='$signum'")

or die(mysql_error());

 

// once saved, redirect back to the view page

header("Location: view.php");

}

}

else

{

// if the 'id' isn't valid, display an error

echo 'Error!';

}

}

 

PLEASE EDIT THE SCRIPT FOR ME AND PASTE IT OVER HERE IF POSSIBLE...

Link to comment
Share on other sites

Hi Ben and thanks for this awsome guide!

 

I'm trying to create a dynamic table based on your example where the only real difference is that I want to have a different kind of PRIMARY KEY. In other words I want my site visitors to be able to see and edit the primary key which will be in text form (varchar). What kind of changes should I do in the code?

 

Thanks again

Albersson

Link to comment
Share on other sites

The point of the is_numeric() line is to ensure that someone isn't trying to mess with the website by inputting code into the URL. However, if the value you are expecting in the URL that you are using isn't numeric, then you will need to remove that if statement.

 

Thankyou Thankyou Thankyou... i am done with it.. thanks alot Ben... :*

Link to comment
Share on other sites

  • 5 weeks later...

great info.thanks for this.

i have some other questions?

 

how to view only one row data?

 

Example:- Add a VIEW link after EDIT and DELETE. when click on view show only that id values. (with code) thanks

It would be nearly identical to the edit.php page, except that you wouldn't need to put the record's values in a form for editing purposes, and you won't need code to process the form and update the record.

Link to comment
Share on other sites

  • 3 weeks later...

hi...

Am a beginner in PHP. I have question, If the table(in the above post it is players) is to be selected from the database(for ex:technologies). Then i need a solution where user select from the different tables and that table is displayed. Please help....

Link to comment
Share on other sites

hi...

Am a beginner in PHP. I have question, If the table(in the above post it is players) is to be selected from the database(for ex:technologies). Then i need a solution where user select from the different tables and that table is displayed. Please help....

I think you'll need to explain what you need a bit more before I can help you.

Link to comment
Share on other sites

hey awsm work....it really helped me a lot...but in this i m facing one problem in edit.php....In my form i have used drop down list. I am not able to retrieve the result of that drop down list while editing the form. When i redirected to edit.php the fields having drop down list show first option in drop down list instead of showing selecting option while adding data...plsss help me in this....should i post my code?

Link to comment
Share on other sites

hey awsm work....it really helped me a lot...but in this i m facing one problem in edit.php....In my form i have used drop down list. I am not able to retrieve the result of that drop down list while editing the form. When i redirected to edit.php the fields having drop down list show first option in drop down list instead of showing selecting option while adding data...plsss help me in this....should i post my code?

Yes. Create a new topic and post your question/the code you are working with.

Link to comment
Share on other sites

Hi Ben, first of all great thanks to you for this script and it is going to help me a lot at work. I was actually setting it up with our own database and added all the row names and so on. Editing, adding, viewing results works just fine. But the only problem I dealing with is when I try to delete any existing record. I believe that whenever I change

 

DELETE FROM content WHERE id_number=$id_number

 

in delete.php file ($id_number is the row in my custom database so had to change it), it shows different error each time. Right now if I have it like that then I get an error saying

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

But then If I change mysql_query to

 

DELETE FROM content WHERE id_number='$id_number'

 

then it will not delete that file but will redirect me to listed location. Would you please help me figure out the problem?

Here is the whole delete.php here if you need to go through it:

 

<?php
/* 
DELETE.PHP
Deletes a specific entry from the 'content' table
*/

// connect to the database
include('connect-db.php');

// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['id_number']) && is_numeric($_GET['id_number']))
{
// get id value
$id = $_GET['id_number'];

// delete the entry
$result = mysql_query("DELETE FROM content WHERE id_number='$id_number'")
or die(mysql_error()); 

// redirect back to the view page
header("Location: view-paginated.php?page=4");
}
else
// if id isn't set, or isn't valid, redirect back to view page
{
header("Location: view-paginated.php?page=4");
}

?>

 

Thank you in advance Mr. Falk.

Link to comment
Share on other sites

Hi Ben, first of all great thanks to you for this script and it is going to help me a lot at work. I was actually setting it up with our own database and added all the row names and so on. Editing, adding, viewing results works just fine. But the only problem I dealing with is when I try to delete any existing record. I believe that whenever I change

 

DELETE FROM content WHERE id_number=$id_number

 

in delete.php file ($id_number is the row in my custom database so had to change it), it shows different error each time. Right now if I have it like that then I get an error saying

 

 

 

But then If I change mysql_query to

 

DELETE FROM content WHERE id_number='$id_number'

 

then it will not delete that file but will redirect me to listed location. Would you please help me figure out the problem?

Here is the whole delete.php here if you need to go through it:

 

<?php
/* 
DELETE.PHP
Deletes a specific entry from the 'content' table
*/

// connect to the database
include('connect-db.php');

// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['id_number']) && is_numeric($_GET['id_number']))
{
// get id value
$id = $_GET['id_number'];

// delete the entry
$result = mysql_query("DELETE FROM content WHERE id_number='$id_number'")
or die(mysql_error()); 

// redirect back to the view page
header("Location: view-paginated.php?page=4");
}
else
// if id isn't set, or isn't valid, redirect back to view page
{
header("Location: view-paginated.php?page=4");
}

?>

 

Thank you in advance Mr. Falk.

 

 

I don't actually see $id_number defined in your code on that file. Are you sure you don't mean $id instead? Because I see:

$id = $_GET['id'];

Link to comment
Share on other sites

  • 4 weeks later...

Hello,

 

Great work i am really impress with your work. This should help those wishing to learn some php. It is also a good start to the Admin side of small applications.

Link to comment
Share on other sites

Will you add new features?

 

1)Check/Uncheck all checkboxes based on one check box to delete multiple records and press the delete button (I mean only one button).

2)Check on the checkbox to select the record you want to update and press the update button (I mean only one button).

Link to comment
Share on other sites

  • 3 weeks later...

thank you very much for the code, the code has been helpful for me, although I am not a rookie with php and mysql, your example helped me a lot.

 

however, I would like if possible to help me with something extra, I modified the instructions mysql and have adapted to an application with foreign keys in innodb table,

 

Using innodb table, the update is not working, I've checked the code from the bottom up and top to bottom, the only thing that I changed are the mysql instructions, which operate under mysqlworbench.

 

I know the topic is not about databases, or types of tables, but please could you help me telling me if the official application with innodb tables?

 

I get the error: "/ / if the 'id' in the URL is not valid, or if there is no 'id' value, display an error"

 

There is a difference between the auto increment field in a table myisam / innodb?

 

Or is it that I delete something in the code that I can see ?

 

Thank you very much for your time and reading my question / case.

Link to comment
Share on other sites

I have been going nuts looking for a php lesson like this, plain english and I was able to follow along every step of the way. Thanks to you I'm now learning!

 

I have a question though, users. How could you bridge this with something lets say joomla (only because thats my next everest :P) so that only registered users could view it. Would I have to build this as a module and assign individual user levels??

 

I have actually made this script able to put new users into joomla, using only this script! Thankyou!

Thanks again

Karlos

Link to comment
Share on other sites

Hello!

 

Dear Friend This was an awesome tutorial which I have used to create a database system .

There I have a problem ?? Please see attachment!! post-41754-033817300 1329386741_thumb.jpg:)

I want to edit the "feedback" field without leaving the view page. I want to edit on preview table. How can I do that. I have modify your code look like that ....

<?php
require_once('auth.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Member Index</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<center>
<img src="./images/sonali.jpg" height="100px" width="100px"/>
</center>
<div align="center">
<p> <h1> Welcome TO Sonali Bank Limited</h1><br />

<h2><?php echo $_SESSION['SESS_FIRST_NAME'];?></h2>
<h2><?php echo $_SESSION['SESS_LAST_NAME'];?></h2>
<h3>BRANCH CODE : <?php echo $_SESSION['SESS_MEMBER_LOGIN'];?></h3>
</div>

<p><a href="member-profile.php">My Profile</a> | <a href="logout.php">Logout</a> | <a href="user-view-admin.php">View ALL</a> | <a href="user-view-paginated.php">View Paginated</a></p>
<?php
//This code is for SONALI BANK ONLY
// connect to the database
include('user-connect-db.php');
include('config.php');
//check user name
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM sblnrat WHERE brcode = $_SESSION[sESS_MEMBER_LOGIN] Order by id")) 
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table align='center' border='1' cellpadding='10'>";                                     
// set table headers
echo "<tr><th>SL</th><th>PON</th><th>TRA NO.</th><th>TRA DT</th><th>BR-CODE</th><th>BR-NAME</th><th>BF-NAME</th><th>BF-A/C</th><th>AMT</th><th>Feed Back</th></tr>";
while ($row = $result->fetch_object())
{
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->id . "</td>";
echo "<td>" . $row->pon . "</td>";
echo "<td>" . $row->trano . "</td>";
echo "<td>" . $row->tradt . "</td>";
echo "<td>" . $row->brcode . "</td>";
echo "<td>" . $row->brname . "</td>";
echo "<td>" . $row->bfname . "</td>";
echo "<td>" . $row->bfac . "</td>";
echo "<td>" . $row->amt . "</td>";
echo "<td>" . $row->feedback . "</td>";
echo "<td>".'
<form action="insert.php?id='. $row->id . '" method="post"> 
<select name="feedback"> 
       <option selected="selected">'.$row->feedback.'</option> 
       <option value="Paid">02</option> 
       <option value="NotPaid">05</option> 
       </select>    
<input type="submit" name="submit" value="Update" /> 
</form>'."</td>";

echo "<td><a href='user-records.php?id=" . $row->id . "'>Edit</a></td>";
echo "</tr>";
}
echo "</table>";
}
//if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}

// close database connection
$mysqli->close();
?>
<br />
</body>
</html>

 

When I want to submit feed back the insert.php was nor working ????? can any one make a insert.php for me

or any java script solution or other else!!!

Link to comment
Share on other sites

Hello!

 

Dear ADMIN This was an awesome tutorial which I have used to create a database system .

There I have a problem ?? Please see attachment!! post-41754-033817300 1329386741_thumb.jpg:)

I want to edit the "feedback" field without leaving the view page. I want to edit on preview table. How can I do that. I have modify your code look like that ....

<?php
require_once('auth.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Member Index</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<center>
<img src="./images/sonali.jpg" height="100px" width="100px"/>
</center>
<div align="center">
<p> <h1> Welcome TO Sonali Bank Limited</h1><br />

<h2><?php echo $_SESSION['SESS_FIRST_NAME'];?></h2>
<h2><?php echo $_SESSION['SESS_LAST_NAME'];?></h2>
<h3>BRANCH CODE : <?php echo $_SESSION['SESS_MEMBER_LOGIN'];?></h3>
</div>

<p><a href="member-profile.php">My Profile</a> | <a href="logout.php">Logout</a> | <a href="user-view-admin.php">View ALL</a> | <a href="user-view-paginated.php">View Paginated</a></p>
<?php
//This code is for SONALI BANK ONLY
// connect to the database
include('user-connect-db.php');
include('config.php');
//check user name
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM sblnrat WHERE brcode = $_SESSION[sESS_MEMBER_LOGIN] Order by id")) 
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table align='center' border='1' cellpadding='10'>";                                     
// set table headers
echo "<tr><th>SL</th><th>PON</th><th>TRA NO.</th><th>TRA DT</th><th>BR-CODE</th><th>BR-NAME</th><th>BF-NAME</th><th>BF-A/C</th><th>AMT</th><th>Feed Back</th></tr>";
while ($row = $result->fetch_object())
{
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->id . "</td>";
echo "<td>" . $row->pon . "</td>";
echo "<td>" . $row->trano . "</td>";
echo "<td>" . $row->tradt . "</td>";
echo "<td>" . $row->brcode . "</td>";
echo "<td>" . $row->brname . "</td>";
echo "<td>" . $row->bfname . "</td>";
echo "<td>" . $row->bfac . "</td>";
echo "<td>" . $row->amt . "</td>";
echo "<td>" . $row->feedback . "</td>";
echo "<td>".'
<form action="insert.php?id='. $row->id . '" method="post"> 
<select name="feedback"> 
       <option selected="selected">'.$row->feedback.'</option> 
       <option value="Paid">02</option> 
       <option value="NotPaid">05</option> 
       </select>    
<input type="submit" name="submit" value="Update" /> 
</form>'."</td>";

echo "<td><a href='user-records.php?id=" . $row->id . "'>Edit</a></td>";
echo "</tr>";
}
echo "</table>";
}
//if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}

// close database connection
$mysqli->close();
?>
<br />
</body>
</html>

 

When I want to submit feed back the insert.php was nor working ????? can any one make a insert.php for me

or any java script solution or other else!!!

 

I have create the insert.php file but it not work can nay one solve this matter....:(

<?php
// connect to the database
include("user-connect-db.php");

/*-------------------------------
          EDIT RECORD
--------------------------------*/
// if the 'id' variable is set in the URL, we know that we need to edit a record

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

{

if (isset($_GET['id']))
{
// if the form's submit button is clicked, we need to process the form

// make sure the 'id' in the URL is valid
if (is_numeric($_POST['id']))

	{
// get variables from the URL/form
$id = $_POST['id'];
$feedback = htmlentities($_POST['feedback'], ENT_QUOTES);

// if everything is fine, update the record in the database
if ($stmt = $mysqli->prepare("UPDATE sblnrat SET feedback= ? WHERE id=?"))
{
$stmt->bind_param("si", $feedback, $id);
$stmt->execute();
$stmt->close();
}
// show an error message if the query has an error
 else
{
echo "ERROR: could not prepare SQL statement.";
 }

// redirect the user once the form is updated
header("Location: member-view.php");
  }
 }
// if the 'id' variable is not valid, show an error message
else
{
echo "Error...............!!!!!!!";
 }


 }

// close the mysqli connection
       $mysqli->close();
?>

Link to comment
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...