1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Inserting data in database.

Discussion in 'PHP' started by MichaelLewis, May 4, 2008.

  1. #1
    Any idea why this code doesn't work when the same code (except for the query VALUES field names) works in another file.

    $link = mysql_connect('localhost', $username, $password);
    if (!$link){die('Not connected : ' . mysql_error());}
    $db_selected = mysql_select_db($database, $link);
    if (!$db_selected){die ('Can\'t use ' .$database .':'. mysql_error());}
    $query = "INSERT INTO invoices VALUES('$Ref','$CoEmpName',".
    "'$InvDate','$Branch','$BrAddr','$BrApt','$BrCity','$BrProv','$BrCtry',".
    "'$BrZip','$AssDesc[0]','$AssPrice[0]')";
    mysql_query($query, $link);
    mysql_close();

    I dont' get any errors and nothing gets added to the database.

    Thanks,
    Michael
     
    MichaelLewis, May 4, 2008 IP
  2. Aaron700

    Aaron700 Active Member

    Messages:
    338
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    
    <?php
    
    $host="localhost"; // Host name
    $username=""; // Mysql username
    $password=""; // Mysql password
    $db_name="test"; // Database name
    $tbl_name="test_mysql"; // Table name
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
     // Insert data into mysql
    $sql="INSERT INTO $tbl_name (Ref, CoEmpName, InvDate, Branch, BrAddr, BrApt, BrCity, BrProv, BrCtry, BrZip, AssDesc, AssPrice) VALUES ('$Ref','$CoEmpName',".
    "'$InvDate','$Branch','$BrAddr','$BrApt','$BrCity','$BrProv','$BrCtry',".
    "'$BrZip','$AssDesc[0]','$AssPrice[0]')";
    $result=mysql_query($sql);
    
    // if successfully insert data into database, displays message "Successful".
    if($result)
    {
    echo "Successful";
    }
    else {
    echo "ERROR";
    }
    
    // close connection
    mysql_close();
    ?>
    Code (markup):
    untested, but i use to use that sort of way.
    should work, if not make sure your table fields are the same and remember to
    $host="localhost"; // Host name
    $username=""; // Mysql username
    $password=""; // Mysql password
    $db_name="test"; // Database name
    $tbl_name="test_mysql"; // Table name
    Code (markup):
    fill out that! :)
     
    Aaron700, May 4, 2008 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Change:

    
    $query = "INSERT INTO invoices VALUES('$Ref','$CoEmpName',"."'$InvDate','$Branch','$BrAddr','$BrApt','$BrCity','$BrProv','$BrCtry',"."'$BrZip','$AssDesc[0]','$AssPrice[0]')";
    mysql_query($query, $link);
    
    Code (markup):
    To:

    
    $query = "INSERT INTO invoices VALUES('$Ref','$CoEmpName','$InvDate','$Branch','$BrAddr','$BrApt','$BrCity','$BrProv','$BrCtry','$BrZip','$AssDesc[0]','$AssPrice[0]')";
    mysql_query($query, $link) or die(mysql_error()); 
    
    Code (markup):
    Peace,
     
    Barti1987, May 4, 2008 IP
  4. MichaelLewis

    MichaelLewis Active Member

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #4
    The code:

    or die(mysql_error());

    gave me the answer. It gave me an error message saying that the number of fields I was trying to insert did not match the number of fields in my database table.

    Many thanks,
    Michael
     
    MichaelLewis, May 5, 2008 IP
  5. ariffin246

    ariffin246 Well-Known Member

    Messages:
    247
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #5
    Drop The database and reload the query
     
    ariffin246, May 5, 2008 IP