Jump to content

How to get values of dynamically generated text area inputs with php


Banky

Recommended Posts

I'm having a challenge with a mini project i'm working on and have googled for a solution for hours without a headway. Please guys i will appreciate your guidance on this. I have a column in my database table whose contents are generated by exploding a previous form inputs(separated by comma in the table). Now i need to get inputs (from users) for these values using textarea in a form. These inputs will be in arrays depending on the number of contents fetched from the db in the first place and then stored in another column in my table. The issue here is that each time i submit the form i get an undefined index notice for the name of the values in the textarea field,which is test-col []. please see my code below:

 

> <?php $conn = mysqli_connect('localhost', 'root', '', 'myDb');

> $users_id = mysqli_real_escape_string($conn, $_GET['id']); $res = mysqli_query($conn, "SELECT tests FROM bal WHERE users_id =

> '$users_id'"); if ($res){ while ($row =

> mysqli_fetch_array($res)){ $tests = explode(',', $row['tests']);

> foreach($tests as $test){ if ($test =="") {

> continue; } echo '<div class="test-res" style="margin-top:10px;"> <form action="" method="post" role="form"

> class="form-horizontal"> <div class="form-group">

> <label for= "test-col" class="form-label col-md-2">'.$test.' test</label>

> <div class="col-md-10">

> <textarea class="form-control" rows="3" name="test-col[]" placeholder="Test result"> </textarea>

> </div> </div> </form> </div>'; '<br /> <br />' ; } } } echo '<form action="" method="post"> <button type="submit" class="btn

> btn-success col-md-offset-5" name="sub-res">Send Result</button>

> </form>'; ?>

>

> //to insert textarea values in db <?php if

> (isset($_POST['sub-res'])){ $conn = mysqli_connect('localhost',

> 'root', '', 'myDb'); foreach ($_POST ['test-col'] as $values){

> $test_results = implode("<br>", $values); } $ins =

> mysqli_query($conn, "INSERT INTO bal (results) VALUES

> ('$test_results') WHERE users_id = '$users_id'"); if (!$ins){

> die(mysqli_error());

> }else{ echo '<div class="alert alert-success">Successfully sent</div>'; } }

> ?>

Link to comment
Share on other sites

I won't help someone who has such a woefully incorrect db structure. One should NEVER store data in comma-separated lists in a table column. Never. Not Ever.

 

Break the data out into distinct columns if the pieces are truly "different" bits of data. If they are the same, then they should be placed into a single column (ie, multiple rows) of a secondary table that is linked to the first by a common key or foreign key value.

 

You should do some reading on a normalized database.

 

Good luck.

 

PS - When you next post code here, please use the appropriate code tags such as php and \php wrapped in square brackets.

Edited by ginerjm
Link to comment
Share on other sites

Thanks for your reply. The thing is those comma separated values were inputs from a multiple-select dropdown from another page, imploded into the same column.Now those values are to be called individually on another form as labels for textarea field so users can fill.So what do u think i should do at this stage please?No solution but to rebuild d database?To rebuild the database I will appreciate ur assistance knowing

Link to comment
Share on other sites

Thanks for your reply. The thing is those comma separated values were inputs from a multiple-select dropdown from another page, imploded into the same column.Now those values are to be called individually on another form as labels for textarea field so users can fill.So what do u think i should do at this stage please?No solution but to rebuild d database?I will appreciate ur assistance pls

Link to comment
Share on other sites

The only solution is to learn how the relational database model works (this is the basis of SQL) and then create a proper data layout.

 

Your task of assigning results to tests is trivial and should be a matter of minutes. But your CSVs have turned it into a nightmare of implode/explode gymnastics and undefined indexes. Now imagine having to solve a difficult problem.

 

The code also needs works. Don't just throw all your PHPSQLHTMLCSS into one big blob of text. Structure the code. CSS belongs into a separate file; the PHP code belongs on top of the script, then comes the HTML markup:

<?php

// PHP code goes here; this is where you process the request, do your queries etc.

?>
<!-- HTML markup goes here -->
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Title</title>
        <link rel="stylesheet" href="/path/to/external/stylesheet.css">
    </head>
    <body>

    </body>
</html>

Dynamic queries are implemented with prepared statements. Manual escaping is fragile, messy and obsolete.

Link to comment
Share on other sites

Thanks @ ginerjm & jacques.But u guys can do without letting me feel thats d worst piece of code ever written even if it was. I'm a newbie in php who just started out with Google and YouTube resources & u guys r doing a great job of discouraging me.I wrote in here coz I'm not perfect like u guys & have d desire to learn and become better.You guys didnt just wake up one day knowing all u do today. I really don't think the way u just replied is d way to help..but thanks anyway

Link to comment
Share on other sites

The point I was making is there is a learning process that involves some reading and some education before you jump into coding up apps like you are doing. And if someone fed you this crappy db design to work with obviously they also need to do some of that reading.

 

Programming and writing viable apps is not simply copying some stranger's code and sticking into your concept of a solution. It is work, just like learning auto repair or house painting or even gardening. My response to you was so negative because it was so far off-base already. It wasn't personal.

Edited by ginerjm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.