Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand this correctly, your front-end looks like this:</p> <pre><code>&lt;form action="action.php" method=POST&gt; &lt;input type="text" name="name1"&gt; &lt;input type="text" name="investment1"&gt; &lt;BR /&gt; &lt;input type="text" name="name2"&gt; &lt;input type="text" name="investment2"&gt; &lt;BR /&gt; ... &lt;/form&gt; </code></pre> <p>And you want a simple way to read all the fields. If so, I suggest this:</p> <pre><code>$i = 1; while (isset($_POST['name'.$i]) AND isset($_POST['investment'.$i])) { ${'user'.$i} = $_POST['name'.$i] . ', ' . $_POST['investment'.$i]; $i = $i + 1; } </code></pre> <p>You will then end up with a bunch of variables called $user1, $user2, $user3... which you can then use in a standard INSERT statement.</p> <p>However, your database probably needs a serious redesign. </p> <p><strong>UPDATE:</strong> The design is not <a href="http://en.wikipedia.org/wiki/Database_normalization" rel="nofollow">normalized</a>, and you are trying to store two different types of data in one field. </p> <p>It is hard to give you a good example of a better database design, because I don't know exactly what you are trying to do. If I had to hazard a guess, it would probably look something like this:</p> <pre><code>CREATE TABLE investments { investment_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, investor VARCHAR, amount FLOAT }; </code></pre> <p>You would typically do one INSERT for each line on your page:</p> <pre><code>INSERT INTO investments (investor, amount) VALUES ('Peter', 200.00); </code></pre> <p>If you somehow group your investments, say by page, or by product, then the design would look more like this:</p> <pre><code>CREATE TABLE pages { page_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY }; CREATE TABLE investments { investment_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, page_id INT REFERENCES pages(page_id), investor VARCHAR, amount FLOAT }; </code></pre> <p>This means that you create a new page for each set of investments. An even better design would be something like this:</p> <pre><code>CREATE TABLE pages { page_id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR }; CREATE TABLE investors { investor_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR }; CREATE TABLE investments { page_id INT REFERENCES pages(page_id), investor_id INT REFERENCES investor(investor_id), amount FLOAT, PRIMARY KEY (page_id, investor_id) }; </code></pre> <p>Of course, these changes make your queries more complex, but they also make your database more flexible and efficient. You don't have to get it right the first time, but see if you can find some other good databases out there, and see what they look like.</p> <p>Here are some links that might help:</p> <ul> <li><a href="http://www.barrywise.com/2008/01/database-normalization-and-design-techniques/" rel="nofollow">Database Normalization And Design Techniques</a></li> <li><a href="http://dev.mysql.com/doc/refman/5.0/en/examples.html" rel="nofollow">MySQL Query examples</a></li> <li><a href="http://www.sitepoint.com/mysql-foreign-keys-quicker-database-development/" rel="nofollow">How to Use MySQL Foreign Keys for Quicker Database Development</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload