Help - Search - Members - Calendar
Full Version: notepad
Forums > Resource Center > Webmasters' Corner > Webmasters' Corner Resolved Topics
Maccabee
im wanting to make an online notepad for personal use. ive got a server. it could be like the one cb uses or this.
http://jumk.de/notepad/

i use that all the time but it sucks. haha.

i figured the best way to do this would be with a database as opposed to cookies or something so that way you can always access it from any pc.

how would i go about this? i only started learning database stuff recently.

fanks. its also just for the learning experience.
JonHMChan
I don't think this should be too hard if you know even basic PHP/MySQL, but you should definitely be using databases, not cookies. How much do you know when it comes to creating a CMD, working with databases, and handling SQL? (Those lynda tuts are really great if you still have a subscription)

Maccabee
ya i watched those. im also going through this.
http://www.tizag.com/mysqlTutorial/mysqltables.php
im just learning as i go.
I made a database and tested it and i guess im about to make a table.
ive always been so intrigued by databases. this is how emails and facebook mainly work with messaging right?
fixtatik
Do you know about logins, too? Assuming you do, something as simple as the example you put could be done with five (you could do it with four if you really wanted to) columns in a database:

CODE
<?php
// Connect to MySQL
mysql_query("CREATE TABLE memo (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), user CHAR(15) NOT NULL, pass CHAR(32) NOT NULL, date DATETIME NOT NULL, last DATETIME NOT NULL, memo TEXT NOT NULL)");
?>

CODE
<?php
// Create a new user
if(count($_POST) > 0) {
  $user = $_POST['user'];
  $pass = md5($_POST['pass']);
  if($user && $pass) mysql_query("INSERT INTO memo ('', '$user', '$pass', NOW(), '', '')");
}
?>

CODE
<?php
// Insert a new memo
if(count($_POST) > 0) {
  $id = $_COOKIE['user']; // When they login, set a cookie as their user ID
  $memo = $_POST['memo'];
  mysql_query("UPDATE memo SET last = NOW() WHERE id = $id"); // Update the last memo time
  mysql_query("UPDATE memo SET memo = '$memo' WHERE id = $id"); // Update the memo
}
?>

CODE
<?php
// To read the memo
$id = $_COOKIE['user'];
$query = mysql_fetch_assoc(mysql_query("SELECT date, last, memo FROM memo WHERE id = $id"));
$date = $query['date'];
$last = $query['last'];
$memo = $query['memo'];
?>
Maccabee
wow. i understand most of that code but i dont really understand what of all is happening. would each note be saved as a database or table or whatever? and for each of those I would need to implement it into like textareas right?

dang. i thought i was gonna get to have a challenge but you just did it for me... haha.
fixtatik
Haha, it could have been a challenge for you if you picked a challenging thing to do. Entering text is easy.

If you're doing it where users don't have separate notes, then you need only one table. There's a unique ID (which increments up 1 when someone makes a new user), a username limited to 15 characters, a password at 32 characters (all words hashed in md5 end up being 32 characters, the time they signed up, the time they last edited their notepad, and the memo text itself.

When they type in a textarea, then push submit, the query updates the memo that's linked to that user's ID. The table looks something like this:
CODE
+----------------------------------------------------------------------------------+
| id | user | pass   | date | last | memo |
+----------------------------------------------------------------------------------+
| 1 | name | b36e... | 2009-10-23 18:25:30 | 2009-10-24 18:20:30 | text |
| 2 | name2 | g57n... | 2009-10-23 18:26:30 | 2009-10-24 02:25:30 | text |
+----------------------------------------------------------------------------------+

Once they sign up, the only thing that will be changed is the "last" column and the "memo" column (unless they have an option to change their username/password).

If you're following the basic example I used for the queries, the textarea could look something like this:
CODE
<textarea name="memo" cols="50" rows="10"><?php echo $memo; ?></textarea>

The fourth code up there (^) defines what the $memo variable is, by gathering the text from the "memo" column in your database, in the row that has an ID that matches the cookie on a user's computer.

I don't think that site you posted uses cookies (didn't check, maybe they do). I don't think they do, because they make you enter your password on every new page. If you didn't use cookies, then instead of running this query:
CODE
"SELECT memo FROM memo WHERE id = $id"

You'd use this one:
CODE
"SELECT memo FROM memo WHERE user = '$user' AND pass = '$pass'"
Where $pass = md5($_POST['pass'])
Maccabee
ok so basically each time a user signs up it creates a new row? and that row contains the notes? and then to access that row it requires a username and pass? cause when i made the database i gave it a username and pass. so is this:

CODE
<?php
// Create a new user
if(count($_POST) > 0) {
  $user = $_POST['user'];
  $pass = md5($_POST['pass']);
  if($user && $pass) mysql_query("INSERT INTO memo ('', '$user', '$pass', NOW(), '', '')");
}
?>


