Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Code working but database keeps blank space

    This is my Code which just take names from customer and place it in databases.What i am seeing is that the code working fine,Showing echo message also but in database i created 2 field 1 auto incremented id and other for name.the id is generated but the name field remains blank..please help me.
    PHP Code:
    <?php

    $link = mysql_connect("localhost","root","");
    if (! $link) {
    die('cannot connect: ' .mysql_error() ) ;
    }
    $db_selected = mysql_select_db ("adform",$link );
    if(!$db_selected){
    die('cannot use :' .mysql_error() ) ;
    }

    $value = $_POST['firstname'];

    $sql ="INSERT INTO demo (firstname) VALUES ('$value')" ;

    if(!mysql_query($sql) ) {
    die ('Error :' . mysql_error());
    }
    echo "record added to database ";
    mysql_close ($link);
    ?>

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,304
    Thanks
    4
    Thanked 204 Times in 201 Posts
    Try changing

    PHP Code:
    if(!mysql_query($sql) ) {
    die (
    'Error :' mysql_error());

    to
    PHP Code:
    mysql_query($sql,$link) or die(mysql_error()); 
    and see what you get.
    Dave .... HostMonster for all of your hosting needs

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    But *FIRST* try DEBUGGING!

    Code:
    $sql ="INSERT INTO demo (firstname) VALUES ('$value')" ; 
    
    echo "<hr/>DEBUG SQL: " . $sql . "<hr/>\n";
    
    ...
    Make sure that you really *are* putting in values. Maybe it's the PHP code with the problem.

    ********

    DJM: I really don't see why your suggestion would matter. He says that records are being created (else why would the autoincrement field be there?). I strongly suspect he simply has no data to insert.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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