MySQL INSERT - NOT inserting.

Discussion in 'PHP' started by ProtegeSales, Jan 30, 2010.

  1. #1
    Here's the database structure and code.

    All of the information 'prints' correctly if I echo it. I am also pretty sure all my fields are properly inserted in means of whether they are int or varchars, etc. '".$varchar." - $int -- etc.


    CREATE TABLE IF NOT EXISTS `orders` (
      `order_number` int(5) NOT NULL,
      `order_firstname` varchar(50) NOT NULL,
      `order_lastname` varchar(50) NOT NULL,
      `order_addressline1` varchar(20) NOT NULL,
      `order_addressline2` varchar(20) NOT NULL,
      `order_city` varchar(50) NOT NULL,
      `order_state` varchar(2) NOT NULL,
      `order_zipcode` int(5) NOT NULL,
      `order_email` varchar(50) NOT NULL,
      `order_phone` varchar(10) NOT NULL,
      `order_magcode1` varchar(50) NOT NULL,
      `order_years1` int(1) NOT NULL,
      `order_magcode2` varchar(50) NOT NULL,
      `order_years2` int(1) NOT NULL,
      `order_paymentamount` int(3) NOT NULL,
      `order_key` varchar(2) NOT NULL DEFAULT 'ws',
      `order_payment_times` int(2) NOT NULL DEFAULT '1',
      `order_errorcode` varchar(50) NOT NULL,
      `order_gender` varchar(1) NOT NULL,
      `order_magazine` varchar(50) NOT NULL,
      `order_agent` int(10) NOT NULL,
      `order_date` varchar(50) NOT NULL,
      `order_comments` text NOT NULL,
      `admin_comments` text NOT NULL,
      `order_cash` int(5) NOT NULL,
      `order_check` int(5) NOT NULL,
      `order_front` int(3) NOT NULL,
      `order_mic` int(3) NOT NULL,
      `order_ph` int(1) NOT NULL DEFAULT '8',
      `order_total` int(5) NOT NULL,
      `order_issues` varchar(4) NOT NULL,
      `order_subscriber` varchar(50) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    PHP:

    Code:
          $q = "INSERT INTO orders (order_number, 
    		order_firstname,
    		order_lastname
    		order_addressline1,
    		order_addressline2,
    		order_city,
    		order_state,
    		order_zipcode,
    		order_email,
    		order_phone,
    		order_magcode1,
    		order_years1,
    		order_magcode2,
    		order_years2,
    		order_paymentamount,
    		order_key,
    		order_payment_times,
    		order_gender,
    		order_magazine, 
    		order_agent,
    	    order_date,
    		order_comments,
    		admin_comments,
    		order_cash,
    		order_check,		
    		order_front,
    		order_mic,
    		order_ph,
    		order_gift,
    		order_total,
    		order_issues,	
    		order_subscriber
    	   ) VALUES (
    	   ".$receipt.",
    	   '".$order_firstname."',
    	   '".$order_lastname."',
    	   '".$addressline1."',
    	   '".$addressline2."',
    	   '".$order_city."',
    	   '".$state."',
    	   ".$zipcode.",
    	   '".$email."',
    	   '".$phone_number."',
    	   '".$magcode1."',
    	   ".$years1.",
    	   '".$magcode2."',
    	   ".$years2.",
    	   ".$total.",
    	   'ws',
    	   1,
    	   '".$gender."',
    	   '".$magazine."'
    	   ".$agent.",
    	   '".$date."',
    	   '".$order_comments."',
    	   '".$admin_comments."',
    	   ".$cash.",
    	   ".$check.",
    	   ".$front.",
    	   ".$mic.",
    	   8,
    	   '".$gift."',
    	   ".$total.",
    	   ".$issues.", 
    	   '".$order_subscriber."')";
    	   mysql_query($q);
    
    	   
    PHP:
    Again all the input vars are showing correctly when echoing. It just wont insert into the db. Nor will it echo and mysql_error() output.

    Thanks,
    Protege Sales, Inc.
     
    ProtegeSales, Jan 30, 2010 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You neglected to take the result of mysql_query(). What is the result? If it's a MySQL statement handle resource, then the database thinks it did the insert. If not, then something went wrong and there should be a value in mysql_error().
     
    SmallPotatoes, Jan 30, 2010 IP
  3. ProtegeSales

    ProtegeSales Guest

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well, either way with or without the result it still refuses to push a mysql_error.

    Any help with that? Maybe someone can play with this and get something effective out of it (I.E. atleast an error so I can atleast debug.)
     
    ProtegeSales, Jan 30, 2010 IP
  4. DivvyWork

    DivvyWork Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    MySQL queries also require a connection string. You only told it the SQL statement you also have to tell it what database, username and passwords to use (A connection string). (In the line below my connection string is $conn)

    Also notice the "or die" followed by error catching is helpful for preventing sql injection attacks and for catching errors.

    You should write your query something more like:

    $result = mysql_query($q, $conn) or die ('Query failed: '.mysql_error());
     
    DivvyWork, Jan 30, 2010 IP
  5. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The connection string is optional; if you don't supply one then it will use the most recently opened connection. The only time you ever really need to use it is if you are connecting to different database servers at the same time or using different MySQL usernames at the same time.

    Agree about the "or die" though.
     
    SmallPotatoes, Jan 30, 2010 IP
  6. ProtegeSales

    ProtegeSales Guest

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I guess I forgot to mention while feeling list a newbie - I'm not really a newbie. I've been programming with PHP for years - I can't for the life of me debug this though. I always do a lot of mysql work (I have a global function page that has my mysql library.) so I'm very familiar, just can't debug this.

    Thus, coming to DP for help. I'll give the or die() option a try.

    Thanks,
    Protege Sales Inc.
     
    ProtegeSales, Jan 31, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    You don't need the excess quotes, variables can be parsed within double quotes.

    <?php
    
    error_reporting(E_ALL);
    
    $q = "INSERT INTO orders (order_number,
            order_firstname,
            order_lastname
            order_addressline1,
            order_addressline2,
            order_city,
            order_state,
            order_zipcode,
            order_email,
            order_phone,
            order_magcode1,
            order_years1,
            order_magcode2,
            order_years2,
            order_paymentamount,
            order_key,
            order_payment_times,
            order_gender,
            order_magazine,
            order_agent,
            order_date,
            order_comments,
            admin_comments,
            order_cash,
            order_check,       
            order_front,
            order_mic,
            order_ph,
            order_gift,
            order_total,
            order_issues,  
            order_subscriber
           ) VALUES (
           $receipt,
           $order_firstname,
           $order_lastname,
           $addressline1,
           $addressline2,
           $order_city,
           $state,
           $zipcode,
           $email,
           $phone_number,
           $magcode1,
           $years1,
           $magcode2,
           $years2,
           $total,
           'ws',
           '1',
           $gender,
           $magazine
           $agent,
           $date,
           $order_comments,
           $admin_comments,
           $cash,
           $check,
           $front,
           $mic,
           '8',
           $gift,
           $total,
           $issues,
           $order_subscriber)";
           mysql_query($q) or die(mysql_error()); 
           
    ?>
    PHP:
    I Suggest:

    1. Check that you have the same amount of values as your columns.

    2. Check that all variables exists/are defined.
     
    danx10, Jan 31, 2010 IP
  8. gr8webseller

    gr8webseller Peon

    Messages:
    1,097
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    give the single quotes for string,,, like $q = "INSERT INTO orders (order_number,
    'order_firstname',
    'order_lastname',
    'order_addressline1',
    'order_addressline2',
    'order_city',
    'order_state', ,,,,,,,,,, and see weather it works
     
    gr8webseller, Jan 31, 2010 IP
  9. mrhrk01

    mrhrk01 Well-Known Member

    Messages:
    664
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    145
    #9
    Hi there

    I figured out why your mysql insert would not occur.

    It was due to 4 basic mistakes you made in your code:
    1) There was no coma after order_lastname - in mysql code
    2) The order_gift field does not exist - it should have been : order_errorcode - in mysql code
    3) The issues variable needed to have single quotes wrapped around it - in php code
    4) There was another error however i have not documented that - sorry

    Fixed code which worked for me:

    <?php
    $connection = mysql_connect("localhost","root","");
    if (!$connection)
    {
    die("Could not connect to database: " . mysql_error());
    }

    else
    {
    mysql_select_db("test", $connection);
    }

    $receipt=10;
    $order_firstname="fname";
    $order_lastname="lname";
    $addressline1 = "address line 1";
    $addressline2 = "address line 2";
    $order_city = "order city";
    $state = "state";
    $zipcode = 111;
    $email = "email@email.com";
    $phone_number = "111 777 999";
    $magcode1 = "mag code 1";
    $years1 = 9;
    $magcode2= "mag code 2";
    $years2 = 10;
    $total = 100;
    $gender = "m";
    $magazine = "magazine";
    $agent = 1000;
    $date = "date";
    $order_comments = "order comments";
    $admin_comments = "admin comments";
    $cash = 100;
    $check = 100;
    $front = 50;
    $mic = 4;
    $gift= "";
    $issues = "no";
    $order_subscriber = "order subscriber";


    $query = "INSERT INTO orders (order_number,
    order_firstname,
    order_lastname,
    order_addressline1,
    order_addressline2,
    order_city,
    order_state,
    order_zipcode,
    order_email,
    order_phone,
    order_magcode1,
    order_years1,
    order_magcode2,
    order_years2,
    order_paymentamount,
    order_key,
    order_payment_times,
    order_errorcode,
    order_gender,
    order_magazine,
    order_agent,
    order_date,
    order_comments,
    admin_comments,
    order_cash,
    order_check,
    order_front,
    order_mic,
    order_ph,
    order_total,
    order_issues,
    order_subscriber
    ) VALUES (
    ".$receipt.",
    '".$order_firstname."',
    '".$order_lastname."',
    '".$addressline1."',
    '".$addressline2."',
    '".$order_city."',
    '".$state."',
    ".$zipcode.",
    '".$email."',
    '".$phone_number."',
    '".$magcode1."',
    ".$years1.",
    '".$magcode2."',
    ".$years2.",
    ".$total.",
    'ws',
    1,
    '".$gift."',
    '".$gender."',
    '".$magazine."',
    ".$agent.",
    '".$date."',
    '".$order_comments."',
    '".$admin_comments."',
    ".$cash.",
    ".$check.",
    ".$front.",
    ".$mic.",
    8,
    ".$total.",
    '".$issues."',
    '".$order_subscriber."')";

    if(mysql_query($query,$connection))
    {
    echo(" 1 Record added !!");
    }
    else
    {
    die('Unable to insert record -error details: ' . mysql_error());
    }

    mysql_close($connection);
    ?>



    When coding in mysql and php, i would suggest:
    putting together your sql code and testing it in phpmyadmin for e.g. so that your definitely sure your mysql code in correct and working, and then you can worry about your php code. Otherwise you could be debugging an issue with any one of your scripts, yet you would not be sure whether its the php code, or mysql.

    Kind Regards,
    mrhrk01
     
    mrhrk01, Feb 1, 2010 IP
  10. ProtegeSales

    ProtegeSales Guest

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I'm getting this.. I can't seem to figure out what the heck it's screaming about to be honest.

    Unable to insert record -error details: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' '', , 48, 'ws', 1, 'yes', 'male', '', 1, '2010/02/01', '', '', 20,' at line 45
    PHP:
     
    ProtegeSales, Feb 1, 2010 IP
  11. mrhrk01

    mrhrk01 Well-Known Member

    Messages:
    664
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    145
    #11
    The error you have just posted here, is this with the code i provided you, or the code you are using ? The code i provided you works, as i tested it myself.

    If it is with the code you are using, i suggest making alterations, as i suggest above. Did you do that ? (point 1-3) in my previous post .

    It could also be that the gender field you have defined only takes 1 varchar(`order_gender` varchar(1) NOT NULL), yet your trying to enter 'male' into it.

    What version of mysql are you using ?
     
    mrhrk01, Feb 2, 2010 IP
  12. anxggxun

    anxggxun Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    hi, what the result for the query string if you echo the $q variable?
     
    anxggxun, Feb 2, 2010 IP
  13. ProtegeSales

    ProtegeSales Guest

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    This was from your code.

    Here is the result of the $query.

    INSERT INTO orders (order_number, order_firstname, order_lastname, order_addressline1, order_addressline2, order_city, order_state, order_zipcode, order_email, order_phone, order_magcode1, order_years1, order_magcode2, order_years2, order_paymentamount, order_key, order_payment_times, order_errorcode, order_gender, order_magazine, order_agent, order_date, order_comments, admin_comments, order_cash, order_check, order_front, order_mic, order_ph, order_total, order_issues, order_subscriber ) VALUES ( 12345, 'Andrew', 'West', 'street #', 'apt. #', 'frankfort', 'OH', 45628, 'burstroo@gmail.com', '7409985639', '', , '', , 40, 'ws', 1, 'yes', 'male', '', 1, '2010/02/01', '', '', 20, 20, 15, 17, 8, 40, '', 'Andrew West')
    PHP:
    Also my MySQL information is below.

    MySQL
    
        * Server: localhost (MySQL host info: localhost via TCP/IP)
        * Server version: 5.1.36-community-log
        * Protocol version: 10
        * User: root@localhost
        * MySQL charset: UTF-8 Unicode (utf8)
    
    Web server
    
        * Apache/2.2.11 (Win32) PHP/5.3.0
        * MySQL client version: mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $
        * PHP extension: mysqli
    
    
    PHP:
     
    ProtegeSales, Feb 2, 2010 IP
  14. mrhrk01

    mrhrk01 Well-Known Member

    Messages:
    664
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    145
    #14
    Since you have set all the fields in the table to be not null, this means you have to enter something into the fields, which you have not.

    If you run this sql query:
    INSERT INTO orders (order_number, order_firstname, order_lastname, order_addressline1, order_addressline2, order_city, order_state, order_zipcode, order_email, order_phone, order_magcode1, order_years1, order_magcode2, order_years2, order_paymentamount, order_key, order_payment_times, order_errorcode, order_gender, order_magazine, order_agent, order_date, order_comments, admin_comments, order_cash, order_check, order_front, order_mic, order_ph, order_total, order_issues, order_subscriber ) VALUES ( 12345, 'Andrew', 'West', 'street #', 'apt. #', 'frankfort', 'OH', 45628, 'burstroo@gmail.com', '7409985639', 'code1',1 , 'code2',1 , 40, 'ws', 1, 'yes', 'm', 'm', 1, '2010/02/01', 'admin comments', 'order comments', 20, 20, 15, 17, 8, 40, 'none', 'Andrew West')

    It works, the code i provided does work, its just that you could not edit/use it properly.

    As all of the table fields have been set to not null, you have to enter data in each field.
     
    mrhrk01, Feb 2, 2010 IP
  15. ProtegeSales

    ProtegeSales Guest

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Ok, so thanks to everyone here who contributed to this small problem (Well, what turned out to be a small problem). I thank you.

    The real problem here occurred when I was '".$escaping_var."' this way. This is what was causing my MySQL error. I could not properly espace the Variables for some reason. They aren't clashing as I have always understood them to do the way I was attempting to prevent anyhow. It must have been a PHP4 / PHP5 difference I guess.

    Anyhow. I thank you. I can move on with this project. :)
     
    ProtegeSales, Feb 2, 2010 IP
  16. ProtegeSales

    ProtegeSales Guest

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Furthermore. You were correct when you said it was due to fields not being placed in the database, but it really wasn't due to the NOT NULL - I think it was merely for the fact that a previous sql query was not correct, was not placing the correct variables infact it was not printing the original mysql_error() - in plain site. It was printing them in the source code of a <select> list, and I hadn't checked there.
     
    ProtegeSales, Feb 2, 2010 IP