the script to do that? wow. you did this by memory? hmmm, i dont get how to put that into form view. ill keep studying.
fixtatik
Yup, that's the script to insert new users. It just checks if the submit button is pushed, then it runs through a couple variables. It's extremely basic, so there's no security protection; something like that is very easy to abuse.

As for the form, something like this works fine:
CODE
<form action="action.php" method="post">
<input name="user" maxlength="15" />
<input name="pass" maxlength="16" />
<input type="submit" />
</form>

The file "action.php" contains the PHP code to insert the user into the database. What it's doing is counting the values in the $_POST array, and if that number is larger than 0 (i.e., the submit button was pushed), then it should insert the data. You could also do it like this:
CODE
<?php
if($_POST['user'] && $_POST['pass']) mysql_query("INSERT INTO memo VALUES ('', '{$_POST['user']}', '" . md5($_POST['pass']) . "', NOW(), '', '')");
?>

That checks if the "user" and "pass" fields are both set, and if they are, insert the data.

As for the query itself (notice the "VALUES" -- forgot that last time):
"INSERT INTO memo VALUES ('', '$user', '$pass', NOW(), '', '')"

You can also do it this way:
"INSERT INTO memo (user, pass, date) VALUES ('$user', '$pass', NOW())"

When you use blank quotes (''), nothing is inserted for that column (or in the case of auto incrementing, it'll insert the next number).

I'd suggest reading through the PHP manual on MySQL functions to get a better understanding of what you can do: http://php.net/manual/en/book.mysql.php
Maccabee
so how does the file layout work? I have never done this before. it most of the php on one action page?

one page:

CODE
<?php
// Connect to MySQL
mysql_query("CREATE TABLE memo (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), user CHAR(15) NOT NULL, pass CHAR(32) NOT NULL, date DATETIME NOT NULL, last DATETIME NOT NULL, memo TEXT NOT NULL)");


// Create a new user
if(count($_POST) > 0) {
  $user = $_POST['user'];
  $pass = md5($_POST['pass']);
  if($user && $pass) mysql_query("INSERT INTO memo ('', '$user', '$pass', NOW(), '', '')");
}
?>

<form action="action.php" method="post">
<input name="user" maxlength="15" />
<input name="pass" maxlength="16" />
<input type="submit" />
</form>


and cant I take out the first little part after I run it cause it only needs to create that table once? and then it sends them to the action page?

and how do I let them update the memo?

CODE
<form action="update.php" method="post">
<textarea name="memo" cols="50" rows="10"><?php echo $memo; ?></textarea>
<input type="submit" />
</form>


still a little lost.
JonHMChan
When you're running MySQL to create a table, you only need to run it once. It's probably isn't a good idea to be putting it on a screen that many people will be accessing, because then it will be running the "create table" script each time someone views this page. If you're still learning how MySQL works when it comes to creating tables, how to organize them, and relate them to each other in a database, try using phpMyAdmin from your hosting client if you can -- it makes things much simpler. The reason I would recommend it is because when you do things in a more CMD non-programmer fashion, it also shows you the appropriate code to execute the action you just inputted through the application. That's how I learned most of my SQL syntax, and I think that would help you more.
fixtatik
If you're running it as-is, you'll have problems immediately because you're not connected to your database. The "// Connect to MySQL" is just a comment saying you need to connect to MySQL before doing anything else. Like Jon said, either run the table query once, or make tables inside phpMyAdmin, which most hosts provide standard.

Also, take note of my previous post. I forgot to add "VALUES" to the code earlier; update yours to include "VALUES".

You can pretty much do the whole thing with two pages. You can call the query script on the same page as the form; just make sure instead of linking the action attribute to a different page, link it to itself.

The first page contains the code to create a new user. Make the form, then use the PHP script to check if both the "user" and "pass" fields are filled, and enter it into the database.

The second page contains the memo, after a user has logged in. You need a textarea which contains the memo, and the PHP script to check if the submit button was pushed. If it was pushed, then run a query on the database to update that row.
Maccabee
so could you just give me like step by step instructions on what I do now? I'm a little lost.
fixtatik
Do it for you, or explain a part better? What do you have so far?
Maccabee
I mean, what order do I make the files and which code would I put in each file?
fixtatik
File order doesn't matter, because you're linking to one from the other. It wouldn't even matter if you combined them all into one.

All you really need is the code to check for username & password, and to update the database with a new note & time. You can create the table yourself prior to running the script (inside phpMyAdmin, or something similar). The script runs when a user pushes the submit button (that's what if(isset($_POST['user'])) or if(count($_POST) > 0) does).
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.