Jump to content


Photo

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


  • Please log in to reply
190 replies to this topic

#61 Ben

Ben

    Administrator

  • Administrators
  • 5,660 posts
  • LocationGainesville, Florida

Posted 04 October 2011 - 09:27 AM

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.
  • 0
Benjamin Falk
Falken Creative : Twitter

#62 oninho

oninho

    New member

  • New Members
  • 2 posts

Posted 09 October 2011 - 02:04 AM

I'm impressed with your code and it is real working...!!! Can you help me another code similar to these which will allow a logged in user to view and edit only his/her information from different tables?? Please help me.....
  • 0

#63 sak

sak

    New member

  • New Members
  • 3 posts

Posted 10 October 2011 - 01:35 AM

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...
  • 0

#64 albersson

albersson

    New member

  • New Members
  • 1 posts

Posted 10 October 2011 - 09:50 AM

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
  • 0

#65 sak

sak

    New member

  • New Members
  • 3 posts

Posted 11 October 2011 - 02:23 AM

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... :*
  • 0

#66 jiroh

jiroh

    New member

  • New Members
  • 1 posts

Posted 16 October 2011 - 06:27 AM

uhm hey what is the database used here Access or SQL ?
  • 0

#67 Ben

Ben

    Administrator

  • Administrators
  • 5,660 posts
  • LocationGainesville, Florida

Posted 16 October 2011 - 06:38 AM

uhm hey what is the database used here Access or SQL ?

The system uses a MySQL database.
  • 0
Benjamin Falk
Falken Creative : Twitter

#68 cool

cool

    New member

  • New Members
  • 5 posts

Posted 15 November 2011 - 05:55 AM

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
  • 0

#69 Ben

Ben

    Administrator

  • Administrators
  • 5,660 posts
  • LocationGainesville, Florida

Posted 15 November 2011 - 11:11 AM

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.
  • 0
Benjamin Falk
Falken Creative : Twitter

#70 cool

cool

    New member

  • New Members
  • 5 posts

Posted 16 November 2011 - 02:13 AM

thanks ben. now my view link also working.
  • 0

#71 Rijesh

Rijesh

    New member

  • New Members
  • 1 posts

Posted 05 December 2011 - 03:16 PM

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....
  • 0

#72 Ben

Ben

    Administrator

  • Administrators
  • 5,660 posts
  • LocationGainesville, Florida

Posted 06 December 2011 - 11:36 AM

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.
  • 0
Benjamin Falk
Falken Creative : Twitter

#73 prajakta

prajakta

    New member

  • New Members
  • 5 posts

Posted 07 December 2011 - 11:51 PM

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?
  • 0

#74 Ben

Ben

    Administrator

  • Administrators
  • 5,660 posts
  • LocationGainesville, Florida

Posted 08 December 2011 - 12:34 AM

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.
  • 0
Benjamin Falk
Falken Creative : Twitter

#75 prajakta

prajakta

    New member

  • New Members
  • 5 posts

Posted 08 December 2011 - 05:06 AM

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

i have crated nw topic as u said ben...but m nt getting proper respons..u pls take a luk at that topic...else just suggest me simple editing code for drop down list.its very urgent
  • 0

#76 sadakalo

sadakalo

    New member

  • New Members
  • 1 posts

Posted 08 December 2011 - 11:34 AM

thanks for the information, i will practice myself
  • 0

#77 JotGill

JotGill

    New member

  • New Members
  • 1 posts

Posted 15 December 2011 - 05:13 PM

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.
  • 0

#78 Ben

Ben

    Administrator

  • Administrators
  • 5,660 posts
  • LocationGainesville, Florida

Posted 16 December 2011 - 03:33 PM

This line itself should be fine, as far as I can tell:

DELETE FROM content WHERE id_number='$id_number'

Things to double check though:

-- Do you have a "content" table in the database?
-- Do you have an "id_field" column in that database? (and if so, is it spelled differently? "ID_field" for example)
  • 0
Benjamin Falk
Falken Creative : Twitter

#79 Robin

Robin

    New member

  • Member
  • 11 posts

Posted 22 December 2011 - 06:30 PM

Thank you for making this script :)

I have a question, what does the function renderForm do?
  • 0

#80 Coreinsanity

Coreinsanity

    Member

  • Member
  • PipPip
  • 16 posts

Posted 23 December 2011 - 11:20 PM

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'];

  • 0

#81 sibichan

sibichan

    New member

  • New Members
  • 1 posts

Posted 16 January 2012 - 04:51 AM

thanks sir.

I started php few days ago.

thanks for this simple php script.

i will try to add more options like catagory,views.
  • 0

#82 Guest_Danish

Guest_Danish
  • Guests

Posted 17 January 2012 - 12:55 AM

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.
  • 0

#83 metabee

metabee

    New member

  • New Members
  • 9 posts

Posted 19 January 2012 - 07:40 AM

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).
  • 0

#84 fran

fran

    New member

  • New Members
  • 1 posts

Posted 06 February 2012 - 09:36 PM

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.
  • 0

#85 Ben

Ben

    Administrator

  • Administrators
  • 5,660 posts
  • LocationGainesville, Florida

Posted 08 February 2012 - 08:58 PM

Unfortunately I know very little about innodb, so that's something you'll probably need to research on your own. Sorry!
  • 0
Benjamin Falk
Falken Creative : Twitter

#86 karlos

karlos

    New member

  • New Members
  • 1 posts

Posted 09 February 2012 - 01:15 PM

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
  • 0

#87 Rupom

Rupom

    New member

  • New Members
  • 5 posts

Posted 16 February 2012 - 04:59 AM

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!! view.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!!!
  • 0

#88 Rupom

Rupom

    New member

  • New Members
  • 5 posts

Posted 17 February 2012 - 07:08 PM

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!! view.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();
?>

  • 0

#89 Ben

Ben

    Administrator

  • Administrators
  • 5,660 posts
  • LocationGainesville, Florida

Posted 19 February 2012 - 11:43 AM

I would suggest looking at something like http://www.appelsiin...jects/jeditable and a tutorial on that script: http://www.programmi...it-placeinline/
  • 0
Benjamin Falk
Falken Creative : Twitter

#90 Rupom

Rupom

    New member

  • New Members
  • 5 posts

Posted 20 February 2012 - 12:25 AM

I would suggest looking at something like http://www.appelsiin...jects/jeditable and a tutorial on that script: http://www.programmi...it-placeinline/

Thank you very much !!!!!!! I have solved my problem..
  • 0




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users