0

Using a PHP form to insert the text strings from users into the table, and another form to pull it later on another page, what would be the best method of creating a table in MySQL for strings of text, and what options when creating the table would likely be necessary to best handle text strings?

The complicating factor, I suppose, is that the text that would exist in the table doesn't exist yet (as it would need to be input through the form, etc.), I am unsure if this is why I've had trouble (along with my relative inexperience, so I am unsure of what, precisely would be an ideal table configuration).

Since I don't want to store any other data beyond this user input (like I said, just strings of text i.e a sentence), I assumed I only needed one column when creating the table, but I was unsure of this as well; it seems it is possible I am more likely just overlooking something about how SQL works.

5
  • 1
    (1) you have to consider the estimated, maximum length of such strings to decide whether to use varchar-fields or text-fields in mysql. (2) consider having a 2nd field called id (int, primary key, auto increment), when you need to reference those strings later. (3) use mysqli or PDO instead of mysql, which is deprecated. Commented Mar 3, 2013 at 23:17
  • 1
    You'll need to create a database, a user, and a table, and your fields (like michi said, a unique identifier is a good idea). Check out PDO (or mysqli). Easier to work and since you're learning, might as well start with what's current. Commented Mar 3, 2013 at 23:25
  • @michi Thank you, would the length of the strings (for example, a human-readable sentence of text) lend itself to varchar vs. text-fields? Commented Mar 3, 2013 at 23:35
  • 2
    1) Do not suppress errors using @. Use error_reporting. 2) Stop using mysql_ functions, they are deprecated. 3) Your code is vulnerable to SQL injection. Commented Mar 3, 2013 at 23:43
  • 1
    If you want to avoid duplicates for "key's" data, than you can use this SQL query: "INSERT INTO tablename VALUES (?, ?) ON DUPLICATE KEY UPDATE value_field_name=?" Commented Mar 3, 2013 at 23:50

1 Answer 1

1

I'll put my comments into an answer now:

  1. consider the estimated, maximum length of such strings to decide whether to use varchar-fields or text-fields in mysql.

    Quoting from the MySQL-Manual (BTW a good read for your purpose): Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. http://dev.mysql.com/doc/refman/5.0/en/char.html

    It is said that varcharis faster, for a good summary, see MySQL: Large VARCHAR vs. TEXT?

  2. consider having at least a 2nd field called id (int, primary key, auto increment), when you need to reference those strings later. Consider having a field referencing the author of that string. Maybe a field to store the date and time when the string was put into the database would be a good idea as well.

  3. use mysqli or PDO instead of mysql, which is deprecated. See here, there are links to good tutorials in the 1st answer: How do I migrate my site from mysql to mysqli?

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you,, sections 1 and 2 were exactly what I needed. I was unaware of the differences in mysqli, PDO, etc. so I am going to update that as well.
@josephmarhee glad I could help, have fun!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.