Jump to content

php Insert not inserting with echoed form elements


mrt003003

Recommended Posts

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 :)

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.