Link to home
Start Free TrialLog in
Avatar of Craig Brzyski
Craig Brzyski

asked on

HTML Form creates Multidimensional Array, I need to insert this to mysql using php

Long Title, but that is exactly what I am trying to do.

I need a php script that will insert all of the inputs to a mysql table, I have tried to make one using a variety of methods, but none have worked, or they would insert the last value into the table.


Currently I have this HTML code:
		<table id="register">
		<form action="processing_games.php" method="post"/>
		<tr>
			<td><p>Date<br>(m/dd/yy)</p></td>
			<td><p>Time<br>(h:mm AM/PM)</p></td>
			<td><p>Opponent</p></td>
			<td><p>Location<br>(Rink Name)</p></td>
			<td><p>Conference<br>Game?</p></td>
			<td><p>Home or Away<br>Game?</p></td>
		</tr>
		<tr>
			<td><input type="date" name="inputs[0][date]"></td>
			<td><input type="time" name="inputs[0][time]"></td>
			<td><input type="text" name="inputs[0][opponent]"></td>
			<td><input type="text" name="inputs[0][location]"></td>
			<td><input type="radio" name="inputs[0][conference]" value="+"> Conference
			<br>
			<input type="radio" name="inputs[0][conference]" value=""> Non-Conference </td>
			<td><input type="radio" name="inputs[0][home_away]" value=" vs "> Home
			<br>
			<input type="radio" name="inputs[0][home_away]" value=" @ "> Away</td>
		</tr>
		<tr>
			<td><input type="date" name="inputs[1][date]"></td>
			<td><input type="time" name="inputs[1][time]"></td>
			<td><input type="text" name="inputs[1][opponent]"></td>
			<td><input type="text" name="inputs[1][location]"></td>
			<td><input type="radio" name="inputs[1][conference]" value="+"> Conference
			<br>
			<input type="radio" name="inputs[1][conference]" value=""> Non-Conference </td>
			<td><input type="radio" name="inputs[1][home_away]" value=" vs "> Home
			<br>
			<input type="radio" name="inputs[1][home_away]" value=" @ "> Away</td>
		</tr>
		<tr>
			<td><input type="date" name="inputs[2][date]"></td>
			<td><input type="time" name="inputs[2][time]"></td>
			<td><input type="text" name="inputs[2][opponent]"></td>
			<td><input type="text" name="inputs[2][location]"></td>
			<td><input type="radio" name="inputs[2][conference]" value="+"> Conference
			<br>
			<input type="radio" name="inputs[2][conference]" value=""> Non-Conference </td>
			<td><input type="radio" name="inputs[2][home_away]" value=" vs "> Home
			<br>
			<input type="radio" name="inputs[2][home_away]" value=" @ "> Away</td>
		</tr>

		</table>
		<br><br>
		<center>
		<input type="submit" value="Submit"/>
		</form>
		</center>

Open in new window


Here is my current php file:
<?php
include '../connection.php';

$array = $_POST['inputs'];

foreach($array as $value=>$data)
{
$game_date = $data['date'];
$game_time = $data['time'];
$value3 = $data['opponent'];
$value4 = $data['location'];
$value5 = $data['conference'];
$value6 = $data['home_away'];

$value = date('n/j/y', strtotime($game_date));
$value2 = date('g:i A', strtotime($game_time));
$sql = "INSERT INTO schedule (date, time, opponent, location, conference, home_away) VALUES ('$value', '$value2', '$value3', '$value4', '$value5', '$value6')";
}

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

mysqli_close($conn); 
?>

Open in new window

Avatar of Scott Fell
Scott Fell
Flag of United States of America image

<input type="date" name="inputs[1][date]">
<input type="date" name="inputs[2][date]">

Open in new window


should be
<input type="date" name="date[]">
<input type="date" name="date[]">

Open in new window


Then
echo $_POST['date'][0];
echo $_POST['date'][1];

Open in new window

See if this helps get you on track.
Avatar of Craig Brzyski
Craig Brzyski

ASKER

Tried that, I was unable to make it work.
You will have to give me more details than that.

Let's start with a basic page that only has the code below.
<form method="post" action="process.php">
<input type="date" name="date[]">
<input type="date" name="date[]">
<button type="submit">Submit</button>	
	
</form>

Open in new window

Then create a page called process.php
if(isset($_POST['date'])) {
     
      print_r($_POST);

      echo "<hr>";

      $date1 = $_POST['date'][0];
      $date2 = $_POST['date'][1];

      echo "date1 = ".$date1."<br>date1 = ".$date2;

} else {

	echo "Nothing posted";
}

Open in new window


This is very basic and in your actual code you will want to validate your date field.    I have tested and the output is as expected.
When I tried this, the form did not allow me to select the radio buttons for the second input. It also failed to input either of the dates and times, instead it showed 12/31/69 and 11:59pm as the date and time while the other text inputs were blank.

Here is what I have, it works now and I am working on changing the date and time format before entering them into mysql.

HTML:
  <html>

  <body>
    <form method="post">
      <table>
        <tr>
          <td>
            <p>Date<br>(m/dd/yy)</p>
          </td>
          <td>
            <p>Time<br>(h:mm AM/PM)</p>
          </td>
          <td>
            <p>Opponent</p>
          </td>
          <td>
            <p>Location<br>(Rink Name)</p>
          </td>
          <td>
            <p>Conference<br>Game?</p>
          </td>
          <td>
            <p>Home or Away<br>Game?</p>
          </td>
        </tr>
        <tr>
          <td><input type="date" name="items[0][date]"></td>
          <td><input type="time" name="items[0][time]"></td>
          <td><input type="text" name="items[0][opponent]"></td>
          <td><input type="text" name="items[0][location]"></td>
          <td><input type="radio" name="items[0][conference]" value="+"> Conference
            <br>
            <input type="radio" name="items[0][conference]" value=""> Non-Conference </td>
          <td><input type="radio" name="items[0][home_away]" value=" vs "> Home
            <br>
            <input type="radio" name="items[0][home_away]" value=" @ "> Away</td>
        </tr>
        <tr>
          <td><input type="date" name="items[1][date]"></td>
          <td><input type="time" name="items[1][time]"></td>
          <td><input type="text" name="items[1][opponent]"></td>
          <td><input type="text" name="items[1][location]"></td>
          <td><input type="radio" name="items[1][conference]" value="+"> Conference
            <br>
            <input type="radio" name="items[1][conference]" value=""> Non-Conference </td>
          <td><input type="radio" name="items[1][home_away]" value=" vs "> Home
            <br>
            <input type="radio" name="items[1][home_away]" value=" @ "> Away</td>
        </tr>
      </table>
      <input type="submit" name="submit" value="submit" />
    </form>
  </body>

  </html>

Open in new window


PHP:
<?php

$postItems = null;
if (isset($_POST['items'])) {
    $postItems = $_POST['items'];

    foreach($postItems as $item) {

        var_dump($item); // this is my check to see that all inputs are being seen, which they are, both in the 1st and 2nd array of values.

        $sql = 'INSERT INTO schedule (date, time, opponent, location, conference, home_away) 
            VALUES ("'.$item['date'].'", "'.$item['time'].'", "'. $item['opponent'].'", "'.$item['location'].'", "'.$item['conference'].'", "'.$item['home_away'].'")';

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}
mysqli_close($conn); 
?>

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.