Duplicate rows

Discussion in 'PHP' started by chxxangie, Dec 27, 2007.

  1. #1
    <?php
    $cont_date=date('y-m-d');
    $cont_time=date('G:i:s');
    $name = @$HTTP_POST_VARS["name"];
    $email = @$HTTP_POST_VARS["email"];
    $comp_name = @$HTTP_POST_VARS["comp_name"];
    $attn = @$HTTP_POST_VARS["attn"];
    $subject = @$HTTP_POST_VARS["subject"];
    $msg = @$HTTP_POST_VARS["msg"];

    include("database.php");


    $query = "INSERT INTO contact_info(cont_date,cont_time,name,email,comp_name,attn,subject,msg)
    values
    ('".$cont_date."','".$cont_time."','".$name."','".$email."','".$comp_name."','".$attn."','".$subject
    ."','".$msg."')";

    $rs=mysql_query($query);

    if (!$rs) { ?>
    <SCRIPT LANGUAGE="Javascript">
    alert ('Your form has NOT been submitted!');
    window.location="contactus.php";
    </SCRIPT> <?php
    exit;
    } ?>



    ----------------------------
    Result in database:
    ----------------------------
    1 , 2007-12-27 , 12:02:18 , sgsdg , we@sg , sfsf , Project Manager , sdf , fffds , 0
    2 , 2007-12-27 , 12:02:19 , , , , , , , 0
     
    chxxangie, Dec 27, 2007 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    Please use

    this code



    <?php
    $cont_date=date('y-m-d');
    $cont_time=date('G:i:s');
    $name = @$HTTP_POST_VARS["name"];
    $email = @$HTTP_POST_VARS["email"];
    $comp_name = @$HTTP_POST_VARS["comp_name"];
    $attn = @$HTTP_POST_VARS["attn"];
    $subject = @$HTTP_POST_VARS["subject"];
    $msg = @$HTTP_POST_VARS["msg"];

    include("database.php");

    if (isset($name))
    {
    $query = "INSERT INTO contact_info(cont_date,cont_time,name,email,comp_name,attn,subject,msg)
    values
    ('".$cont_date."','".$cont_time."','".$name."','".$email."','".$comp_name."','".$attn."','".$subject
    ."','".$msg."')";

    $rs=mysql_query($query);
    }
    if (!$rs) { ?>
    <SCRIPT LANGUAGE="Javascript">
    alert ('Your form has NOT been submitted!');
    window.location="contactus.php";
    </SCRIPT> <?php
    exit;
    } ?>
     
    kmap, Dec 27, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    $HTTP_POST_VARS is deprecated, but anyway, add this line before assigning the variables.
    
    $HTTP_POST_VARS = array_map('mysql_real_escape_string', $HTTP_POST_VARS);
    
    PHP:
    And that's why: www.php.net/mysql_real_escape_string
     
    nico_swd, Dec 28, 2007 IP
  4. chxxangie

    chxxangie Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    above 2 methods don't works....

    the record is sent into database, the problem is another incomplete records is sent into database too. looks like duplicated record! all this happen in a submit form only.

    the most "amazing" is when i retrieve the record from database, the showing record is only the complete record!
     
    chxxangie, Jan 1, 2008 IP
  5. chxxangie

    chxxangie Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    nobody can help me in this problem??? :(
     
    chxxangie, Jan 2, 2008 IP
  6. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #6

    You probably are getting a character in your string.

    Be sure to strip off characters before inserting.

    make a file called func.php then include it in your .php
    
    <?php
    #contents of func.php
    function cleanse($string)
    {
            return str_replace('"', '\"', strip_tags($string));
    }
    
    function esc ($s) {
            return escapeshellcmd($s);
    }
    ?>
    
    Code (markup):

    Now do:

    
    
    <?php
    include "func.php";
    
    $cont_date=date('y-m-d');
    $cont_time=date('G:i:s');
    $name = @$HTTP_POST_VARS["name"];
    $email = @$HTTP_POST_VARS["email"];
    $comp_name = @$HTTP_POST_VARS["comp_name"];
    $attn = @$HTTP_POST_VARS["attn"];
    $subject = @$HTTP_POST_VARS["subject"];
    $msg = @$HTTP_POST_VARS["msg"];
    
    
    cleanse($cont_date);
    cleanse($cont_time);
    cleanse($name);
    cleanse($email);
    cleanse($comp_name);
    cleanse($attn);
    cleanse($subject);
    cleanse($msg);
    
    include("database.php");
    
    if (isset($name))
    {
    $query = "INSERT INTO contact_info(cont_date,cont_time,name,email,comp_name,attn,subject,msg)
    values
    ('".esc($cont_date)."','".esc($cont_time)."','".esc($name)."','".esc($email)."','".esc($comp_name)."','".esc($attn)."','".esc($subject)."','".esc($msg)."')";
    $rs=mysql_query($query);
    }
    if (!$rs) { ?>
    <SCRIPT LANGUAGE="Javascript">
    alert ('Your form has NOT been submitted!');
    window.location="contactus.php";
    </SCRIPT> <?php
    exit;
    } ?>
    
    
    Code (markup):

    Post your results.
     
    LittleJonSupportSite, Jan 3, 2008 IP
  7. chxxangie

    chxxangie Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    there is no data is inserted into database.

    
    	include("func.php");
    	
    	$cont_date=date('y-m-d');
    	$cont_time=date('G:i:s'); 
    	$name = @$HTTP_POST_VARS["name"];
    	$email = @$HTTP_POST_VARS["email"];
    	$comp_name =@$HTTP_POST_VARS["comp_name"];
    	$attn = @$HTTP_POST_VARS["attn"];
    	$subject = @$HTTP_POST_VARS["subject"];
    	$msg =@$HTTP_POST_VARS["msg"];
    
    echo "1";
    
    	cleanse($cont_date);
    	cleanse($cont_time);
    	cleanse($name);
    	cleanse($email);
    	cleanse($comp_name);
    	cleanse($attn);
    	cleanse($subject);
    	cleanse($msg);
    	
    echo "2";
    
    
    PHP:
    i try to "debug" the code, the webpage only show 1.

    *************************************************************************

    when i remove the include "func.php" and paste the code to the .php file...
    
    function cleanse($string)
    {
            return str_replace('"', '\"', strip_tags($string));
    }
    
    function esc ($s) {
            return escapeshellcmd($s);
    }
    	
    	
    	$cont_date=date('y-m-d');
    	$cont_time=date('G:i:s'); 
    	$name = @$HTTP_POST_VARS["name"];
    	$email = @$HTTP_POST_VARS["email"];
    	$comp_name =@$HTTP_POST_VARS["comp_name"];
    	$attn = @$HTTP_POST_VARS["attn"];
    	$subject = @$HTTP_POST_VARS["subject"];
    	$msg =@$HTTP_POST_VARS["msg"];
    
    
    	cleanse($cont_date);
    	cleanse($cont_time);
    	cleanse($name);
    	cleanse($email);
    	cleanse($comp_name);
    	cleanse($attn);
    	cleanse($subject);
    	cleanse($msg);
    	
    
    PHP:
    and like that the result is inserted seccessfully into database and no more duplicate row.
    what is the problem of the include?
     
    chxxangie, Jan 3, 2008 IP
  8. chxxangie

    chxxangie Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    anyway, i want to say THANK YOU to all the people who help me in solving this problem especially is LittleJonSupportSite.....
     
    chxxangie, Jan 3, 2008 IP