Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Mar 2007
    Posts
    26

    Unanswered: trouble doing basic mysql insert

    Does anyone know why this isnt getting inserted into the database?
    I have another insert thats working fine. Just this one is not.

    $name = $_POST['name'];
    $address = $_POST['address'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];

    $insert_details = "INSERT INTO participant_info VALUES('$name', '$address', '$phone', '$email')";
    mysql_query($insert_details);

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    please show what you get for this query --
    Code:
    SHOW CREATE TABLE participant_info
    also, have you tried running the query outside of php (with actual values substituted for the php variables)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Nov 2003
    Posts
    2,935
    Provided Answers: 12
    This is not a SQL script, this is a PHP script.

    The first step in isolating SQL problems that occur in the programming language is to run the generated SQL directly against the database. In my experience if 90% of the time the program code (i.e. in your case your PHP code) is the reason - not the SQL itself.

    And for future questions, you will get more accurate answers if you try to be more detailed than just "isn't working" (e.g. showing the error message)

  4. #4
    Join Date
    Mar 2007
    Posts
    26

    heres the table

    Hi, I havent tried running a query outside of php itself, I really not sure how to do this?...do you mean just insert a some values manualy?... I am using same code for another insert on a different table and it works also I get no error messages from my php script. any ideas?.... sorry for being vague, i just wanted to try an keep the post brief.
    Regards, John


    -- Table structure for table `participant_info`
    --

    CREATE TABLE `participant_info` (
    `name` varchar(50) NOT NULL,
    `address` varchar(55) NOT NULL,
    `phone` smallint(12) NOT NULL,
    `email` varchar(55) NOT NULL,
    PRIMARY KEY (`email`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    --
    -- Dumping data for table `participant_info`
    --

  5. #5
    Join Date
    Mar 2007
    Posts
    26

    increment issue / related problem? to insert?

    Im not sure if this is somehow related to the previous problem, but this table will not auto increment as expected when doing an insert of data, I have to generate a random number in php then it works...really strange as it did work locally when i sent a nul ("") value for "userid" field. When live though i have to actually send a value for that parameter.
    John

    -- Table structure for table `user_registrations`
    --

    CREATE TABLE `user_registrations` (
    `userid` int(6) NOT NULL auto_increment,
    `theday` date NOT NULL,
    `seats` smallint(4) NOT NULL,
    PRIMARY KEY (`userid`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=119309 ;

    --
    -- Dumping data for table `user_registrations`
    --

    INSERT INTO `user_registrations` (`userid`, `theday`, `seats`) VALUES
    (109845, '2007-03-11', 4),
    (66471, '2007-03-05', 1),
    (35629, '2007-03-11', 1),
    (27049, '2007-03-07', 1),
    (119308, '2007-03-07', 1),

  6. #6
    Join Date
    Mar 2004
    Posts
    480
    I'll take an educated guess in that you already have that email address in your table. you should get into the habit of error handling in your scripts.

    as to how to connect to mysql without the php code, you can use the command line client for mysql to test your queries and make sure they work okay.

  7. #7
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by john_cmd
    ... this table will not auto increment as expected when doing an insert of data
    yes it will
    Code:
    CREATE TABLE john_cmd (
    `userid` int(6) NOT NULL auto_increment,
    `theday` date NOT NULL,
    `seats` smallint(4) NOT NULL,
    PRIMARY KEY (`userid`)
    ) ;
    
    insert into john_cmd (theday,seats) values
     ( '2007-03-22', 9 )
    ,( '2007-03-22', 3 )
    ,( '2007-03-22', 7 )
    ;
    select * from john_cmd;
    
    userid      theday      seats
       1      2007-03-22      9
       2      2007-03-22      3
       3      2007-03-22      7
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  8. #8
    Join Date
    Mar 2007
    Posts
    26

    hi guelphdad

    no emails are present at all in the DB, as I couldnt get it to insert in the first place, but thanks...

  9. #9
    Join Date
    Mar 2007
    Posts
    26

    hi r937

    when I try an insert without sending a number of some sort for the userid param, my database is never updated and does insert of the data...when I generate a random num from php to send with the insert giving that first param a value it works....
    how else can i trouble shoot this?
    John

  10. #10
    Join Date
    Mar 2007
    Posts
    26

    table structure

    hi since i am beginner using Databases, am i creating the table correctly? to insert basic data? do I need another field that is for an ID to autoincremenet?
    Best, John

    -- Table structure for table `participant_info`
    --

    CREATE TABLE `participant_info` (
    `name` varchar(50) NOT NULL,
    `address` varchar(55) NOT NULL,
    `phone` smallint(12) NOT NULL,
    `email` varchar(55) NOT NULL,
    PRIMARY KEY (`email`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    --
    -- Dumping data for table `participant_info`
    --

  11. #11
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    okay since you are just getting going, please do the following

    - drop your table
    - go to post #7 and copy the script down to but not including the blue stuff
    - open an sql window in phpmyadmin or the command line or whatever it is you're using to administer your database
    - paste in the code, execute it, and let us know what happened
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  12. #12
    Join Date
    Mar 2007
    Posts
    26

    ok

    will do thanks!

  13. #13
    Join Date
    Mar 2007
    Posts
    26

    working

    yes you are right i was wrong, so seems it must be the way i am sending the insert cmd? or param from php right? actually this var is coming from flash into php then does insert.
    John

  14. #14
    Join Date
    Mar 2007
    Posts
    26

    is this where i am going wrong?

    flash sending id=""
    so since that didnt work sending "", i used a random num for a value, should I just have left that parameter blank?
    like this:
    $insert_seat = "INSERT INTO user_registrations VALUES('', '$thedate', '$guests')";
    mysql_query($insert_seat);

    what i have now, php is:

    $userid = $_POST["id"];
    $thedate = $_POST["date"];
    $guests = $_POST["guests"];

    $insert_seat = "INSERT INTO user_registrations VALUES('$userid', '$thedate', '$guests')";
    mysql_query($insert_seat);

  15. #15
    Join Date
    Mar 2007
    Posts
    26

    really stupid

    I looked closely at your SQL you posted, and i see the problem now...
    i had this:

    $insert_seat = "INSERT INTO user_registrations VALUES('$userid', '$thedate', '$guests')";

    you had this:

    insert into john_cmd (theday,seats) values
    ( '2007-03-22', 9 )
    ,( '2007-03-22', 3 )
    ,( '2007-03-22', 7 )
    ;

    seems i didnt specify where to insert --> (theday,seats) values

    thanks much!.
    John

Posting Permissions

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