Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to store a dynamically added content to database
    primarykey
    data
    text
    <p>I am having a few trouble storing my content into the database. The table/rows are dynamically generated using jquery! but when i click the submit button, just a row of numbers is stored in my database table even when i store text values. Here is my jquery code:</p> <pre><code>function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var cell1 = row.insertCell(0); var element1 = document.createElement("input"); element1.type = "checkbox"; element1.name="chkbox[]"; cell1.appendChild(element1); var cell2 = row.insertCell(1); cell2.innerHTML = "&lt;input type='text' name='Obstacle'&gt;"; var cell3 = row.insertCell(2); cell3.innerHTML = "&lt;select name='Likelihood'&gt; &lt;option value='1'&gt;Low&lt;/option&gt; &lt;option value='2'&gt;Medium&lt;/option&gt;&lt;option value='3'&gt;High&lt;/option&gt;/&gt;"; var cell4 = row.insertCell(3); cell4.innerHTML = "&lt;select name='Severity'&gt; &lt;option value='1'&gt;Low&lt;/option&gt; &lt;option value='2'&gt;Medium&lt;/option&gt;&lt;option value='3'&gt;High&lt;/option&gt;/&gt;"; var cell5 = row.insertCell(4); cell5.innerHTML = "&lt;select name='Priority'&gt; &lt;option value='1'&gt;Low&lt;/option&gt; &lt;option value='2'&gt;Medium&lt;/option&gt;&lt;option value='3'&gt;High&lt;/option&gt;/&gt;"; } </code></pre> <p>Here is my html:</p> <pre><code>&lt;div class="compTable"&gt; &lt;form action="" method="post"&gt; &lt;TABLE width="425" border="1"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th width="4"&gt;&lt;/th&gt; &lt;th width="100"&gt;Obstacle&lt;/th&gt; &lt;th width="50"&gt;Likelihood&lt;/th&gt; &lt;th width="50"&gt;Severity&lt;/th&gt; &lt;th width="50"&gt;Priotity&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody id="dataTable"&gt; &lt;/tbody&gt; &lt;/TABLE&gt; &lt;INPUT type="submit" value="Save" name="submit" /&gt; &lt;INPUT type="button" value="Add Obstacle" onClick="addRow('dataTable')" /&gt; &lt;INPUT type="button" value="Delete Obstacle" onClick="deleteRow('dataTable')" /&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>and finally my php code:</p> <pre><code>if(isset($_POST['submit'])){ $user = $_SESSION['user']['FullName']; $obstacleDec = isset($_POST["Obstacle"][$key]); $likelihoodScale = isset($_POST["Likelihood"][$key]); $severityScale = isset($_POST["Severity"][$key]); $priorityScale = isset($_POST["Priority"]); foreach ($_POST as $key =&gt; $value){ $sql = "INSERT INTO obstacles (ObstacleID, ObstacleDescription, Priority, Uncertainty, Severity, UserName, ComplianceID, Accepted) VALUES (:ID, :obstacleDec, :priorityScale, :likelihoodScale, :severityScale, :aUser, :compID, :accepted)"; try{ $stmt = $db-&gt;prepare($sql); $result= $stmt-&gt;execute(array(':ID'=&gt;'', ':obstacleDec'=&gt;$obstacleDec,':likelihoodScale'=&gt;$likelihoodScale, ':severityScale'=&gt;$severityScale,':priorityScale'=&gt;$priorityScale, ':aUser'=&gt;$user, ':compID'=&gt;'', ':accepted'=&gt;'' )); } catch(PDOException $ex){ die("Failed to run query: " . $ex-&gt;getMessage()); } header("Location: obstacles.php"); die("Redirecting to obstacles.php"); if(!$sql){ die("error inserting values"); } else{ die("successfully inserted the values"); } } } </code></pre> <p>Thanks for the help</p>
    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.
 

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