shadiadiph Posted January 27, 2009 Share Posted January 27, 2009 I have built a form and symbol may have numerous values i have been trying to get it to insert into the database each value on a different row in tablepricedetails has anyone got any ideas how to do this? I can't number the arrays $symbol0 = $_POST["symbol"][]; etc as i do not know how many symbol values it will return I am sure there is an easy way of doing this but i just can't think of what it is. form input page <? $tradesql="select * from tblpricesymbolsdetails where active='Yes' "; $tempdata=$DB_site->query($tradesql); $symcount=$DB_site->num_rows($tempdata); while($row=$DB_site->fetch_array($tempdata)) { $symid = $row["intSymbolsID"]; $symbollist = $row["symbol"]; ?> <? echo '<tr><td class="norml">'.$symbollist.'</td><input type="hidden" name="symbol[]" value="<?=$symbollist?>" /></tr>'; } ?> submit page <?PHP ini_set ("display_errors", "1"); error_reporting(E_ALL); include("../../checkadminlogin.php"); include("../../../global/connection.php"); $symbol = ($_POST["symbol"]); $insertsql ="insert into tblpricedetails (intSymbolsID, dtadded) values ('$symbol', now())"; $DB_site->query($insertsql); header("Location:updateprices.php"); exit; ?> Link to comment https://forums.phpfreaks.com/topic/142605-solved-echo-form-row-and-insert-array-problem/ Share on other sites More sharing options...
gevans Posted January 27, 2009 Share Posted January 27, 2009 I think this is what you're trying to do; <?PHP ini_set ("display_errors", "1"); error_reporting(E_ALL); include("../../checkadminlogin.php"); include("../../../global/connection.php"); foreach($_POST['symbol'] as $value) { $symbol = mysql_real_escape_string($value);//if its a string #$symbol = (int) $value;//if its an integer $insertsql ="INSERT INTO `tblpricedetails` (`intSymbolsID`, `dtadded`) values ('$symbol', now())"; $DB_site->query($insertsql); } header("Location:updateprices.php"); exit; ?> Link to comment https://forums.phpfreaks.com/topic/142605-solved-echo-form-row-and-insert-array-problem/#findComment-747439 Share on other sites More sharing options...
shadiadiph Posted January 27, 2009 Author Share Posted January 27, 2009 that seems to work fine but the problem i am having is i have another 2 values to insert into each row as they are in the script below they post as arrays if i use a foreach for them also it produces a seperate row for every value i want all the values on the same row as $symbol? $current and $last are integers where $symbol is varchar $last = mysql_real_escape_string($_POST['last']); $current = mysql_real_escape_string($_POST['current']); foreach($_POST['symbol'] as $value) { $symbol = mysql_real_escape_string($value);//if its a string #$symbol = (int) $value;//if its an integer $insertsql ="INSERT INTO `tblpricedetails` (`intSymbolsID`, `last`, `current`, `dtadded`) values ('$symbol', '$last', '$current', now())"; $DB_site->query($insertsql); } header("Location:updateprices.php"); exit; ?> Link to comment https://forums.phpfreaks.com/topic/142605-solved-echo-form-row-and-insert-array-problem/#findComment-747455 Share on other sites More sharing options...
gevans Posted January 27, 2009 Share Posted January 27, 2009 hows this... <?php $num_queries = count($_POST['symbol']); for($i=0;$i<$num_queries;$i++) { $last = mysql_real_escape_string($_POST['last'][$i]); $current = mysql_real_escape_string($_POST['current'][$i]); $symbol = mysql_real_escape_string($_POST['symbol'][$i]); $insertsql ="INSERT INTO `tblpricedetails` (`intSymbolsID`, `last`, `current`, `dtadded`) values ('$symbol', '$last', '$current', now())"; $DB_site->query($insertsql); } header("Location:updateprices.php"); exit; ?> Link to comment https://forums.phpfreaks.com/topic/142605-solved-echo-form-row-and-insert-array-problem/#findComment-747467 Share on other sites More sharing options...
shadiadiph Posted January 27, 2009 Author Share Posted January 27, 2009 awesome thanks Link to comment https://forums.phpfreaks.com/topic/142605-solved-echo-form-row-and-insert-array-problem/#findComment-747472 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.