Killersites Community: Basic PHP System: View/Edit/Delete/Add Records - Killersites Community

Jump to content

  • (6 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • 6
  • You cannot start a new topic
  • You cannot reply to this topic

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

#81 User is offline   sibichan 

  • View blog
  • Group: New Members
  • Posts: 1
  • Joined: 16-January 12

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

  • Group: 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 User is offline   metabee 

  • View blog
  • Group: New Members
  • Posts: 9
  • Joined: 06-August 11

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 User is offline   fran 

  • View blog
  • Group: New Members
  • Posts: 1
  • Joined: 06-February 12

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 User is offline   Ben 

  • View blog
  • Group: Administrators
  • Posts: 5,409
  • Joined: 19-December 08
  • LocationChico, CA

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!
Benjamin Falk | Falken Creative : Twitter : KillerSites Screencast Blog
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
0

#86 User is offline   karlos 

  • View blog
  • Group: New Members
  • Posts: 1
  • Joined: 09-February 12

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 User is offline   Rupom 

  • View blog
  • Group: New Members
  • Posts: 5
  • Joined: 16-February 12

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!! Attached Image: 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 User is offline   Rupom 

  • View blog
  • Group: New Members
  • Posts: 5
  • Joined: 16-February 12

Posted 17 February 2012 - 07:08 PM

View PostRupom, on 16 February 2012 - 04:29 PM, said:

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!! 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 User is offline   Ben 

  • View blog
  • Group: Administrators
  • Posts: 5,409
  • Joined: 19-December 08
  • LocationChico, CA

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/
Benjamin Falk | Falken Creative : Twitter : KillerSites Screencast Blog
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
0

#90 User is offline   Rupom 

  • View blog
  • Group: New Members
  • Posts: 5
  • Joined: 16-February 12

Posted 20 February 2012 - 12:25 AM

View PostBen, on 19 February 2012 - 11:13 PM, said:

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

#91 User is offline   GIRISH 

  • View blog
  • Group: New Members
  • Posts: 2
  • Joined: 29-February 12

Posted 29 February 2012 - 01:44 AM

Hi Rupom Can u please tell me what u did?? Actually I am looking for the same solution.
0

#92 User is offline   teke 

  • View blog
  • Group: New Members
  • Posts: 7
  • Joined: 02-March 12

Posted 03 March 2012 - 06:11 AM

hello, i try to add a new column, but i fail

i add in a SQL file this

CREATE TABLE `players` (
 `id` int(11) NOT NULL auto_increment,
 `leader` varchar(32) NOT NULL,
 `firstname` varchar(32) NOT NULL,
 `lastname` varchar(32) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


in view.php like this

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
        <title>View Records</title>
</head>
<body>

<?php
/* 
        VIEW.PHP
        Displays all data from 'players' table
*/

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

        // get results from database
        $result = mysql_query("SELECT * FROM players") 
                or die(mysql_error());  
                
        // display data in table
        //echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";
        
        echo "<table border='1' cellpadding='10'>";
        echo "<tr> <th>ID</th> <th>Leader</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";

        // loop through results of database query, displaying them in the table
        while($row = mysql_fetch_array( $result )) {
                
                // echo out the contents of each row into a table
                echo "<tr>";
                echo '<td>' . $row['id'] . '</td>';
                echo '<td>' . $row['leader'] . '</td>';
                echo '<td>' . $row['firstname'] . '</td>';
                echo '<td>' . $row['lastname'] . '</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>";
?>
<p><a href="new.php">Add a new record</a></p>

</body>
</html> 


i remove view-paginated.php

in new.php

<?php
/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/
 
 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($leader, $first, $last, $error)
 {
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
 <title>New 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">
 <div>
 <strong>Leader: *</strong> <input type="text" name="leader" value="<?php echo $leader; ?>" /><br/>
 <strong>First Name: *</strong> <input type="text" name="firstname" value="<?php echo $first; ?>" /><br/>
 <strong>Last Name: *</strong> <input type="text" name="lastname" value="<?php echo $last; ?>" /><br/>
 <p>* required</p>
 <input type="submit" name="submit" value="Submit">
 </div>
 </form> 
 </body>
 </html>
 <?php 
 }
 
 
 

 // connect to the database
 include('connect-db.php');
 
 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $leader = mysql_real_escape_string(htmlspecialchars($_POST['leader']));
 $firstname = mysql_real_escape_string(htmlspecialchars($_POST['firstname']));
 $lastname = mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
 
 // check to make sure both fields are entered
 if ($leader == '' || $firstname == '' || $lastname == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';
 
 // if either field is blank, display the form again
 renderForm($leader, $firstname, $lastname, $error);
 }
 else
 {
 // save the data to the database
 mysql_query("INSERT players SET leader='$leader' firstname='$firstname', lastname='$lastname'")
 or die(mysql_error()); 
 
 // once saved, redirect back to the view page
 header("Location: view.php"); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','');
 }
?> 



what is wrong ? please help me, Thanks ! sorry for my english ,i'm from Romania
0

#93 User is offline   Ben 

  • View blog
  • Group: Administrators
  • Posts: 5,409
  • Joined: 19-December 08
  • LocationChico, CA

Posted 03 March 2012 - 11:29 AM

What errors are you getting?
Benjamin Falk | Falken Creative : Twitter : KillerSites Screencast Blog
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
0

#94 User is offline   teke 

  • View blog
  • Group: New Members
  • Posts: 7
  • Joined: 02-March 12

Posted 04 March 2012 - 10:41 AM

not appear in the table/column
0

#95 User is offline   Ben 

  • View blog
  • Group: Administrators
  • Posts: 5,409
  • Joined: 19-December 08
  • LocationChico, CA

Posted 04 March 2012 - 10:49 AM

Two things in new.php:

This line

mysql_query("INSERT players SET leader='$leader' firstname='$firstname', lastname='$lastname'")

is missing a comma after "$leader":

mysql_query("INSERT players SET leader='$leader', firstname='$firstname', lastname='$lastname'")

Secondly, right at the end of the file, this line

renderForm('','','');

probably needs to be this:

renderForm('','','','');

Benjamin Falk | Falken Creative : Twitter : KillerSites Screencast Blog
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
1

#96 User is offline   teke 

  • View blog
  • Group: New Members
  • Posts: 7
  • Joined: 02-March 12

Posted 04 March 2012 - 10:59 AM

thanks !!! it's working !
0

#97 User is offline   skunkfu 

  • View blog
  • Group: New Members
  • Posts: 3
  • Joined: 19-March 12

Posted 19 March 2012 - 01:27 PM

First of all, thank you very much Ben for this PHP script. It has helped me immensely for a college project, and has also up'd my interest in web development again!

Now, I wish to expand on your code further to give the ability to add columns to the database. Currently, columns such as FirstName and LastName are pre-defined within the code.

The most basic way to do this would be to create a form with a textbox and submit button, and have PHP use the alter command to create the column. The problem here would be the code to view, edit and add records, since they are still hard-coded with the FirstName and LastName variables.

Is there any particular method you could point me towards to figure this out? I may be a little over my head with this since it may require rewriting the whole PHP code.

Hope I explained this well enough.

Thank you!
0

#98 User is offline   Ben 

  • View blog
  • Group: Administrators
  • Posts: 5,409
  • Joined: 19-December 08
  • LocationChico, CA

Posted 19 March 2012 - 01:55 PM

Maybe you can explain why you would need to programatically modify the database? Most systems that I know and work with don't work that way -- you usually have a set database structure, and you are simply adding/removing/editing records from it.
Benjamin Falk | Falken Creative : Twitter : KillerSites Screencast Blog
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
0

#99 User is offline   skunkfu 

  • View blog
  • Group: New Members
  • Posts: 3
  • Joined: 19-March 12

Posted 19 March 2012 - 02:11 PM

Ah yes you have an extremely good point. I'm probably making it more complicated than it needs to be.

Its scalability really. For the project, I'm building a database interface for contacts. Later on I may realise I need another attribute of data for my contacts list, such as a secondary telephone number.

Do you believe it is a better idea to take into account all attributes that would be required for a contacts database first, then design the database structure?

Extra credits for my class is on the mind, going that extra mile. :rolleyes:

Thanks!
0

#100 User is offline   Ben 

  • View blog
  • Group: Administrators
  • Posts: 5,409
  • Joined: 19-December 08
  • LocationChico, CA

Posted 19 March 2012 - 02:21 PM

If you want to focus on scalability, I'd suggest looking at object oriented programming and MVC -- those will help make your code modular, separating out the responsibilities of your application into individual objects and making things easier to maintain and improve in the future.

Yes, you do want to consider how the application might be expanded... but you can't fully anticipate what sort of changes you will want to make and code for all of them.
Benjamin Falk | Falken Creative : Twitter : KillerSites Screencast Blog
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
0

#101 User is offline   skunkfu 

  • View blog
  • Group: New Members
  • Posts: 3
  • Joined: 19-March 12

Posted 19 March 2012 - 02:29 PM

Understood Ben. OOP and MVC would be extreme overkill for the little project I'm dealing with.

I will keep it simple for now then. Later on if I require such scalable features (being taken on for a client perhaps), then I will look into it further.

Thanks for your time!! ^_^
0

#102 User is offline   teke 

  • View blog
  • Group: New Members
  • Posts: 7
  • Joined: 02-March 12

Posted 26 March 2012 - 03:35 PM

how can add in table a timeleft column and when the time is up ,automatically delete row

if you can...
0

#103 User is offline   Ben 

  • View blog
  • Group: Administrators
  • Posts: 5,409
  • Joined: 19-December 08
  • LocationChico, CA

Posted 26 March 2012 - 05:13 PM

teke, on 26 March 2012 - 01:35 PM, said:

how can add in table a timeleft column and when the time is up ,automatically delete row

if you can...

I don't think that is something that is done with plain PHP -- I believe you would need to use a CRON job that repeats a certain number of times a day to call a PHP script to do the cleanup. If you are dealing with small amounts of time (minutes rather than hours, for example) you might use AJAX that loops every x seconds to call a PHP script to do the cleanup.
Benjamin Falk | Falken Creative : Twitter : KillerSites Screencast Blog
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
0

#104 User is offline   teke 

  • View blog
  • Group: New Members
  • Posts: 7
  • Joined: 02-March 12

Posted 27 March 2012 - 04:54 AM

the time in days i need, anyway thanks !
0

#105 User is offline   teke 

  • View blog
  • Group: New Members
  • Posts: 7
  • Joined: 02-March 12

Posted 28 March 2012 - 02:53 PM

i returned... do you know something about countdown timer in table? any
0

#106 User is offline   Blackburn 

  • View blog
  • Group: New Members
  • Posts: 1
  • Joined: 03-April 12

Posted 03 April 2012 - 10:53 AM

I have some problem with this tutorial. I made a bit change to suit my forum script I building. Heres what my changed files.
EDIT.PHP
<?php
//create_cat.php
 include 'connect.php';
 include 'header.php';
 include 'modules/bbcode.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, $error)
 {
 ?>
 <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>Content*</strong> <input type="text" name="firstname" value="<?php echo $firstname; ?>"/><br/>
 <p>* Required</p>
 <input type="submit" name="submit" value="Submit">
 </div>
 <a href="/settings.php">Back</a>
 </form> 
 </body>
 </html> 
 <?php
 }



 // connect to the database

 
 // 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['post_id'];
 $firstname = mysql_real_escape_string(htmlspecialchars($_POST['post_content']));
 
 // check that firstname/lastname fields are both filled in
 if ($firstname == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';
 
 //error, display form
 renderForm($id, $firstname, $error);
 }
 else
 {
 // save the data to the database
 mysql_query("UPDATE 
					posts
				SET 
					post_content='$firstname' 
				WHERE post_id='$id'")
				
 or die(mysql_error()); 
 
 // once saved, redirect back to the view page
 header("Location: settings.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 = mysql_query("SELECT * FROM posts WHERE post_id=$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
 $firstname = $row['post_content'];
 
 // show form
 renderForm($id, $firstname, '');
 }
 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!';
 }
 }
 include 'footer.php';
?>


and NEW.PHP

<?php
//create_cat.php
include 'connect.php';
include 'header.php';

/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/
 
 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($first, $last, $error)
 {
 ?>
 <html>
 <head>
 <title>New 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">
 <div>
 <strong>Content: *</strong> <input type="textarea" name="firstname" value="<?php echo $first; ?>" /><br/>
 <p>* required</p>
 <input type="submit" name="submit" value="Submit">
 </div>
 </form> 
 </body>
 </html>
 <?php 
 }
 
 


 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $firstname = mysql_real_escape_string(htmlspecialchars($_POST['post_content']));

 
 // check to make sure both fields are entered
 if ($firstname == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';
 
 // if either field is blank, display the form again
 renderForm($first, $last, $error);
 }
 else
 {
 // save the data to the database
 mysql_query("INSERT posts SET post_content='$firstname'")
 or die(mysql_error()); 
 
 // once saved, redirect back to the view page
 header("Location: settings.php"); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','');
 }
 
 include 'footer.php';
?> 


In both Im getting error "ERROR: Please fill in all required fields" if I want to change something or add. The delete option is working.
0

#107 User is offline   Face 

  • View blog
  • Group: New Members
  • Posts: 9
  • Joined: 10-April 12

Posted 10 April 2012 - 07:19 PM

First post, please be kind!

Ok, so I don't really know much about anything regarding PHP/MySQL - I can do HTML and Flash (providing that is stays away from DBs)...

So I downloaded and installed XAMPP, basically copied and pasted, made all the necessary files - and now I have your basic PHP program running on my computer *ALMOST* exactly as you have it on yours (BEN).

The problem I am having, and i can't figure it out for the life of me is why I get this unexplainable text at the top of the page saying "VALUES(4, 'Sam', 'Smith');" which is the last line of code that I imported into the "RECORDS" DB... (screen shot included). This text will follow me to every part of the program (new.php, edit.php...)

Attached Image: viewphp.png

Any ideas of what I missed?

PS - The reason I am learning this is to help someone else with a school project. The more I look into PHP the more I want to know, so this might be the start of a long relationship.

Cheers,
Face
0

#108 User is offline   Ben 

  • View blog
  • Group: Administrators
  • Posts: 5,409
  • Joined: 19-December 08
  • LocationChico, CA

Posted 10 April 2012 - 07:29 PM

@Face: I would suggest looking at your connect-db.php file. I'm betting that you didn't do the copy/paste exactly right, and you may have that text at the beginning of the file, probably before the opening <?php tag.
Benjamin Falk | Falken Creative : Twitter : KillerSites Screencast Blog
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
0

#109 User is offline   Face 

  • View blog
  • Group: New Members
  • Posts: 9
  • Joined: 10-April 12

Posted 10 April 2012 - 07:35 PM

And of course that is exactly what it was. Thanks for your very quick response to a very stupid problem... I had checked all the files except for that one.

Thanks again. from this point I expect to make my own modifications and actually start learning stuff - so you should be hearing from me soon (very).

Cheers,
Face
0

#110 User is offline   Face 

  • View blog
  • Group: New Members
  • Posts: 9
  • Joined: 10-April 12

Posted 10 April 2012 - 07:48 PM

Actually, to help myself out I'm going to let you all in on what the project is.

Basically its supposed to be a simple PHP/MySQL database that keeps track of books coming in/out of a collection. So basically what I am going to try to do is add a Title, Publisher, and Date Aqcuired column... perhaps a "Aqcuired from where." while always keeping the ability to delete/edit/add...

and then I will move onto a program for myself that can help me inventory shop tools / materials.

Always happy to recieve advice!

Cheers,
Face
0

Share this topic:


  • (6 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • 6
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users