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

asked on

Insert values are dynamic

I have this code:
<?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


As you can see it gets me the column names of my table along with the text for the username ="jasmine", I want to be able to allow users to add another row to the columns and for the "gathered by" area to always be static which is jamine or whatever the name is of the person logged in. I think I got most of the steps down, Im just stuck on the insert query since I won't know any static names, is there a way to do it even without me knowing the column name?
Right now I have these errors:
Notice: Undefined variable: columns

Notice: Undefined variable: columns

Warning: implode(): Invalid arguments passed
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Lines 69-74... Try something more like this.  You want column names, right?
    while($row4 = mysqli_fetch_assoc($result4)){
        echo '<tr>';
        foreach($row4 as $key => $values3){
        echo '<td>';
        echo '<input type="text" name="' .$row4[$key] .'" />';
        echo '</td>';

Open in new window

Avatar of Jazzy 1012
Jazzy 1012

ASKER

Yeah But im still getting the same errors I think I have something wrong in the query part.

<?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?

}
?>  

Open in new window

First, we need to fix this.  In the snippet, $columns is an undefined variable, and $row["Field"] is an unused variable.  You do not want to have extraneous code and variables in your scripts.
$row["Field"] = $columns;

Open in new window


When your script runs a query, you can test for success or failure, and print out the error messages.  Follow the code examples in this article.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html 
Look in the article for Run the CREATE TABLE Query and Handle Errors/Exceptions.  You will need to make some adjustments because you're using procedural code, but you can still use the error testing and visualization techniques.
Avatar of Julian Hansen
There are many things I don't understand about this code
1. Your loop to output the headers has this
while($row = mysqli_fetch_array($query))

Open in new window

Then later in your code to output fields you reference $row["Field"] but this will now only refer to the last $row from the header loop which will have exhausted the array and left $row empty.

2. Consider this line.
        echo '<input type="text" name="' .$row['Field'] .'">';

Open in new window

In the above snippet you are in a loop - each time it goes round name will be set to $row["Field"] which as a loop invariant - it is not altered in this loop so will be the same value. What you probably wanted was
    while($row4 = $result4->fetch_assoc()){
        echo '<tr>';
        foreach($row4 as $field=>$values3){
        echo '<td>';
        echo '<input type="text" name="' .$field .'">';
        echo '</td>';
        }

Open in new window

But event this does not make sense because if your query returns more than one row you will have multiple <input> controls with the same name

Can you explain a bit more about what you are expecting this page to do.
I want this page to display all the columns of the table called "rsvp" and with one input box under each column (therefore just added on row), and after I write the information and I click add, it inserts the information in my database depending on the column it is on, it is similar to my editing field. But how can I do it with insert?
Will there only be one row - or is it possible to have multiple records returned?
Here I can make it more clear:
in page 1 have this code:
<?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="try2.php" method="post">
<table>
<thead>
   <?php 
    $query = mysqli_query($conn,"SHOW columns FROM `rsvp`");
    // Loop over all result rows
    while($row = mysqli_fetch_array($query))
    {
     if($row["Field"] == 'id')
     {
			continue;
	 }   
		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 = $result4->fetch_assoc()){
        echo '<tr>';
        foreach($row4 as $field=>$values3)
        {
			if($field == 'id')
			{
				continue;
			}
        	echo '<td>';
        	echo '<input type="text" name="' .$field .'">';
        	echo '</td>';
        }
        ?>
        <td>
        <input type="submit" name="add" value="Add">
        </td>
        <?php
        echo '</tr>';}
 ?>
 </tbody>
</table>
</form>

        
        </div>
      </div>
      
    </div>
  </div>
  


</body>
</html>

Open in new window

In this code, I get the column names along with the input text under each so I can insert the information, after I click add, it goes to this page:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
$username= $_SESSION['username'];
if($_SESSION['username'] == "")
{
	header("Location: http://markitlive.com/users/");
}

require "connection.php";

if(isset($_POST['add'])){
      
$result2= mysqli_query($conn,"SELECT * FROM users WHERE username = '$username'");
$row2 = mysqli_fetch_row($result2);

$field= $columns;
mysqli_query($conn,'INSERT INTO table (text, category) VALUES '.implode(',', $columns));




//// WHAT DO I DO HERE?

}
?>  

Open in new window


Where I insert the information to the database
One row, do you want to see an image of the page?
Also can I have the input box for the name already prefilled with the username and the date prefilled with todays date?
I did the date and username, but how can I do the insert?
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa 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