Populating mysql database using flash form and PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Head In A Pan
    New Member
    • Mar 2007
    • 25

    Populating mysql database using flash form and PHP

    OK... I am now heading into day 3 of attempting to populate a database through my flash form!! I figure I need some help before I crack!

    I just can't get it to work at all...

    I am able to get my variables from flash for the purposes of sending them via email but need a PHP/SQL guru to tell me what I am doing so horribly wrong in this case... it may be something really obvious to someone above my novice level - Please? ;)

    Here's my code:

    Actionscript:
    Code:
    function sendDb() {
    	
    	var dataOut:LoadVars = new LoadVars();
    	var dataIn:LoadVars = new LoadVars();
    	
    	dataOut.first_name = first_name.text;//First Name
        dataOut.last_name = last_name.text;//Last Name
    	dataOut.website_url = website_url.text;//Website URL
    	dataOut.bus_name = bus_name.text;//Business Name
    	dataOut.contact_number = contact_number.text;//Contact Number
    	dataOut.website_cat = website_cat.text;//Website Catagory
    	dataOut.website_caption = website_caption.text;//Website Caption
    	dataOut.email_address = email_address.text;//Email address
    	dataOut.continent = continent.text;//Continent
    	dataOut.enter_coord = enter_coord.text;//X Y Coordinates
    	dataOut.total_boxes = total_boxes;//Total Boxes
    	dataOut.box_numbers = box_numbers;//Box Numbers
    	dataOut.need_website = need_website.selected;//I need a new website!
    	dataOut.terms = terms.selected;//I have read the terms & conditions
    
       dataOut.sendAndLoad("buyForm_db.php", dataIn, "POST");
       gotoAndPlay("Thankyou");
    };
    
    btn_submit.onRelease = function() {
        sendDb();
    };
    And my first PHP:
    Code:
    <?php 
        require("db.php"); 
    
        
    /*php varibales to be used*/  
    
    	$first_name =  $_POST['first_name'];  
    	$last_name =  $_POST['last_name'];
    	$website_url =  $_POST['website_url'];
    	$bus_name =  $_POST['bus_name']; 
    	$contact_number =  $_POST['contact_number'];
    	$website_cat =  $_POST['website_cat']; 
    	$website_caption =  $_POST['website_caption'];
    	$email_address =  $_POST['email_address'];
    	$continent =  $_POST['continent'];
    	$enter_coord =  $_POST['enter_coord'];
    	$total_boxes =  $_POST['total_boxes'];  
    	$box_numbers =  $_POST['box_numbers'];
    	$need_website =  $_POST['need_website'];
    	$terms =  $_POST['terms'];
    
    /* Strip any escape characters etc */
    
        $first_name = stripslashes($first_name); 
        $last_name = stripslashes($last_name);
    	$website_url = stripslashes($website_url);
    	$bus_name = stripslashes($bus_name); 
    	$contact_number = stripslashes($contact_number);
    	$website_cat = stripslashes($website_cat); 
    	$website_caption = stripslashes($website_caption);
    	$email_address = stripslashes($email_address);
    	$continent = stripslashes($continent);
    	$enter_coord = stripslashes($enter_coord);
    	$total_boxes = stripslashes($total_boxes);  
    	$box_numbers = stripslashes($box_numbers);
    	$need_website = stripslashes($need_website);
    	$terms = stripslashes($terms); 
    
        $q1 = "INSERT INTO Customers (first_name, last_name, website_url, bus_name, contact_number, website_cat, db_password, website_caption, email_address, continent, enter_coord, total_boxes, box_numbers, need_website ,terms) VALUES ('$first_name', '$last_name', '$website_url', '$bus_name', '$contact_number', '$website_cat','$website_caption','$email_address', '$db_password', '$continent', '$enter_coord', '$total_boxes', '$need_website', '$terms')"; 
    
    
        mysql_close($connection);      
    ?>
    And my initial connection php:
    Code:
    <? 
    /*  Database Information - Required!!  */
    /* -- Configure the Variables Below --*/
    $dbhost = 'localhost';
    $dbusername = 'my_username';
    $dbpasswd = 'my_password';
    $database_name = 'my_database';
    
    /* Database Stuff, do not modify below this line */
    
    $connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd") 
    	or die ("Couldn't connect to server.");	
    	
    echo "connection = $connection";
    	
    $db = mysql_select_db("$database_name", $connection)
    	or die("Couldn't select database.");
    
    echo "db = $db";	
    
    ?>
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    I don't about flash. but from flash script if you can send the variables to PHP side successfully i can help here after. in your script one line id missing. thats the problem.

    to check whether your flash is sending the data successfully before you put it to the sql script print it using echo.
    here is your coding.

    for db connection this is enough.
    db.php
    [PHP]<?
    $con = mysql_connect(' localhost', 'root', 'dba') or die ("Could not connect to the Database");
    mysql_select_db ('test', $con) or die (mysql_error()) ;
    ?> [/PHP]

    insert.php
    [PHP]<?
    // your Variables goes here

    $q1 = "INSERT INTO Customers (first_name, last_name, website_url, bus_name, contact_number, website_cat, db_password, website_caption , email_address, continent, enter_coord, total_boxes, box_numbers, need_website ,terms) VALUES ('$first_name', '$last_name', '$website_url', '$bus_name', '$contact_numbe r', '$website_cat', '$website_capti on','$email_add ress', '$db_password', '$continent', '$enter_coord', '$total_boxes', '$need_website' , '$terms')";

    if (!mysql_query($ q1,$con))
    {
    echo 'Error';
    }else
    {
    echo 'Done';
    }
    mysql_close($co n);
    ?>[/PHP]

    mysql_query(sql _query,connecti on_string)

    i have enclosed this one in side a if condition for trapping the errors, otherwise this line is enough without error trapping.

    Comment

    • Head In A Pan
      New Member
      • Mar 2007
      • 25

      #3
      Ok - I'm trying to make it work... but bearing in mind I don't know what's going on... Do I put my login info into the ('localhost','r oot','dba') ?

      There are a few different databases... How do I target the right db?





      Originally posted by ajaxrand
      I don't about flash. but from flash script if you can send the variables to PHP side successfully i can help here after. in your script one line id missing. thats the problem.

      to check whether your flash is sending the data successfully before you put it to the sql script print it using echo.
      here is your coding.

      for db connection this is enough.
      db.php
      [PHP]<?
      $con = mysql_connect(' localhost', 'root', 'dba') or die ("Could not connect to the Database");
      mysql_select_db ('test', $con) or die (mysql_error()) ;
      ?> [/PHP]

      insert.php
      [PHP]<?
      // your Variables goes here

      $q1 = "INSERT INTO Customers (first_name, last_name, website_url, bus_name, contact_number, website_cat, db_password, website_caption , email_address, continent, enter_coord, total_boxes, box_numbers, need_website ,terms) VALUES ('$first_name', '$last_name', '$website_url', '$bus_name', '$contact_numbe r', '$website_cat', '$website_capti on','$email_add ress', '$db_password', '$continent', '$enter_coord', '$total_boxes', '$need_website' , '$terms')";

      if (!mysql_query($ q1,$con))
      {
      echo 'Error';
      }else
      {
      echo 'Done';
      }
      mysql_close($co n);
      ?>[/PHP]

      mysql_query(sql _query,connecti on_string)

      i have enclosed this one in side a if condition for trapping the errors, otherwise this line is enough without error trapping.

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        Just take a look at for the coding.

        [PHP]<?
        $con = mysql_connect(' localhost', 'Your_user_name ', 'Your_password' ) or die ("Could not connect to the Database");
        mysql_select_db ('Your_database _name', $con) or die (mysql_error()) ;
        ?> [/PHP]

        This is a Global file for your entire web site. In your original post also you are using the same, only thing i did here i made it in to two three lines.
        this is the connection string to your database. make sure to modify user name,password and database name.

        until you use this line in your other php file you can call for that.

        [PHP]
        require 'db.php';
        //Rest of the coding goes here
        [/PHP]

        hope that it will help out for you. come back if you found any difficulties down there.
        and try this Tutorial

        Comment

        • Head In A Pan
          New Member
          • Mar 2007
          • 25

          #5
          Hello again,
          Thanks so much for your help -
          I have followed both your recommendations and the tutorial but for the life of me I cannot get any data into my database.

          I have even deleted the original db & created an entirely new db - just to make sure it's ok.

          I have all of my scripts sitting in a separate folder to the root directory - Is this perhaps the problem?
          I created a folder called db_test - this is where I have my db.php, buy_form_db.php & my buyForm.swf

          I have stripped my scripts back to the bare minimum but still have an empty database aside from my initial test data.

          I know the variables are coming out of flash to php as my code mails fine.

          Any ideas where I could be going wrong?

          Comment

          • mohsenhosseini
            New Member
            • Mar 2007
            • 28

            #6
            hi

            when you create you $sql echo it before or after mysql_query() function.

            then copy and past it in your Database GUI like phpmyadmin query section.

            if your query run without error then try to put mysql_error() after mysql_query() to see why you cant insert into DB;

            best regards.
            MH.

            Comment

            • Head In A Pan
              New Member
              • Mar 2007
              • 25

              #7
              Thanks Mohsenhosseini -
              This sounds interesting... I wasn't sure about this query section...
              Everything I put into it it tells me is a syntax error.
              I'm going to look it up -
              So are you saying I need to put some code into the query section as well?



              Originally posted by mohsenhosseini
              hi

              when you create you $sql echo it before or after mysql_query() function.

              then copy and past it in your Database GUI like phpmyadmin query section.

              if your query run without error then try to put mysql_error() after mysql_query() to see why you cant insert into DB;

              best regards.
              MH.

              Comment

              • Head In A Pan
                New Member
                • Mar 2007
                • 25

                #8
                Still no joy -

                In my 'SQL' section on phpmyadmin I have what I think is a query
                SELECT * FROM `Customers` WHERE 1

                This seems pretty standard - I am going from the other databases for this site and they all have the same.

                I have tried putting in other code and it hates it - giving me a syntax.
                I am at a complete loss.

                Could it be the WHERE 1? Should I tell it to look somewhere else?
                I know nothing of databases.

                Comment

                • mohsenhosseini
                  New Member
                  • Mar 2007
                  • 28

                  #9
                  hi again

                  send me these
                  1 DB schema !
                  2- flash file (swf)
                  3- php codes

                  i will check them and try to fix them !

                  regards.
                  mh

                  Comment

                  • mohsenhosseini
                    New Member
                    • Mar 2007
                    • 28

                    #10
                    hi again!
                    run your code with ECHO command like it
                    [PHP]
                    $q1 = "INSERT INTO Customers (first_name, last_name, website_url, bus_name, contact_number, website_cat, db_password, website_caption , email_address, continent, enter_coord, total_boxes, box_numbers, need_website ,terms) VALUES ('$first_name', '$last_name', '$website_url', '$bus_name', '$contact_numbe r', '$website_cat', '$website_capti on','$email_add ress' , '$db_password', '$continent', '$enter_coord', '$total_boxes', '$need_website' , '$terms')";

                    echo $q1;
                    [/PHP]

                    then send us the output string.

                    tnx

                    Comment

                    • Head In A Pan
                      New Member
                      • Mar 2007
                      • 25

                      #11
                      Hey!! Thanks for being so patient -
                      I am meant to be a flash designer but somehow I am coding in languages I never contemplated! :)

                      Where would I put that ECHO string you just posted?
                      Does it go somewhere into phpmyadmin or as a part of one of my php codes?

                      And how do I get a DB schema to send to you?

                      And how do I attach/send you my files?

                      Sorry for all the questions - I've been up a very long time...

                      Comment

                      • Head In A Pan
                        New Member
                        • Mar 2007
                        • 25

                        #12
                        Hello again -
                        I have posted the files for you at:
                        http://www.scimple.com .au/Download/db.zip

                        See what you can make of it -
                        As this is my first attempt at databases it will be interesting to see where I am going wrong -
                        I start off slowly but once I have some knowledge I am fine from there.
                        I am teaching myself all of this stuff from home so have no colleagues on hand to help me... apart from cool people like yourself. ;)

                        Comment

                        • ak1dnar
                          Recognized Expert Top Contributor
                          • Jan 2007
                          • 1584

                          #13
                          I went through the URL of the zip file that you posted,but only thing i could find out from there your swf file, connection string file and a pdf file for Table schema.

                          But there is no file for get the input variables from your flash file.

                          in both the php files the content is this.

                          [PHP]<?
                          $con = mysql_connect(' localhost', 'woeru_user', 'mag00') or die ("Could not connect to the Database");
                          mysql_select_db ('woeru_buy', $con) or die (mysql_error()) ;
                          ?> [/PHP]

                          Please do like this upload the all the files with you again.
                          and we need the mySQL script that you are using.
                          from your connection string i can find out the Name of the database.
                          'woeru_buy

                          I need the SQL script.
                          go to phpmyadmin and select the database name from the left side pane.
                          click the table from there that you have already created.

                          right side pane will display the name of the table.
                          click the export tab and click the go button, you will get the complete script.
                          copy it and put it to a note pad and send it.

                          and if there is any changes for this line post it also.
                          Code:
                           dataOut.sendAndLoad("buyForm_db.php", dataIn, "POST");

                          Comment

                          • Head In A Pan
                            New Member
                            • Mar 2007
                            • 25

                            #14
                            Hello! Thanks for getting back on board... Much appreciated!

                            OK - my files are updated:
                            http://www.scimple.com .au/Download/db.zip

                            And the POST actionscript has changed - I reverted back to a previous script used for my email but it works exactly the same:

                            var dataOut is now var email
                            var dataIn is now var buyForm

                            as code:
                            email.sendAndLo ad("buyForm_db. php", buyForm, "POST");

                            Thanks.

                            Comment

                            • ak1dnar
                              Recognized Expert Top Contributor
                              • Jan 2007
                              • 1584

                              #15
                              In your Your SQL Script one column item is missing you are passing 14 columns, but only 13 (box_numbers missing)is having values for them then It will caused to my SQL errors. You can't find that error because after submitting the SWF form to buyForm_db.php again you are calling to another thank you page(I don't know is it a page or some thing its flash).This is not a good idea. remove this
                              Code:
                              gotoAndPlay("Thankyou");
                              from your flash script for now. first try to post the data. if you want from php also you can call for another page. its there in my Coding.

                              Put your swf file to HTML page what_ever.html and call for that html page like.
                              http://localhost/dir_name/what_ever.html

                              submit the form with values and (before that remove that goto thank you part)
                              if there is error it will display in buyForm_db.php page.

                              if no errors check your database table for the values. select that the table as i told in earlier post. go to browse or select the run the default SQL coding from SQL tab in phpmyadmin.

                              Code:
                              SELECT * FROM `products` WHERE 1
                              <?php
                              require("db.php ");


                              /*php varibales to be used for next php script*/

                              $first_name = $_POST['first_name'];
                              $last_name = $_POST['last_name'];
                              $website_url = $_POST['website_url'];
                              $bus_name = $_POST['bus_name'];
                              $contact_number = $_POST['contact_number '];
                              $website_cat = $_POST['website_cat'];
                              $website_captio n = $_POST['website_captio n'];
                              $email_address = $_POST['email_address'];
                              $continent = $_POST['continent'];
                              $enter_coord = $_POST['enter_coord'];
                              $total_boxes = $_POST['total_boxes'];
                              $box_numbers = $_POST['box_numbers'];
                              $need_website = $_POST['need_website'];
                              $terms = $_POST['terms'];

                              /* Strip any escape characters etc */

                              $first_name = stripslashes($f irst_name);
                              $last_name = stripslashes($l ast_name);
                              $website_url = stripslashes($w ebsite_url);
                              $bus_name = stripslashes($b us_name);
                              $contact_number = stripslashes($c ontact_number);
                              $website_cat = stripslashes($w ebsite_cat);
                              $website_captio n = stripslashes($w ebsite_caption) ;
                              $email_address = stripslashes($e mail_address);
                              $continent = stripslashes($c ontinent);
                              $enter_coord = stripslashes($e nter_coord);
                              $total_boxes = stripslashes($t otal_boxes);
                              $box_numbers = stripslashes($b ox_numbers);
                              $need_website = stripslashes($n eed_website);
                              $terms = stripslashes($t erms);



                              $q1 = "INSERT INTO Customers (
                              first_name,
                              last_name,
                              website_url,
                              bus_name,
                              contact_number,
                              website_cat,
                              website_caption ,
                              email_address,
                              continent,
                              enter_coord,
                              total_boxes,
                              box_numbers,
                              need_website ,
                              terms
                              ) VALUES (
                              '$first_name',
                              '$last_name',
                              '$website_url',
                              '$bus_name',
                              '$contact_numbe r',
                              '$website_cat',
                              '$website_capti on',
                              '$email_address ' ,
                              '$continent',
                              '$enter_coord',
                              '$total_boxes',
                              '$box_numbers',
                              '$need_website' ,
                              '$terms'
                              )";

                              mysql_query($q1 ,$con) or die (mysql_error()) ;
                              mysql_close($co n);
                              // header('Locatio n: thankyou.html') ;
                              // Onece You uncomment this after submitting data you can call for another Page from PHP it self
                              ?> )

                              Comment

                              Working...