Page 1 of 1

Insert data not working

Posted: Tue Nov 14, 2006 8:16 pm
by Dvorak
Hi all,
i have a problem with inserting data to database.i have to insert array as well,which is not working.the first query work,but the second query didnt work.the second query should insert array of data.

Code: Select all

<?php

session_start();

// Connects to your Database 
mysql_connect("localhost", "ODBC", "") or die(mysql_error()); 
mysql_select_db("wms") or die(mysql_error()); 
//checks cookies to make sure they are logged in 
if(isset($_COOKIE['ID_my_site'])) 
{ 
$username = $_COOKIE['ID_my_site']; 
$pass = $_COOKIE['Key_my_site']; 
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
while($info = mysql_fetch_array( $check )) 
{ 
//if the cookie has the wrong password, they are taken to the login page 
if ($pass != $info['password']) 
{ header("Location:http://localhost/projek/home.php"); 
} 
//otherwise they are shown the user area 
else 
{ 

$bilpekerja = isset($_POST[bilorg]) ? $_POST[bilorg] : '';
$jarak = isset($_POST[jarak]) ? $_POST[jarak] : '';
$kondisi = isset($_POST[kondisi]) ? $_POST[kondisi] : '';
$input = isset($_POST[input]) ? $_POST[input] : '';
$jenis = isset($_POST[jenis]) ? $_POST[jenis] : '';
$hayat = isset($_POST[hayat]) ? $_POST[hayat] : '';
$kelas = isset($_POST[kelas]) ? $_POST[kelas] : '';
$aktiviti = isset($_POST[aktiviti]) ? $_POST[aktiviti] : '';
$berat = isset($_POST[berat]) ? $_POST[berat] : '';
$isipadu = isset($_POST[isipadu]) ? $_POST[isipadu] : '';

$rujukan = $_SESSION['rujukan'];
$tarikh = $_SESSION['tarikh'];
$syarikat = $_SESSION['syarikat'];
$jenisp = $_SESSION['jenisp'];
    
$db=mysql_connect("localhost","ODBC","");
mysql_select_db("wms",$db);

include ("function.inc.php");
include ("pengiraan_pekerja.php");

    if(is_array($jenis))
    {
    $kirahayatvar = kirahayat($_POST['hayat']);
    $kiraberatvar = kiraberat($_POST['berat']);
    $kiraisipaduvar = kiraisipadu($_POST['isipadu']);
    $kelasvar = kelas($_POST['kelas']);
    $aktivitivar = aktiviti($_POST['aktiviti']);
    
    foreach($jenis as $key=>$value)
    {
    $kadarharga[$key] = $kelasvar[$key] * ($kirahayatvar[$key] + $aktivitivar[$key] + $kiraisipaduvar[$key] + $kiraberatvar[$key]);
     
    $s[] = $kadarharga[$key];
    $kosPengurusan = array_sum($s);    
    
    }
    }
    
    if($kondisi =="Y")
       $kond = 327 * $input;
    else
       $kond = 0;    

    $Kdrpgktn = 1 * $jarak;
    //overal calculation
    $subjumlah = $kosPengurusan + $Kdrpgktn;
    
    if ($agensi == "A"){
        $awam = 0.9 * $subjumlah;
        $SubJumlah = $awam;
         }
    else {
        $swasta = 1 * $subjumlah;
        $SubJumlah = $swasta;
        }
        
    $KosKhidmat = $SubJumlah + $KdrElaun + $kond;
    
    $MarginUntung = 0.2 * $KosKhidmat;
    
    $KosSeluruh = $KosKhidmat + $MarginUntung;
    
        
    $sql="INSERT INTO  borangsrs(no_rujukan, tarikh, nama_syarikat, jenis_perkhidmatan, bil_pekerja, kadar_elaun, kadar_pengangkutan, sub_jumlah, kos_khidmat, margin, kos_seluruh, kondisi) VALUES('$rujukan', '$tarikh', '$syarikat', '$jenisp', '$bilpekerja', '$KdrElaun', '$Kdrpgktn', '$SubJumlah', '$KosKhidmat', '$MarginUntung', '$KosSeluruh', '$kond')";

$result=mysql_query($sql);


//this code not working,the thing is,i have to insert this array at the same place as a above coz they're connected
    foreach($jenis as $key=>$value)
{
    $q[] = $kadarharga[$key];
    $kosPeng = array_sum($q);    
    
     $sql="UPDATE borangsrs SET unsur ='$value', hayat ='$hayat[$key]', kelas ='$kelas[$key]',  aktiviti = '$aktiviti[$key]',isipadu ='$isipadu[$key]',berat = '$berat[$key]', kadarharga = '$kadarharga[$key]',kospengurusan ='$kosPeng' WHERE no_rujukan ='$rujukan'";

$result=mysql_query($sql);

}
   
    
?>
actually i have to retrieve data from this table later,to display in report.the data contain array as well.at first i create two tables.but i dont know how to connect it.Please help..

Posted: Wed Nov 15, 2006 3:20 am
by volka
There's no error handling for the mysql operations. Simple solution: append or die(mysql_error()).
Also check the sql statement by printing it.

Code: Select all

//this code not working,the thing is,i have to insert this array at the same place as a above coz they're connected
foreach($jenis as $key=>$value)
{
	$q[] = $kadarharga[$key];
	$kosPeng = array_sum($q);   
   
	$sql="UPDATE borangsrs SET unsur ='$value', hayat ='$hayat[$key]', kelas ='$kelas[$key]',  aktiviti = '$aktiviti[$key]',isipadu ='$isipadu[$key]',berat = '$berat[$key]', kadarharga = '$kadarharga[$key]',kospengurusan ='$kosPeng' WHERE no_rujukan ='$rujukan'";
	echo '<div>Debug: ', htmlentities($sql), "</div>\n";
	$result=mysql_query($sql) or die(mysql_error());
}