timmah1 Posted July 23, 2009 Share Posted July 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/167131-solved-multiple-textareas/ Share on other sites More sharing options...
timmah1 Posted July 23, 2009 Author Share Posted July 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/167131-solved-multiple-textareas/#findComment-881288 Share on other sites More sharing options...
timmah1 Posted July 23, 2009 Author Share Posted July 23, 2009 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?? Quote Link to comment https://forums.phpfreaks.com/topic/167131-solved-multiple-textareas/#findComment-881317 Share on other sites More sharing options...
o3d Posted July 23, 2009 Share Posted July 23, 2009 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 :/ Quote Link to comment https://forums.phpfreaks.com/topic/167131-solved-multiple-textareas/#findComment-881501 Share on other sites More sharing options...
timmah1 Posted July 23, 2009 Author Share Posted July 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/167131-solved-multiple-textareas/#findComment-881564 Share on other sites More sharing options...
fenway Posted July 24, 2009 Share Posted July 24, 2009 Echo the actual queries, not your code. Quote Link to comment https://forums.phpfreaks.com/topic/167131-solved-multiple-textareas/#findComment-882307 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.