0

I am trying to send my dynamic table to mysql database but I am having difficulties. I've tried using a for to get it to send but I'm not getting very far. At the moment I am just displaying it but I really want to send it to mysql. I want all the data to be in one row with the rest of the data like test case name, test case number and so on. But the dynamic table must be put into the same row where the rest of the data goes.

Below you will see what I have done and maybe you can see what my intentions are...

<div class="col-md-6">
  <label>Test Case Number:</label>

<?php
  $sql = "SELECT test_case_number FROM qa_testing_application ORDER BY id desc LIMIT 1";
  $result = mysqli_query($database, $sql) or trigger_error("SQL", E_USER_ERROR);

  while ($row = mysqli_fetch_assoc($result)) { 
?>
  <input class="form-control" style="display: none" type="text" name="test_case_number" readonly="readonly" value="<?php echo $row['test_case_number']+3?>">
<?php } ?>
  <label>Company Name:</label>
  <input class="form-control" type="text"  name="test_case_company_name"/>
  <label>Tester:</label>
  <input class="form-control" style="display: none" type="text"  name="user_username" value="<?php echo $user_username ?>" readonly/>
  <label>System:</label>
  <input class="form-control" type="text"  name="test_case_system"/>
  <label>URL:</label>
  <input class="form-control" type="text"  name="test_case_url"/>
</div>

<div class="col-md-12" style="border: 1px solid #28415b; padding-bottom: 12px; padding-top: 12px;margin-top: 20px; margin-bottom: 15px">
  <p>
      <INPUT class="btn btn-primary ladda-button" type="button" value="Add row" onclick="addRow('dataTable')" />
      <INPUT class="btn btn-primary ladda-button" type="button" value="Delete row" onclick="deleteRow('dataTable')" />
  </p>
  <div class="clear"></div>
  <p>'All fields below are compatible to use markdowns for editing' </p> 



<table class="table table-hover">
    <thead>
    <tr>
        <th style="width: 15px">Chk</th>
        <th style="width: 335px">Action:</th>
        <th style="width: 326px;">Expected System Response:</th>
        <th style="width: 151px;">Pass/ Fail</th>
        <th>Comment</th>
    </tr>
    </thead>
</table>
<table id="dataTable" class="table table-hover">
    <tbody>
    <tr>
        <td style="width:20px;"><INPUT type="checkbox" name="chk[]" id="chk"/></td>
        <td><INPUT class="form-control" type="text" name="step[]"  autocomplete="on" placeholder="Action" required/></td>
        <td><INPUT class="form-control" type="text" name="url[]"  autocomplete="on" placeholder="Expected Outcome" required/></td>
        <td>
            <select name="passfail[]" class="form-control" style="width:120px;">
                <OPTION value="Pass">....</OPTION>
                <OPTION value="Pass">Pass</OPTION>
                <OPTION value="Fail">Fail</OPTION>
            </select>
        </td>
        <td>
            <TEXTAREA class="form-control" type="text" name="comment[]" rows="2" cols="15" placeholder="Comment" required></TEXTAREA>
        </td>
    </tr>
    </tbody>
</table>

Thats my table that i am dynamically making...

In my PHP file i display the dynamic table as follows:

<table class="table table-bordered">
    <thead>
          <tr>
          <td>Step</td>
          <td>process</td>
          <td>Expected System Response</td>
          <td>
              <center>Pass/ Fail</center>
          </td>
          <td>Comment</td>
          </tr>
    </thead>
<?php
    if (isset($_POST)) {
    $step = $_REQUEST['step'];
    $url = $_REQUEST['url'];
    $pass_fail = $_REQUEST['passfail'];
    $comment = $_REQUEST['comment'];
    $countPass = 0;
    $countFail = 0;
    foreach ($step as $key => $row) {
?>
    <tbody>
    <tr>
        <td><?php echo $key + 1; ?></td>
        <td><?php echo $step[$key]; ?></td>
        <td><?php echo $url[$key]; ?></td>
        <td style="color:<?php if ($pass_fail[$key] == 'Fail') {
            echo 'color: red';
        } else {
            echo 'limegreen';
        } ?>"><b>
                <center><?php echo $pass_fail[$key]; ?></center>
            </b></td>
        <td><?php echo $comment[$key]; ?>
        </td>
    </tr>
    </tbody>
</table>

Now how do i insert it into mysql???

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

1 Answers1

1

So basically what you need to do to insert it into your database is doing a sql query in PHP.

Let me show you an code example:

//establish db connection
$con=mysqli_connect("dbhost","username","dbpassword","dbname");

$sql = "INSERT INTO tablename (name_of_row1, 
                               name_of_row2, 
                               name_of_row3) 
                       VALUES ('".$value1."',
                               '".$value2."',
                               '".$value3."')";

mysqli_query($con, $sql);
mysqli_close($con);

So you have to open a sql connection and then insert your data to the table you want. I hope this it's clear enough. Now if the PHP file gets called the SQL query is beeing made.

Marcel Wasilewski
  • 2,519
  • 1
  • 17
  • 34
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 07 '15 at 14:41
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Please try to teach good habits in code examples. – Jay Blanchard Dec 07 '15 at 14:41
  • i get this error Notice: Array to string conversion in C:\Users\Jolene\Desktop\qa_testing_application\testingpostdynamictable.php on line 11 – Matthew Stuart Dec 07 '15 at 14:51
  • So it seems you got a syntax error in your code. You can show us your code? And please consider reading about SQL Injection Attacks like Jay Blanchard metioned in the comments. And have alook at my post, i made an edit at the mysqli_query. – Marcel Wasilewski Dec 07 '15 at 14:53