1

I am building an online WYSIWYG editor, and I have constructed a method for clients to edit their own webpages through a <textarea> method in HTML. Now the only issue is that I have never dealt directly with databases (when I say 'never' I mean... never.)

I am seriously stuck on a few things about this... I'm hoping someone knows how to fix this.

So, code is as follows:
(all of this is within the body tags...)

<!-- Database Connection -->
<?php
    if ($_POST['submit']) {
        // MySql Database Connection, connecting at ("?", with details "user", "password") or spit error
        mysql_connect ("localhost", "user", "password") or die ('Error: ' . mysql_error());
        // Select database named ("dbName") or spit error
        mysql_select_db("dbName") or die ('Data error:' . mysql_error());
        // Select text to input, from textarea, name="textName"
        $text = mysql_real_escape_string($_POST['textName']); 
        // Input ^ into table "TableName", field (field)
        $query="INSERT INTO TableName (field) VALUES ('$text')";
        // Submit query, or spit error
        mysql_query($query) or die ('Error updating database' . mysql_error());
    }
?>

<!-- Actual Text Input -->
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <textarea name="textName">INITIAL TEXT</textarea>
    <input name="submit" type="submit" value="submit" />
</form>

The actual code for pushing information into the database is working, I can very easily input text into the textarea and click submit and then look at it inside the database I have setup- however I now need to take the information out of the database.

I need to pull it out in two ways:

  1. To display it onto the webpage (which will be done on another document to this) AND
  2. To display it inside the textarea, replacing the "INITIAL TEXT".

(If any of the code looks like it shouldn't work then it is probably because I have quickly removed information about the actual server).

halfer
  • 19,824
  • 17
  • 99
  • 186
lukesrw
  • 1,692
  • 1
  • 14
  • 20
  • 2
    Heads up. Switch to mysqli. mysql is a depreciated library – Dawid O Dec 17 '14 at 21:28
  • 1
    Before you get any further, stop using mysql_* functions. Look into PDO or mysqli. – Robbert Dec 17 '14 at 21:28
  • 4
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). – Jay Blanchard Dec 17 '14 at 21:28
  • 1
    omg, are there some more people who want to tell him to not use depricated stuff ? – Dwza Dec 17 '14 at 21:29
  • I will be looking into MySQLi next, it is on my list of things to do- however I would like to get this working first... or, if someone would like to help explaining how to convert (as I said, completely new to databases) – lukesrw Dec 17 '14 at 21:29
  • 1
    There are @Dwza - these were posted close together. Others will want to chime in too. IsrwLuke no one will write the code for you. Please show what you have tried and be prepared for no one to help until you have quit using the deprecated function library. – Jay Blanchard Dec 17 '14 at 21:31
  • I've stated that I'm brand new to databases as a whole, I have no idea what to try. How would I start using MySQLi? – lukesrw Dec 17 '14 at 21:32
  • This is a bit too broad for the SO format. I suggest that you make an attempt, like you did with your `INSERT` statement, then let us know what you tried and what specifically went wrong. – showdev Dec 17 '14 at 21:33
  • This code looks fine, but since it works I am not sure what you are stuck on. There are fetch functions documented for this library in the manual, `mysql_fetch` I think. Have a look at the docs for an example: https://php.net/mysql_query – halfer Dec 17 '14 at 21:33
  • Ah, and you'll need a `SELECT` query too, outside of your post clause. Again the example in the link above should suffice. (FWIW I don't agree that people won't help if you are using a deprecated library, sometimes people have no choice. But it would help for the question to be a bit less broad). – halfer Dec 17 '14 at 21:36
  • 1
    As everyone recommends, switch to PDO or mysqli. If PDO, consider reading this: http://stackoverflow.com/questions/134099/are-pdo-prepared-statements-sufficient-to-prevent-sql-injection so that your inserts into the database are safe(er). – visevo Dec 17 '14 at 21:39
  • 1
    I don't think anyone mentioned yet, but you should use `mysqli` instead of `mysql`. ;) – Phil Tune Dec 17 '14 at 21:39

1 Answers1

1

In the words of Obi-Wan Kenobi, "you have much to learn about the ways of the force." But that's OK ... we all need to learn more if we want to go anywhere in life ;)

Nonetheless, I'll try and offer some help. First off, read up a bit on DB design. You should probably be putting more than one datum into that data table (things like timestamps, user ID and perhaps a unique ID [which the DBMS can do for you] come to mind).

The primary thing you'll notice about mysqli_* vs. mysql_* is the inclusion of the required data object:

$connection = mysqli_connect("localhost","root","","test");

$sql = "select * from myTable;";
$result = mysqli_query($connection,$sql);

And the above code also acts as a primer for getting a "result resource" from MySQL. But a "result resource" isn't actual data; it's more like a "pointer". If you did this:

echo $result;

You'd just get "1" if the query was successful.

Here's a brief bit of code that might help you understand what to do to get data from a database. I'm going to make some assumptions about your DB table structure, and I'm going to assume you know just which bit you want by including a contrived "$foo_id" variable.

$sql = "select timestamp, user_id, textfieldname from myTable where post_id = $foo_id;";
$res = mysqli_query($connection,$sql);
if ($res && $res->num_rows) { //we got a result, and the result contains data (at least 1 row)

  $return = array();   //this array will hold data for our script

  while ($row = $res->fetch_assoc()) {
     $return['text'] = $row['textfieldname'];
     $return['ts'] = $row['timestamp'];
     $return['user'] = $row['user_id'];
  }
}

At this point, the array $return should hold three variables of interest, one of which is the text you stored previously.

Kevin_Kinsey
  • 2,285
  • 1
  • 22
  • 23
  • 1
    I know I should be adding in multiple parts about the input, I am planning on doing this- for now I was simply working on the one input. Does Mysqli use the same databases as Mysql? – lukesrw Dec 17 '14 at 21:53
  • Good; and I understand --- as I mentioned, we all start somewhere. Tackling things in chunks isn't a bad idea; just remember that programming is such an "exact science" that most of us will be a bit pedantic/exacting/perfectionistic with answers and comments if it appears that you're being too "simple" in your questions, I guess. – Kevin_Kinsey Dec 17 '14 at 21:55
  • I'm sorry if I sound a bit simple. I've no experience haha, so same databases? – lukesrw Dec 17 '14 at 22:02
  • Yes. Both of them connect to a MySQL server. The "i" in "mysqli" stands for "improved"; it's very similar to the mysql extension, but some things are ... different (oops, I mean, "improved") ;) – Kevin_Kinsey Dec 18 '14 at 22:07