Link to home
Start Free TrialLog in
Avatar of Jazzy 1012
Jazzy 1012

asked on

insert into database through form with dynamic fields.

<?php
require "connection.php";
error_reporting(E_ALL);
ini_set('display_errors',1);
$username = "jasmine";
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<link href = "http://fonts.googleapis.com/css?family=Roboto:400">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<body>


  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">ADD</h4>
        </div>
        <div class="modal-body">
          <p>
          <?php
if(isset($_POST['add'])){
      
$result2= mysqli_query($conn,"SELECT * FROM users WHERE username = '$username'");
$row2 = mysqli_fetch_row($result2);
if($_POST['add'] != "")
{

//// WHAT DO I DO HERE?
}
}
?>

<form action="" method="post">
<table>
<thead>
   <?php 
    $query = mysqli_query($conn,"SHOW columns FROM `call`");
    // Loop over all result rows
    while($row = mysqli_fetch_array($query))
    {
        echo '<th>';
        echo $row["Field"];
        echo '</th>';
    }
 ?>
 </thead>
 <tbody>
 <?php 
    $query4="SELECT * from `call` WHERE `Generated By` = '$username'";

    $result4=  mysqli_query($conn, $query4);
    while($row4 = mysqli_fetch_assoc($result4)){
        echo '<tr>';
        foreach($row4 as $values3){
        echo '<td>';
        echo '<input type="text" name="field_name">';
        echo '</td>';
        }
        ?>
        <td>
        <input type="submit" name="add" value="Add">
        </td>
        <?php
        echo '</tr>';}
 ?>
 </tbody>
</table>
</form>
          
          
          
          </p>
        </div>
      </div>
      
    </div>
  </div>
  


</body>
</html>

Open in new window


I have this code, it should add when I click the add button, but how am I suppose to write the "insert" query when I don't know the field names that I want it to insert into the values, could anyone help me with this?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jazzy 1012
Jazzy 1012

ASKER

Ok i have this so far but its still not working, can you see what the problem is?
<?php
require "connection.php";
error_reporting(E_ALL);
ini_set('display_errors',1);
$username = "jasmine";
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<link href = "http://fonts.googleapis.com/css?family=Roboto:400">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<style>
.modal-dialog{
	width:100%;
	overflow: scroll;
	
}

</style>


</head>
<body>


  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">ADD</h4>
        </div>
        <div class="modal-body">
          <p>


<form action="" method="post">
<table>
<thead>
   <?php 
    $query = mysqli_query($conn,"SHOW columns FROM `rsvp`");
    // Loop over all result rows
    while($row = mysqli_fetch_array($query))
    {
        echo '<th>';
        echo $row["Field"];
        echo '</th>';
    }
 ?>
 </thead>
 <tbody>
 <?php 
    $query4="SELECT * from `rsvp` WHERE `Gathered By` = '$username'";

    $result4=  mysqli_query($conn, $query4);
    while($row4 = mysqli_fetch_assoc($result4)){
        echo '<tr>';
        foreach($row4 as $values3){
        echo '<td>';
        echo '<input type="text" name="' .$row['Field'] .'">';
        echo '</td>';
        }
        ?>
        <td>
        <input type="submit" name="add" value="Add">
        </td>
        <?php
        echo '</tr>';}
 ?>
 </tbody>
</table>
</form>
          
<?php
if(isset($_POST['add'])){
      
$result2= mysqli_query($conn,"SELECT * FROM users WHERE username = '$username'");
$row2 = mysqli_fetch_row($result2);

$row['Field'] = $columns;
mysqli_query($conn,'INSERT INTO table (text, category) VALUES '.implode(',', $columns));




//// WHAT DO I DO HERE?

}
?>  
          
          </p>
        </div>
      </div>
      
    </div>
  </div>
  


</body>
</html>

Open in new window


I get these errors
Notice: Undefined variable: columns

Warning: implode(): Invalid arguments 

Open in new window