mrt003003 Posted October 2, 2011 Share Posted October 2, 2011 Hi there im having trouble with my code and was hoping someone may be able to help me? I think the trouble i am having is because i am echoing the form elements (form, hidden) and the value in those elements. This part is fine i think: $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")){ $insertSQL = sprintf("INSERT INTO ships (ShipName, FleetID, PlayerName, Image1, Image2, Credits, HealthA, HealthB, MaxHealthA, MaxHealthB, Class, Hyperspace, PlanetID, Building, Maintenance) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['shipname1'], "text"), GetSQLValueString($_POST['fleetid1'], "int"), GetSQLValueString($_POST['playername1'], "text"), GetSQLValueString($_POST['image11'], "text"), GetSQLValueString($_POST['image21'], "text"), GetSQLValueString($_POST['credits1'], "int"), GetSQLValueString($_POST['healtha1'], "int"), GetSQLValueString($_POST['healthb1'], "int"), GetSQLValueString($_POST['maxhealtha1'], "int"), GetSQLValueString($_POST['maxhealthb1'], "int"), GetSQLValueString($_POST['class1'], "int"), GetSQLValueString($_POST['hyperspace1'], "int"), GetSQLValueString($_POST['planetid1'], "int"), GetSQLValueString($_POST['building1'], "int"), GetSQLValueString($_POST['maintenance1'], "int")); mysql_select_db($database_swb, $swb); $Result1 = mysql_query($insertSQL, $swb) or die(mysql_error()); $insertGoTo = "planet.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } The problem may lay here: <?php echo'<form id="form1" name="form1" method="post" action="">';echo'<table width="400" border="0" align="top">'; do { echo '<tr> <td width="200" align="center">'; echo '<span class="arial12">'; echo "<img src=".$row_Ships["Image1"]." height =\"250\" width=\"400\">"; echo'| Maintenance: '; echo $row_Ships['Maintenance'];echo ' | '; echo'<input type="submit" name="Submit" value="Build" />'; echo' | Construction Time: '; echo $row_Ships['Building']; echo' | ';echo '</td></tr>'; echo" <input name='fleetid1' type='hidden' id='fleetid1' value='".$totalRows_Fleet." + '1''/>"; echo" <input name='fleetname1' type='hidden' id='fleetname1' value='Fleet ".$totalRows_Fleet." + '1''/>"; echo" <input name='shipname1' type='hidden' id='shipname1' value='".$row_Ships['ShipName']."'/>"; echo" <input name='playername1' type='hidden' id='playername1' value='".$_SESSION['MM_Username']."'/>"; echo" <input name='credits1' type='hidden' id='credits1' value='".$row_Ships['Credits']."'/>"; echo" <input name='image11' type='hidden' id='image11' value='".$row_Ships['Image1']."'/>"; echo" <input name='image21' type='hidden' id='image21' value='".$row_Ships['Image2']."'/>"; echo" <input name='healtha1' type='hidden' id='healtha1' value='".$row_Ships['HealthA']."'/>"; echo" <input name='healthb1' type='hidden' id='healthb1' value='".$row_Ships['HealthB']."'/>"; echo" <input name='maxhealtha1' type='hidden' id='maxhealtha1' value='".$row_Ships['MaxHealthA']."'/>"; echo" <input name='maxhealthb1' type='hidden' id='maxhealthb1' value='".$row_Ships['MaxHealthB']."'/>"; echo" <input name='class1' type='hidden' id='class1' value='".$row_Ships['Class']."'/>"; echo" <input name='hyperspace1' type='hidden' id='hyperspace1' value='".$row_Ships['Hyperspace']."'/>"; echo" <input name='maintenance1' type='hidden' id='maintenance1' value='".$row_Ships['Maintenance']."'/>"; echo" <input name='Planetid1' type='hidden' id='planetid1' value='".$row_Credits['PlanetID']."'/>"; echo" <input name='building1' type='hidden' id='building1' value='".$row_Ships['Building']."'/>"; } while ($row_Ships = mysql_fetch_assoc($Ships)); echo '</table></form>';?> No records are insterted at all?? The page redirection after insert is not happening either. If anybody coould help that would be brilliant... Thank You Quote Link to comment https://forums.phpfreaks.com/topic/248280-php-insert-not-inserting-with-echoed-form-elements/ Share on other sites More sharing options...
mrt003003 Posted October 2, 2011 Author Share Posted October 2, 2011 I did just not the action bit in the form is empty and modified it to: [cdoe]echo'<form id="form1" name="form1" method="post" action="'.$editformaction.'">'; Its still not inserting or redirecting tho?? :| Quote Link to comment https://forums.phpfreaks.com/topic/248280-php-insert-not-inserting-with-echoed-form-elements/#findComment-1274975 Share on other sites More sharing options...
Buddski Posted October 2, 2011 Share Posted October 2, 2011 I do not see any form elements with the name "MM_insert" Quote Link to comment https://forums.phpfreaks.com/topic/248280-php-insert-not-inserting-with-echoed-form-elements/#findComment-1274976 Share on other sites More sharing options...
mrt003003 Posted October 2, 2011 Author Share Posted October 2, 2011 Hi there thanks for replying, yes i see i missed that out, so ive added it and moved the end of the </form> tag within the do loop. echo "<input type='hidden' name='MM_insert' value='form1'>"; echo'</form>'; It still doesnt instert anything or redirect page tho? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/248280-php-insert-not-inserting-with-echoed-form-elements/#findComment-1274978 Share on other sites More sharing options...
Buddski Posted October 2, 2011 Share Posted October 2, 2011 You shouldnt have moved the </form> to inside the loop. Are you only expecting one row of results from the form? If not, you will need to create a HTML array for each for the interations, as to prevent the subsequent rows from overwriting the previous one. Does anything happen when you submit your form? Do you have error reporting turned on? error_reporting(E_ALL); ini_set('display_errors',1); Quote Link to comment https://forums.phpfreaks.com/topic/248280-php-insert-not-inserting-with-echoed-form-elements/#findComment-1274980 Share on other sites More sharing options...
mrt003003 Posted October 2, 2011 Author Share Posted October 2, 2011 Its now inserting and redirecting...!!! I had some form elements set as text and not int... Its still not quite working right though as it only ever inserts the first record, even if im clicking submit on the second record??? Am i missing something here? Is there a problem with using echoed form elements in a loop?? For example, i have records that are outputted and with each row is a form with submit button and hidden fields.. Its only ever inserting the first row in the database regardless of submiting a different row??? THanks Quote Link to comment https://forums.phpfreaks.com/topic/248280-php-insert-not-inserting-with-echoed-form-elements/#findComment-1274983 Share on other sites More sharing options...
Buddski Posted October 2, 2011 Share Posted October 2, 2011 That is because each iteration for you do while is creating the same named elements over and over again. The quickest way to fix this would be to move the entire form into the do while loop. This will mean that each iteration is its own form. Quote Link to comment https://forums.phpfreaks.com/topic/248280-php-insert-not-inserting-with-echoed-form-elements/#findComment-1274991 Share on other sites More sharing options...
mrt003003 Posted October 2, 2011 Author Share Posted October 2, 2011 Yeah thats it! Thank you very much, its all working Quote Link to comment https://forums.phpfreaks.com/topic/248280-php-insert-not-inserting-with-echoed-form-elements/#findComment-1274993 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.