Page 1 of 1

How to insert into database using an array

Posted: Sun Jul 27, 2014 7:19 am
by shail
Warm greetings to all,
I am new to programming, in initial learning stage.
I have 10 questions in my database, and i am displaying them to user on his page.
When they enters the answer to it, that needs to go to database. My problem is that i haven't used array for INSERT ever. I googled on this too but not truly understood whats was going on. Can someone please help me on this?

Code: Select all

<?php
include_once"db_conx.php";
$sql = "SELECT * FROM mymake ";
$query = mysqli_query($db_conx,$sql);
$numrows = mysqli_num_rows($query);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
form > select { width:280px;} 
</style>
</head>
<body>
<form>
<?php
if ($numrows > 0){
  while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
?>

    <input type="text" name="q<?php echo $row["id"];?>" placeholder="<?php echo $row["question"];?>"  size="82" disabled >
    <select name="response" required="required">
      <option value=""></option>
      <option value="<?php echo $row["option1"]; ?>"><?php echo $row["option1"]; ?></option>
      <option value="<?php echo $row["option1"]; ?>"><?php echo $row["option1"]; ?></option>
      <option value="<?php echo $row["option2"]; ?>"><?php echo $row["option2"]; ?></option>
      <option value="<?php echo $row["option3"]; ?>"><?php echo $row["option3"]; ?></option>
    </select>
    <br>
<?php
  }
}
?>
<br /><input type="submit" value="Ready to Go" name="submit" />
<input type="reset" value="Reset" name="reset" />
</form>

</body>
</html>

Thanks
Shail

Re: How to insert into database using an array

Posted: Sun Jul 27, 2014 8:16 am
by Celauran
How are you keeping track of questions/answers? You're putting the question ID in an input field and then asking for an answer in a field just named response. Response isn't an array, so you're going to have a bunch of fields with the same name, which is going to be problematic once you submit the form.

Re: How to insert into database using an array

Posted: Sun Jul 27, 2014 8:29 am
by shail
hi..
let me brief my condition here.
i have a table like:
ID USERNAME Question1 Question2 Question3....question10 Answer1 Answer2......Answer10

In another table i have a set of ten questions.

above is my code which displays the question from second table to user webpage. then user has to fill them and as per their response it will be inserted into table 1. Can someone please guide me how i can achieve this. I havent used arrays before for insertion. there are methods in google which tells to enter data into question answer but not how i want.

May be there isnt any possible method for it. My basic idea is to store the 10question from table2 to table1 with answers for everyuser. i will really appreciate if there is anyother method to do it

Re: How to insert into database using an array

Posted: Sun Jul 27, 2014 6:25 pm
by Celauran
shail wrote: i have a table like:
ID USERNAME Question1 Question2 Question3....question10 Answer1 Answer2......Answer10

In another table i have a set of ten questions.
That doesn't look like an efficient design. Might make more sense to use a table for questions, a table for possible answers with the ID of the question they relate to as a foreign key, and then a join table for user/question/answer.

As for "using arrays for insertion", it still isn't clear to me what you're trying to do. Probably the easiest approach would be to have question/answer stored in a multidimensional array, prepare your statement, then execute while iterating over the array.