PDA

View Full Version : Inserting in a table from results from another...uh?


chesneil
07-16-2003, 08:31 AM
Hi guys. Been banging my head on this one and finally got to ask. I'm selecting values from one table and in the 'while $row=' cycle I'm checking the value of one column and depending on the value in one column, writing it to another table with one of the columns in the new table set to a value depending on the value input in a text box. Difficult to explain, but hopefully you can just see my error in the code below. Any help would be appreciated. Thanks in advance.

<?php

if (($submit=="ADD TO ACCOUNTS") && ($password=="xxx")) { //A

include ('dblon.php');
$sql="SELECT * FROM Standardincome ORDER BY source,stunos";
$result=mysql_query($sql);

$memo="standard bank";
$thismonth=date('n');
$thisyear=date('Y');

while ($row=mysql_fetch_array($result)) { //B

$fromx=$row['fromdetails'];
$stunox=$row['stunos'];
$amountx=$row['amount'];
$sourcex=$row['source'];

if ($sourcex=="CFB") { //C

$result = mysql_query("INSERT INTO Income (year, month, date, from, stuno, amount, fees, memo) VALUES ('$thisyear' , '$thismonth' , '$cfbdate' , '$fromx', '$stunox' , '$amountx' , '$amountx' , '$memo')");

if (!$result) {
echo ("Error");
exit();
}
} ///C

if ($sourcex=="NFB") { //D

$result = mysql_query("INSERT INTO Income (year, month, date, from, stuno, amount, fees, memo) VALUES ('$thisyear' , '$thismonth' , '$nfbdate' , '$fromx', '$stunox' , '$amountx' , '$amountx' , '$memo')");

if (!$result) {
echo ("Error");
exit();
}
} ///D

if (($sourcex!="NFB") && ($sourcex!="CFB")) { ///E

$result = mysql_query("INSERT INTO Income (year, month, from, stuno, amount, fees) VALUES ('$thisyear' , '$thismonth' , '$fromx', '$stunox' , '$amountx' , '$amountx')");

if (!$result) {
echo ("Error");
exit();
}
} ///E

} ///B
} ///A

///////////////FORM///////////////////////////////

?>
<form method="post" action="<?=$PHP_SELF?>">
<?php
include ('dblon.php'); //log on to db
$sql="SELECT * FROM Standardincome ORDER BY source,stunos";
$result=mysql_query($sql);

while ($row=mysql_fetch_array($result)) {

$id=$row['ID'];
$fromdetailsx=$row['fromdetails'];
$stunox=$row['stunos'];
$amountx=$row['amount'];
$typex=$row['type'];
$sourcex=$row['source'];

echo //...the above values in a nice table
}

?>
CFB date: <input type="text" name="cfbdate">
NFB date: <input type="text" name="nfbdate">
Add to Income: <input type="submit" name="submit" value="ADD TO ACCOUNTS">
Password: <input type="password" name="password">

</form>
</body>
</html>

chesneil
07-17-2003, 04:26 AM
Well it turns out that i was using a reserved word for a column name ('from'). On we go...