SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    HELP ON INSERTING MUTIPLE VALUES IN Mysql table using PHP

    I have a php code for inserting records, now this code inserts more than two records at a time.
    The code is below. I want the code NOT be inserting a record in case a form variable or value is empty. I have tried but I can not get it. I mean if a user did not key any value in text filed form, then submit, it should not insert the value but skip and go to another row or record that I have value. Iam asking this becuase when I submit with all form values or variables, the php code is inserting without a problem but when I submit a form with some form values or variables Iam get an errory saying ('Error, insert query failed'); .

    You will help will be appreciated.


    <?php

    $fClaimCode = $_POST["Allowance3"];
    $fAmountRequired = $_POST["Amount3"];


    $gClaimCode = $_POST["Allowance4"];
    $gAmountRequired = $_POST["Amount4"];


    $hClaimcode = $_POST["Allowance5"];
    $hAmountRequired = $_POST["Amount5"];


    $iClaimcode = $_POST["Allowance6"];
    $iAmountRequired = $_POST["Amount6"];


    $jClaimcode = $_POST["Allowance7"];
    $jAmountRequired = $_POST["Amount7"];

    $lClaimcode = $_POST["Allowance9"];
    $lAmountRequired = $_POST["Amount9"];

    $zramoney = $_POST["AmountForZRA"];
    $employemoney = $_POST["AmountforEmployee"];

    $Docem = $_POST["DocumentName"];
    $mdetails = $_POST["details"];



    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$dClaimCode','$dAmountRequired')" ;
    mysql_query($mysql_query) or die('Error, insert query failed');


    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$eClaimCode','$eAmountRequired')" ;
    mysql_query($mysql_query) or die('Error, insert query failed');


    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode, Amount)".
    "VALUES ('$tAllowanceID','$fClaimCode','$fAmountRequired')" ;
    mysql_query($mysql_query) or die('Error, insert query failed');


    $mysql_query="INSERT INTO aftertravel (AllowanceId, ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$gClaimCode','$gAmountRequired')" ;
    mysql_query($mysql_query) or die('Error, insert query failed');

    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$hClaimcode','$hAmountRequired')" ;
    mysql_query($mysql_query) or die('Error, insert query failed');


    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$iClaimcode','$iAmountRequired')" ;
    mysql_query($mysql_query) or die('Error, insert query failed');

    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$jClaimcode','$jAmountRequired')" ;
    mysql_query($mysql_query) or die('Error, insert query failed');

    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$lClaimcode','$lAmountRequired')" ;
    mysql_query($mysql_query) or die('Error, insert query failed');



    $mysql_query="INSERT INTO attdocumentation (AllowanceId,Documentname,Details)".
    "VALUES ('$tAllowanceID','$Docem','$mdetails')" ;
    mysql_query($mysql_query) or die('Error, insert query failed');

    ?>

  2. #2
    SitePoint Enthusiast elena_sev's Avatar
    Join Date
    Apr 2003
    Location
    Sofia, Bulgaria
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Zamzam,
    I don't know whether I will help you but I will try
    first of all when one of your queries is not corrent the die() is executed that is why nothing happens after that bacause die() stops the execution of the script
    you can first check whether the user entered the values needed for the query and then if everything is ok execute it.
    But even if there are missing values the query should pass and insert empty values maybe somethings wrong with the mysql table and the values you are trying to insert you can see what error the mysql_query raise with mysql_error()
    try something like this:

    $query = mysql_query($mysql_query);
    print mysql_error($query);

    I hope something will help :-)

  3. #3
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Elena,
    If there 6 records to insert and I type 6 records in a form there are able to go with out a problem. But if there 6 records to insert and I type 4 records I receive an error.
    That is where I need some help

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Use if's around each insert:

    PHP Code:
    if (($tAllowanceID == '') || ($dClaimCode == '') || ($dAmountRequired == ''))
    {
        
    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$dClaimCode','$dAmountRequired')" ;
    mysql_query($mysql_query) or die('Error, insert query failed'); 

    Anyway, you should check the post values before inserting, to prevent sql injection problems.

  5. #5
    SitePoint Evangelist ikeo's Avatar
    Join Date
    Oct 2004
    Location
    Austin Texas
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use the empty() function to check the variables before an insert so

    if (!empty($variable_youre_checking))
    {
    //Do the SQL insert
    }

    etc etc

  6. #6
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried this code below, but it is not working. where Iam going wrong???
    if (($tAllowanceID != '') || ($dClaimCode != '') || ($dAmountRequired != '')){

    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$dClaimCode','$dAmountRequired')" ;
    mysql_query($mysql_query) or die('Error2, insert query failed');
    }

    if (($tAllowanceID != '') || ($eClaimCode != '') || ($eAmountRequired != '')) {

    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$eClaimCode','$eAmountRequired')" ;
    mysql_query($mysql_query) or die('Error3, insert query failed');

    }
    if (($tAllowanceID !='') || ($fClaimCode != '') || ($fAmountRequired != '')){

    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode, Amount)".
    "VALUES ('$tAllowanceID','$fClaimCode','$fAmountRequired')" ;
    mysql_query($mysql_query)or die('Error4, insert query failed');
    }
    if (($tAllowanceID != '') || ($gClaimCode != '') || ($gAmountRequired != ''))
    {

    $mysql_query="INSERT INTO aftertravel (AllowanceId, ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$gClaimCode','$gAmountRequired')" ;
    mysql_query($mysql_query) or die('5Error, insert query failed');
    }
    if (($tAllowanceID != '') || ($hClaimcode !='') || ($hAmountRequired != ''))
    {
    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$hClaimcode','$hAmountRequired')" ;
    mysql_query($mysql_query) or die('6Error, insert query failed');
    }
    if (($tAllowanceID != '') || ($iClaimcode != '') || ($iAmountRequired != ''))
    {

    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$iClaimcode','$iAmountRequired')" ;
    mysql_query($mysql_query) or die('7Error, insert query failed');
    }
    if (($tAllowanceID != '') || ($jClaimcode !='') || ($jAmountRequired != ''))
    {
    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$jClaimcode','$jAmountRequired')" ;
    mysql_query($mysql_query) or die('8Error, insert query failed');
    }

    if (($tAllowanceID !='') || ($lClaimcode != '') || ($lAmountRequired != ''))
    {
    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
    "VALUES ('$tAllowanceID','$lClaimcode','$lAmountRequired')" ;
    mysql_query($mysql_query) or die('9Error, insert query failed');

    }
    if (($tAllowanceID !='') || ($Docem != '') || ($mdetails != '')){


    $mysql_query="INSERT INTO attdocumentation (AllowanceId,Documentname,Details)".
    "VALUES ('$tAllowanceID','$Docem','$mdetails')" ;
    mysql_query($mysql_query) or die('10Error, insert query failed');

    }

  7. #7
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Formatted to make it readable here (but otherwise unchanged)
    PHP Code:
    if (($tAllowanceID != '') || ($dClaimCode != '') || ($dAmountRequired != ''))
    {
        
    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
        
    "VALUES ('$tAllowanceID','$dClaimCode','$dAmountRequired')" ;
        
    mysql_query($mysql_query) or die('Error2, insert query failed');
    }

    if ((
    $tAllowanceID != '') || ($eClaimCode != '') || ($eAmountRequired != ''))
    {
        
    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
        
    "VALUES ('$tAllowanceID','$eClaimCode','$eAmountRequired')" ;
        
    mysql_query($mysql_query) or die('Error3, insert query failed');
    }

    if ((
    $tAllowanceID !='') || ($fClaimCode != '') || ($fAmountRequired != ''))
    {
        
    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode, Amount)".
        
    "VALUES ('$tAllowanceID','$fClaimCode','$fAmountRequired')" ;
        
    mysql_query($mysql_query)or die('Error4, insert query failed');
    }

    if ((
    $tAllowanceID != '') || ($gClaimCode != '') || ($gAmountRequired != ''))
    {
        
    $mysql_query="INSERT INTO aftertravel (AllowanceId, ClaimCode,Amount)".
        
    "VALUES ('$tAllowanceID','$gClaimCode','$gAmountRequired')" ;
        
    mysql_query($mysql_query) or die('5Error, insert query failed');
    }

    if ((
    $tAllowanceID != '') || ($hClaimcode !='') || ($hAmountRequired != ''))
    {
        
    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
        
    "VALUES ('$tAllowanceID','$hClaimcode','$hAmountRequired')" ;
        
    mysql_query($mysql_query) or die('6Error, insert query failed');
    }

    if ((
    $tAllowanceID != '') || ($iClaimcode != '') || ($iAmountRequired != ''))
    {
        
    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
        
    "VALUES ('$tAllowanceID','$iClaimcode','$iAmountRequired')" ;
        
    mysql_query($mysql_query) or die('7Error, insert query failed');
    }

    if ((
    $tAllowanceID != '') || ($jClaimcode !='') || ($jAmountRequired != ''))
    {
        
    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
        
    "VALUES ('$tAllowanceID','$jClaimcode','$jAmountRequired')" ;
        
    mysql_query($mysql_query) or die('8Error, insert query failed');
    }

    if ((
    $tAllowanceID !='') || ($lClaimcode != '') || ($lAmountRequired != ''))
    {
        
    $mysql_query="INSERT INTO aftertravel (AllowanceId,ClaimCode,Amount)".
        
    "VALUES ('$tAllowanceID','$lClaimcode','$lAmountRequired')" ;
        
    mysql_query($mysql_query) or die('9Error, insert query failed');
    }

    if ((
    $tAllowanceID !='') || ($Docem != '') || ($mdetails != ''))
    {
        
    $mysql_query="INSERT INTO attdocumentation (AllowanceId,Documentname,Details)".
        
    "VALUES ('$tAllowanceID','$Docem','$mdetails')" ;
        
    mysql_query($mysql_query) or die('10Error, insert query failed');

    Ian Anderson
    www.siteguru.co.uk

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Point #1 - change all the || to && ... you want to check if variable1 is not empty AND variable2 is not empty AND variable3 etc.
    Ian Anderson
    www.siteguru.co.uk


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •