0

I have a script that allows for multiple rows to be created on the page and then I need to have the rows counted on the post and then each row inserted into the table.

I have the GUI working to where users can add or remove rows as needed yet when I submit no data is being written to the table. I have tried altering the script for the post to be straight '$variables' and it works to write but only writes the first row.

I have attached the action script that I am using from WebLesson that works great for one field but for more than one I am at a loss for what to try.

//includes
include '----';
include '---'; 
session_start();

$number = count($_POST['name']);

echo $_POST['name'] . "<br>";
echo $number . "<br>";
 if($number >= 1)
{
 echo $_POST['pasid'];

$conn = mysqli_connect($servername, $username, $password, $dbname);

$id = $_POST['pasid'];
$name = $_POST['name'];
$dose = $_POST['dose'];
$dir = $_POST['directions'];
$time = $_POST['time'];

echo $i;
for($i=0; $i<$number; $i++)
    {
    if(trim($_POST["name"][$i] != ''))
    {

        $sql = "INSERT INTO meds (id, name, dose, directions) 
VALUES('".mysqli_real_escape_string($_POST["pasid"][$i])."', 
'".mysqli_real_escape_string($_POST["name"][$i])."', 
'".mysqli_real_escape_string($_POST["dose"][$i])."', 
'".mysqli_real_escape_string($_POST["directions"][$i])."', 
'".mysqli_real_escape_string($_POST["time"][$i])."') " ;
        mysqli_query($conn, $sql);
    }
}
echo "Data Inserted";
}
else
{
 die("Connection failed: " . mysqli_connect_error());
}

I would like this to count how many rows were posted and then submit each row to the table.

Picture of the UI:

enter image description here

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • The '_ POST' is per the template that Web lesson had listed and I followed that. I would prefer to use the variables that I have defined above – Concentric Dec 27 '18 at 05:18
  • https://stackoverflow.com/questions/22304930/is-mysqli-real-escape-string-safe Please read the responses to that question. Do not use mysqli_real_escape_string the way you are using it. It is not as safe as you have been lead to believe. – thinsoldier Dec 27 '18 at 05:41
  • If you could post a screenshot of you UI I might be able to understand your question better. My best guess is using arrays and foreach will result in easier to follow code for your situation. For php beginners I suggest reading all the nitty gritty details about PDO and MySQLi on https://phpdelusions.net/ but don't actually use PDO and MySQLi for anything that is easily handled by an easy to use library like http://j4mie.github.io/idiormandparis/ – thinsoldier Dec 27 '18 at 05:44

1 Answers1

0

You can generate unique name attributes for each form element and then loop through them in PHP. The easiest way to do that would probably be increment a number at the end of the name attribute each time the user creates a new row. You will have to do that with Javascript.

var suffix = 0;
function addNewRow(){
    ...
    <input type="text" name="pasid_${i}"/>   //Add incriminating suffix
    ...
    suffix++;     //Increment suffix for next time
}

Then your PHP would look like

$suffix = 0;
while(isset($_POST['pasid' . $suffix])){   //Keep looping until no more POST value

    //Process row $suffix, referencing all the data like $_POST['field_name'. $suffix]

    $suffix++;                            //Increment suffix for the next field
}

Make sure the field you are checking for in the while loop is required or else the user might omit an input and the loop would terminate prematurely. You could also check multiple $_POST indexes in the while loop if non of the fields are required.

Parsonswy
  • 13
  • 1
  • 5