1

I'm basically a front end designer and new to PHP and MySQL but I want a user to be able to store multiple names and there meanings in a MySQL database table named names using PHP I will dynamically create form fields with JQuery every time a user clicks on a link so a user can enter 1 to 1,000,000 different names and there meanings which will be stored in a table called names.

All I want to know is how can I store multiple names and there meanings in a database and then display them back to the user for them to edit or delete using PHP & MySQL?

I created the MySQL table already.

CREATE TABLE names (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
userID INT NOT NULL,
name VARCHAR(255) NOT NULL,
meaning VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);

Here is the HTML.

<form method="post" action="index.php">
<ul>
    <li><label for="name">Name: </label><input type="text" name="name" id="name" /></li>
    <li><label for="meaning">Meaning: </label><input type="text" name="meaning" id="meaning" /></li>
    <li><input type="submit" name="submit" value="Save" /></li>
</ul>
</form>
7
  • Why do you have the hidden submit? Did you mean to have a hidden userid? Commented Apr 25, 2010 at 22:50
  • The hidden field is hidden to send the info to the PHP script to do the validation. Commented Apr 25, 2010 at 22:52
  • 1
    This question is really way too general. It sounds like you're stuck at the "PHP" part. I suggest you run through a basic guestbook-style tutorial or three, and see what you learn. Then come back with more specific questions. As it stands, your question is a little to close to "give me the codez!!11!" Commented Apr 25, 2010 at 22:54
  • @RIP, no, the submit button (labeled SAVE) sends the info to the script. I can't tell what the hidden field does based on what you have provided. Commented Apr 25, 2010 at 22:56
  • you don't have to give me the code exactly but an example of how to store multiple names and there meanings will be nice:) Commented Apr 25, 2010 at 22:56

2 Answers 2

1

If you use

<input name="myfield[]">

then you can repeat this element multiple times and in PHP you will get an array with all the values.

Instead of myfield, you can use any name. Just make sure you append the "[]" at the end.

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

Comments

1

You would somehow create an array of name and meanings out of the POST data, then run it through something like:

// Assuming count($name) == count($meaning)
for($i = 0; $i < count($name); $i++)
{
    $sql = "INSERT INTO `names` (`id`, `userID`, `name`, `meaning`) VALUES('', '', '".$name[$i]."', '".$meaning[$i]."');";
    mysql_query($sql);
}

Note that since there is user input the arrays should be escaped with addslashes when creating them.

Comments

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.