Jump to content

Recommended Posts

I'm going to have a maximum of 5 textareas on a form, all with the same name, but people will insert different information

My text boxes are like this:

<tr>
      <td align="left" valign="top">Analysis:</td>
      <td align="left" valign="top"><textarea name="description[]" id="description[]" cols="65" rows="25"></textarea></td>
    </tr>
<tr>
      <td align="left" valign="top">Analysis:</td>
      <td align="left" valign="top"><textarea name="description[]" id="description[]" cols="65" rows="25"></textarea></td>
    </tr>
<tr>
      <td align="left" valign="top">Analysis:</td>
      <td align="left" valign="top"><textarea name="description[]" id="description[]" cols="65" rows="25"></textarea></td>
    </tr>
<tr>
      <td align="left" valign="top">Analysis:</td>
      <td align="left" valign="top"><textarea name="description[]" id="description[]" cols="65" rows="25"></textarea></td>
    </tr>
<tr>
      <td align="left" valign="top">Analysis:</td>
      <td align="left" valign="top"><textarea name="description[]" id="description[]" cols="65" rows="25"></textarea></td>
    </tr>

 

Then my insert code is this:

$count = count($_POST['description']);

						for ($i = 0; $i < $count; $i++) {							

							$description = stripslashes(mysql_real_escape_string($_POST['description'][$i]));
							$sql = "INSERT INTO picks(description) VALUES(
												'$description');";
												mysql_query($sql)
												or die("Sorry, there was a problem adding picks<br /> ".mysql_error());

							echo "Picks for '".date("F j, Y", strtotime($posted))."' has been uploaded<br />";
							echo "<a href='$back'>Go back to picks page</a><br />$count";
		  				}

 

But this is not working. I only inserts the last one, I need this to be able to count how many boxes have text in it, and insert each one into the database.

 

Can anybody tell me what I'm doing wrong?

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/167131-solved-multiple-textareas/
Share on other sites

ok, I have this to work exactly like it should.

Now, my next question is, how do I get it to only insert if the description is filled out?

 

Meaning, if it's empty, it not insert that one.

 

<?php
foreach($_POST['description'] as $row=>$Act)
					{
					$description=mysql_real_escape_string($Act);
					$posted = $_POST['posted1y']."-".$_POST['posted1m']."-".$_POST['posted1d']; 
					$game = $_POST['posted_y'][$row]."-".$_POST['posted_m'][$row]."-".$_POST['posted_d'][$row];
					$game_time = mysql_real_escape_string($_POST['game_time'][$row]);
					$away_team = mysql_real_escape_string($_POST['away'][$row]);
					$home_team = mysql_real_escape_string($_POST['home'][$row]);
					$sport = mysql_real_escape_string($_POST['sport']);



							$sql = "INSERT INTO picks1(sport, posted, game, game_time, description, away, home) VALUES(
												'$sport',
												'$posted',
												'$game',
												'$game_time',
												'$description',
												'$away_team',
												'$home_team');";
												mysql_query($sql)
												or die("Sorry, there was a problem adding picks<br /> ".mysql_error());

							echo "Picks for '".date("F j, Y", strtotime($posted))."' has been uploaded<br />";
							echo "<a href='$back'>Go back to picks page</a><br />Posted: $posted<br />Game Time: $game<br />";
		  				}
?>

 

Thank in advance

I tried this, but it still inserts all 5 even though I only filled out 1

<?php
if (empty($_POST['description'])) {

				}

				else {


				foreach($_POST['description'] as $row=>$Act)
					{
					$description=mysql_real_escape_string($Act);
					$posted = $_POST['posted1y']."-".$_POST['posted1m']."-".$_POST['posted1d']; 
					$game = $_POST['posted_y'][$row]."-".$_POST['posted_m'][$row]."-".$_POST['posted_d'][$row];
					$game_time = mysql_real_escape_string($_POST['game_time'][$row]);
					$away_team = mysql_real_escape_string($_POST['away'][$row]);
					$home_team = mysql_real_escape_string($_POST['home'][$row]);
					$premiere = mysql_real_escape_string($_POST['premiere'][$row]);
					$sport = mysql_real_escape_string($_POST['sport']);



							$sql = "INSERT INTO picks(sport, posted, game, game_time, description, away, home, premiere) VALUES(
												'$sport',
												'$posted',
												'$game',
												'$game_time',
												'$description',
												'$away_team',
												'$home_team',
												'$premiere');";
												mysql_query($sql)
												or die("Sorry, there was a problem adding picks<br /> ".mysql_error());
		  				}
					}
