Note that there are some explanatory texts on larger screens.

plurals
  1. POsingle html form insert into multiple mysql tables with one to many relationship
    primarykey
    data
    text
    <p>So i know that this is a popular question and has a lot of responses already.However, my challenge is slightly different which has been bugging me for days now.I have an html form,which is meant to store data into multiple tables in my database.(using phpMyAdmin for now).What is a bit different is,this form allows the user to create survey questions,along with all the possible answers for that question and the preferred input type for each question and store them in mysql db.Let me show you my html code:</p> <pre><code> &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Create a new Survey &lt;/title&gt; &lt;script type="text/javascript" src="script.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body style= "background: #D1D0CE"&gt; &lt;h1 style= "text-align: center; font-family: Garamond; color: brown"&gt; Create a new survey&lt;/h1&gt;&lt;br /&gt; &lt;p style="text-align:center"&gt;&lt;img src = "logo.png"/&gt;&lt;/p&gt; &lt;form action="insert.php" method="post"&gt; &lt;fieldset class="row1"&gt; &lt;table id="formTable" class="form" &gt; &lt;tbody&gt; &lt;p style="font-family:Garamond; color:brown"&gt; &lt;strong&gt;Form Information&lt;/strong&gt; &lt;/p&gt; &lt;p&gt;(Please enter your form details below.)&lt;/p&gt; &lt;tr&gt; &lt;td&gt; Form Name : &lt;/td&gt;&lt;br /&gt;&lt;br /&gt; &lt;td&gt; &lt;input type="text" size="12" maxlength="40" name="formName"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Date : &lt;/td&gt;&lt;br /&gt; &lt;td&gt; &lt;input type="date" size="12" maxlength="40" name="formDate"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;fieldset class="row2"&gt; &lt;table id="questionTable" class="form" &gt; &lt;p style= "font-family: Garamond; color: brown"&gt; &lt;strong&gt;Question Details&lt;/strong&gt; &lt;/p&gt; &lt;p&gt; &lt;input type="button" value="Add Question" onClick="addRow('questionTable')" /&gt; &lt;input type="button" value="Remove Question" onClick="deleteRow('questionTable')" /&gt; &lt;/p&gt; &lt;tr&gt; &lt;p&gt; &lt;td&gt;&lt;input type="checkbox" name="chk[]"/&gt;&lt;/td&gt; &lt;td&gt; &lt;label&gt;Question : &lt;/label&gt; &lt;input type="text" size = "16" maxlength= "50" name="description"/&gt; &lt;/td&gt; &lt;td&gt; &lt;label&gt;Input Type :&lt;/label&gt; &lt;select id="widgetType" name="widgetType" required="required"&gt; &lt;option&gt;....&lt;/option&gt; &lt;option&gt;radio button&lt;/option&gt; &lt;option&gt;check box&lt;/option&gt; &lt;option&gt;Edit Text&lt;/option&gt; &lt;option&gt;Spinner&lt;/option&gt; &lt;option&gt;Rating bar&lt;/option&gt; &lt;/select&gt; &lt;/td&gt; &lt;/p&gt; &lt;/tr&gt; &lt;/table&gt; &lt;fieldset class="row3"&gt; &lt;table id="answerTable" class="form"&gt; &lt;p style= "font-family: Garamond; color: brown"&gt; &lt;strong&gt;Answer Details&lt;/strong&gt; &lt;/p&gt; &lt;p&gt; &lt;input type="button" value="Add Answer" onClick="addAnswer('answerTable')" /&gt; &lt;input type="button" value="Remove Answer" onClick="deleteAnswer('answerTable')" /&gt; &lt;p&gt;(Click to add more answer options.)&lt;/p&gt; &lt;/p&gt; &lt;tr&gt; &lt;p&gt; &lt;td&gt; &lt;input type="checkbox" name="chk[]"/&gt;&lt;/td&gt; &lt;td&gt;&lt;label&gt;Answer : &lt;/label&gt; &lt;input type="text" size="12" maxlength="40" name="answerText"&gt;&lt;/td&gt; &lt;td&gt; &lt;label&gt;Match to Question :&lt;/label&gt; &lt;select id="QuestionNumber" name="question" required="required"&gt; &lt;option&gt;....&lt;/option&gt; &lt;option&gt;Question 1&lt;/option&gt; &lt;option&gt;Question 2&lt;/option&gt; &lt;option&gt;Question 3&lt;/option&gt; &lt;option&gt;Question 4&lt;/option&gt; &lt;option&gt;Question 5&lt;/option&gt; &lt;option&gt;Question 6&lt;/option&gt; &lt;option&gt;Question 7&lt;/option&gt; &lt;option&gt;Question 8&lt;/option&gt; &lt;option&gt;Question 9&lt;/option&gt; &lt;option&gt;Question 10&lt;/option&gt; &lt;/select&gt; &lt;/td&gt; &lt;/p&gt; &lt;/tr&gt; &lt;/table&gt; &lt;fieldset class="row4"&gt; &lt;input type="submit" name="submit" value="Submit"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And the php part:</p> <pre><code> &lt;?php if(isset($_POST['submit'])){ $host = "localhost"; // host of MySQL server $user = "root"; // MySQL user $pwd = ""; // MySQL user's password $db = "webservice"; // database name // Create connection $con = mysqli_connect($host, $user, $pwd , $db); // Check connection if(mysqli_connect_errno($con)) { die("Failed to connect to MySQL: " . mysqli_connect_error()); } $sql = "INSERT INTO form (title,date) VALUES ('".$_POST['formName']."','".$_POST['formDate']."')"; mysqli_query($con , $sql); mysqli_close($con) ; } ?&gt; </code></pre> <p>So in my db,i have a the following structure :</p> <ol> <li><p>Form table</p> <pre><code>-form_id(PK, AI) -tile -date </code></pre></li> <li><p>questions table</p> <pre><code>-question_id(PK, AI) -description -form_id(FK) references form_id in form table </code></pre></li> <li><p>answers table</p> <pre><code>-answer_id(PK, AI) -answer_text -question_id(FK) references question_id from questions </code></pre></li> <li><p>widgets (stores the different widgets/input type chosen from form(radio,text area etc).)</p> <pre><code>-widget_id(PK,AI) -widget_type -question_id(FK) references question_id from questions </code></pre> <p>Each question can have more than one answer but only one widget type.Each answer can only have one question and so can the widgets.So a single question_id may appear many times in answers table.This is where i get stuck:</p> <ul> <li>Creating the form properly to achieve this.The questions and all it's possible answers are sort of dynamic.So you can decide to add questions and input fields for answers as you go.</li> <li>I can dynamically add more questions and more answers,but can't seem to figure out how to link each answer to it's question.</li> <li>I'm also having a challenge writing a proper query that will insert the question_id into the questions table and all the related foreign keys where it's referenced as well.</li> </ul> <p>I would really appreciate some pointers so i can head in the right direction.Really sorry for the long question.Thank you</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. 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