Jump to content

[SOLVED] echo form row and insert array problem


shadiadiph

Recommended Posts

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

 

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.