?>

 

Anybody??

I tried this, but it still inserts all 5 even though I only filled out 1

<?php
if (empty($_POST['description'])) {

				}

				else {


				foreach($_POST['description'] as $row=>$Act)
					{
					$description=mysql_real_escape_string($Act);
					$posted = $_POST['posted1y']."-".$_POST['posted1m']."-".$_POST['posted1d']; 
					$game = $_POST['posted_y'][$row]."-".$_POST['posted_m'][$row]."-".$_POST['posted_d'][$row];
					$game_time = mysql_real_escape_string($_POST['game_time'][$row]);
					$away_team = mysql_real_escape_string($_POST['away'][$row]);
					$home_team = mysql_real_escape_string($_POST['home'][$row]);
					$premiere = mysql_real_escape_string($_POST['premiere'][$row]);
					$sport = mysql_real_escape_string($_POST['sport']);



							$sql = "INSERT INTO picks(sport, posted, game, game_time, description, away, home, premiere) VALUES(
												'$sport',
												'$posted',
												'$game',
												'$game_time',
												'$description',
												'$away_team',
												'$home_team',
												'$premiere');";
												mysql_query($sql)
												or die("Sorry, there was a problem adding picks<br /> ".mysql_error());
		  				}
					}
?>

 

Anybody??

 

 

 

if (!empty($_POST['description'])) {
foreach($_POST['description'] as $row=>$Act) {
	$description=mysql_real_escape_string($Act);
	$posted = $_POST['posted1y']."-".$_POST['posted1m']."-".$_POST['posted1d']; 
	$game = $_POST['posted_y'][$row]."-".$_POST['posted_m'][$row]."-".$_POST['posted_d'][$row];
	$game_time = mysql_real_escape_string($_POST['game_time'][$row]);
	$away_team = mysql_real_escape_string($_POST['away'][$row]);
	$home_team = mysql_real_escape_string($_POST['home'][$row]);
	$premiere = mysql_real_escape_string($_POST['premiere'][$row]);
	$sport = mysql_real_escape_string($_POST['sport']);
	if (!empty($description)) {
		$sql = "INSERT INTO picks(sport, posted, game, game_time, description, away, home, premiere) VALUES(
							'$sport',
							'$posted',
							'$game',
							'$game_time',
							'$description',
							'$away_team',
							'$home_team',
							'$premiere');";
							mysql_query($sql)
							or die("Sorry, there was a problem adding picks<br /> ".mysql_error());
	}
}
}

 

try this :/

o3d,

Using this

if (!empty($_POST['description'])) {
  		foreach($_POST['description'] as $row=>$Act) {
      $description=mysql_real_escape_string($Act);
      $posted1 = $_POST['posted1y']."-".$_POST['posted1m']."-".$_POST['posted1d'];
      $game = $_POST['posted_y'][$row]."-".$_POST['posted_m'][$row]."-".$_POST['posted_d'][$row];
      $game_time = mysql_real_escape_string($_POST['game_time'][$row]);
      $away_team = mysql_real_escape_string($_POST['away'][$row]);
      $home_team = mysql_real_escape_string($_POST['home'][$row]);
      $premiere = mysql_real_escape_string($_POST['premiere'][$row]);
      $sport = mysql_real_escape_string($_POST['sport']);
      if (!empty($description)) {
         $sql = "INSERT INTO picks(sport, posted, game, game_time, description, away, home, premiere) VALUES(
                        '$sport',
                        '$posted1',
                        '$game',
                        '$game_time',
                        '$description',
                        '$away_team',
                        '$home_team',
                        '$premiere');";
                        mysql_query($sql)
                        or die("Sorry, there was a problem adding picks<br /> ".mysql_error());
      }
   }
}
echo "Picks for '".date("F j, Y", strtotime($posted1))."' has been uploaded<br />";
echo "<a href='$back'>Go back to picks page</a><br />$count ";
}

It works now, but for some reason the $posted1 does not go into the database at all.

Why would that not work now?

 

Thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.