mrt003003 Posted April 30, 2011 Share Posted April 30, 2011 I have a form that inserts a tables records into my database. Ive tried to modify it so it will insert into two tables but it only seems to insert into one table and not the other. I made a query that counts the rows of the 2nd table and +1, which is then suppose to be inserted. <?php $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 fleet (FleetName) VALUES (%s)", GetSQLValueString($_POST['ffleetname'], "text")); $insertSQL = sprintf("INSERT INTO ships (ShipName, FleetName, PlayerName, Image, Image2, Cost, HealthA, HealthB, MaxHealthA, MaxHealthB, `Class`, PlanetName) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['shipname'], "text"), GetSQLValueString($_POST['fleetname'], "text"), GetSQLValueString($_POST['playername'], "text"), GetSQLValueString($_POST['image1'], "text"), GetSQLValueString($_POST['image2'], "text"), GetSQLValueString($_POST['cost'], "int"), GetSQLValueString($_POST['ha'], "int"), GetSQLValueString($_POST['hb'], "int"), GetSQLValueString($_POST['mha'], "int"), GetSQLValueString($_POST['mhb'], "int"), GetSQLValueString($_POST['class'], "int"), GetSQLValueString($_POST['select'], "text")); mysql_select_db($database_swb, $swb); $Result1 = mysql_query($insertSQL, $swb) or die(mysql_error()); $insertGoTo = "ship.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } $colname_Fleetcount = "-1"; if (isset($_GET['recordID'])) { $colname_Fleetcount = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']); } mysql_select_db($database_swb, $swb); $query_Fleetcount = sprintf("SELECT * FROM fleet"); $Fleetcount = mysql_query($query_Fleetcount, $swb) or die(mysql_error()); $row_Fleetcount = mysql_fetch_assoc($Fleetcount); $totalRows_Fleetcount = mysql_num_rows($Fleetcount); ?> <input name="image1" type="hidden" value="<?php echo $row_Ships['Image']; ?>" /> <input name="image2" type="hidden" value="<?php echo $row_Ships['Image2']; ?>" /> <input name="cost" type="hidden" value="<?php echo $row_Ships['Cost']; ?>" /> <input name="ha" type="hidden" value="<?php echo $row_Ships['HealthA']; ?>" /> [b] <input name="ffleetname" type="hidden" value="<?php echo $totalRows_Fleetcount = mysql_num_rows($Fleetcount) + 1;?>" /> <input name="fleetname" type="hidden" value="<?php echo $totalRows_Fleetcount = mysql_num_rows($Fleetcount) + 1;?>" />[/b] <input name="hb" type="hidden" value="<?php echo $row_Ships['HealthB']; ?>" /> <input name="mha" type="hidden" value="<?php echo $row_Ships['MaxHealthA']; ?>" /> <input name="mhb" type="hidden" value="<?php echo $row_Ships['MaxHealthB']; ?>" /> <input name="class" type="hidden" value="<?php echo $row_Ships['Class']; ?>" /> <input name="playername" type="hidden" value="<?php echo $_SESSION['MM_Username']; ?>" /> <input name="shipname" type="hidden" value="<?php echo $row_Ships['ShipName']; ?>" /> <input type="submit" name="Submit" value="Submit" /> <input type="hidden" name="MM_insert" value="form1"> </form> Any ideas? Thanks Link to comment https://forums.phpfreaks.com/topic/235184-insert-into-two-tables/ Share on other sites More sharing options...
mrt003003 Posted April 30, 2011 Author Share Posted April 30, 2011 Its ok ive fixed it.. Added: mysql_select_db($database_swb, $swb); $Result1 = mysql_query($insertSQL, $swb) or die(mysql_error()); to the first insert. Link to comment https://forums.phpfreaks.com/topic/235184-insert-into-two-tables/#findComment-1208624 Share on other sites More sharing options...
wildteen88 Posted April 30, 2011 Share Posted April 30, 2011 You don't need to call mysql_select_db($database_swb, $swb); every time you run a query. I assume this is code that Dreamweaver has generated. You should really learn to hand code your script rather than relying on dreamweaver. Link to comment https://forums.phpfreaks.com/topic/235184-insert-into-two-tables/#findComment-1208629 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.