-1

I've been working on this PHP coding which connects AS3 and mySQL up...

I've come across this error which i cant figure out whats the matter... if someone can help with a second pair of eyes i would appreciate it a lot...

Here is updated coding for PHP

   <?php 
if(isset($_POST['userFirstName']) && isset($_POST['userLastName']) && isset($_POST['userEmail']) && isset($_POST['userNumber']) && isset($_POST['userMsg']))
{
    $userFirstName=strip_tags($_POST['userFirstName']);
    $userLastName=strip_tags($_POST['userLastName']);
    $userEmail=strip_tags($_POST['userEmail']);
    $userNumber=strip_tags($_POST['userNumber']);
    $userMsg=strip_tags($_POST['userMsg']);

    // connect with database. 

    $username="root";
    $password="dp10aap";
    $database="b-elite-fitness";

    mysql_connect("localhost","$username","$password") or die (mysql_error());
    mysql_select_db("$database") or die (mysql_error());

    //query for inserting data in database.
    $query="INSERT INTO 'formdp' VALUES('NULL','".mysql_real_escape_string($userFirstName)."','".mysql_real_escape_string($userLastName)."','".mysql_real_escape_string($userEmail)."','".mysql_real_escape_string($userNumber)."','".mysql_real_escape_string($userMsg)."')";

    if($query_run=mysql_query($query))
    {
        echo'Data inserted.';
    } else {
        die(mysql_error());
        }
}
?>  

here is my AS3 coding....

processing_mc.visible = false;

function checkComplete(evt:MouseEvent):void {

    // Create A new URLVariables instance to store the variable
    var phpVars:URLVariables = new URLVariables();

    // Create a variable (e.g. candidate) to send
    phpVars.userFirstName = firstname_txt.text;
    phpVars.userLastName = lastname_txt.text;
    phpVars.userNumber = number_txt.text;
    phpVars.userEmail = email_txt.text; 
    phpVars.userMsg = msg_txt.text;

    // Create a new URLRequest instance sending data to "ascom01.php"
    var myRequest:URLRequest = new URLRequest("form.php");
    // Send data using the POST method
    myRequest.method = URLRequestMethod.POST;

    var loader:URLLoader = new URLLoader();
    loader.load(myRequest);

loader.addEventListener(Event.COMPLETE, response);
function response(e:Event):void
{
    trace(loader.data);
}

if(!firstname_txt.length) {
status_txt.text = "Please enter your first name";
} else if(!lastname_txt.length) {
status_txt.text = "Please enter your last name";
} else if (!email_txt.length) {
status_txt.text = "Please enter your email";
} else if (!number_txt.length) {
status_txt.text = "Please enter your phone number";
} else {

}

}
// handler for the PHP script completion and return of status
function completeHandler(event:Event):void {

processing_mc.visible = false;
firstname_txt.text = "";
lastname_txt.text = "";
email_txt.text = "";
number_txt.text = "";
msg_txt.text = "";

status_txt.text = event.target.data.return_msg;
}

// Add event listener for submit button click
submit_btn.addEventListener(MouseEvent.CLICK, checkComplete);

and the problem which i am having is the data will not send once i click the send button... any ideas?

DP187
  • 19
  • 1
  • 7
  • 2
    what do you mean by `NULL['id']`? – John Woo Mar 10 '13 at 22:08
  • `'NULL['id']',` should be `NULL` – Marin Sagovac Mar 10 '13 at 22:08
  • It's due to the quoted array keys inside the string. [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/13935532#13935532) – Michael Berkowski Mar 10 '13 at 22:09
  • @NicholasPickering without ARRAY, id should be only NULL, it will auto append by ID into DB. – Marin Sagovac Mar 10 '13 at 22:10
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. – Kermit Mar 10 '13 at 22:26

4 Answers4

1

here you go try with this.

    mysql_query("INSERT INTO formdp 
      (firstname, lastname, email, number, message)  VALUES('".$userFirstName['firstname']."','".$userLastName['lastname']."','".$userEmail['email']."','".$userNumber['number']."','".$userMsg['message']."')") 
     or die (mysql_error());
     mysql_close();

* If the id is auto_increment you dont have to insert value in id.

echo_Me
  • 36,552
  • 5
  • 55
  • 77
0

When using arrays in double quoted string, you need to put them in {}

mysql_query("INSERT INTO formdp 
  (id ,firstname, lastname, email, number, message)     
  VALUES(NULL,'{$userFirstName['firstname']}','{$userLastName['lastname']}','{$userEmail['email']}','{$userNumber['number']}','{$userMsg['message']}')")

However it seems to me those arrays don't exist anyway, and you're confused about something else too. Probably you want just:

mysql_query("INSERT INTO formdp 
  (id ,firstname, lastname, email, number, message)     
  VALUES(NULL,'$userFirstName','$userLastName','$userEmail','$userNumber','$userMsg')")

This however still has a potential for SQL injection. THe correct code would be:

<?php

$username="root";
$password="dp10aap";
$database="b-elite-fitness";

mysql_connect("localhost","$username","$password") or die (mysql_error());
mysql_select_db("$database") or die (mysql_error());

if(isset($_POST['userFirstName'])){ $userFirstName = mysql_real_escape_string($_POST['userFirstName']); }
if(isset($_POST['userLastName'])){ $userLastName = mysql_real_escape_string($_POST['userLastName']); }
if(isset($_POST['userEmail'])){ $userEmail = mysql_real_escape_string($_POST['userEmail']); }
if(isset($_POST['userNumber'])){ $userNumber = mysql_real_escape_string($_POST['userNumber']); }
if(isset($_POST['userMsg'])){ $userMsg = mysql_real_escape_string($_POST['userMsg']); }



mysql_query("INSERT INTO formdp 
    (id ,firstname, lastname, email, number, message) 
    VALUES(NULL,'$userFirstName','$userLastName','$userEmail','$userNumber','$userMsg')") 
or die (mysql_error());

And note that it still doesn't handle the case, when one or more of the variables is NOT supplied.

Mchl
  • 60,035
  • 9
  • 112
  • 119
  • exactly that! Should only be NULL, not as another `NULL['id']` as @NicholasPickering suggested. You are right! – Marin Sagovac Mar 10 '13 at 22:12
  • yeah I changed it to just NULL and i still have the same error and when i change the coding completely to what @Mchi suggested i get Undefined variable error for all my variables such as first name last name and so on... any ideas ? – DP187 Mar 10 '13 at 22:20
  • I just changed my coding to the one above which you supplied.... and i still get an error for the VALUES(NULL,'$userFirstName','$userLastName','$userEmail','$userNumber','$userMsg')") line. the errors are Undefined variable for firstname lastname email and so on.... – DP187 Mar 10 '13 at 22:34
  • Just like I said:' it still doesn't handle the case, when one or more of the variables is NOT supplied'. Apparently they're missing from your `$_POST` array. – Mchl Mar 10 '13 at 22:40
  • so the data isn't being received from AS3 ? could you help if thats the error because im sure my AS3 coding is fairly correct... ? – DP187 Mar 10 '13 at 22:42
0

you have conflicting quotes in your query, try :

mysql_query("INSERT INTO formdp
    (id ,firstname, lastname, email, number, message)
    VALUES(
            NULL,
        '".$userFirstName['firstname']."',
        '".$userLastName['lastname']."',
        '".$userEmail['email']."',
        '".$userNumber['number']."',
        '".$userMsg['message']."'
    )
")
or die (mysql_error());
vbird
  • 1
  • 1
0
<?php 
if(isset($_POST['userFirstName'])) && isset($_POST['userLastName']) && isset($_POST['userEmail']) && isset($_POST['userNumber']) && isset($_POST['userMsg']))
{
    $userFirstName=strip_tags($_POST['userFirstName']);
    $userLastName=strip_tags($_POST['userLastName']);
    $userEmail=strip_tags($_POST['userEmail']);
    $userNumber=strip_tags($_POST['userNumber']);
    $userMsg=strip_tags($_POST['userMsg']);

    // connect with database. 

    $username="root";
    $password="dp10aap";
    $database="b-elite-fitness";

    mysql_connect("localhost","$username","$password") or die (mysql_error());
    mysql_select_db("$database") or die (mysql_error());

    //query for inserting data in database.
    $query="INSERT INTO `formdp` VALUES('NULL','".mysql_real_escape_string($userFirstName)."','".mysql_real_escape_string($userLastName)."','".mysql_real_escape_string($userEmail)."','".mysql_real_escape_string($userNumber)."','".mysql_real_escape_string($userMsg)."')";

    if($query_run=mysql_query($query))
    {
        echo'Data inserted.';
    } else {
        die(mysql_error());
        }


}
?>  

Have a look to the above code.
Suggestion:
Try to avoid using mysql_* function in your code as it is depreciated. So, try to use mysqli_* or object oriented approach.

Hare Kumar
  • 689
  • 7
  • 23
  • I got an error Parse error: syntax error, unexpected '&&' (T_BOOLEAN_AND) ...... on line 2...? and thank you – DP187 Mar 10 '13 at 22:54
  • the whole error is... ( ! ) Parse error: syntax error, unexpected '&&' (T_BOOLEAN_AND) in C:\wamp\www\NewtestForm\form.php on line 2 – DP187 Mar 10 '13 at 23:01
  • hey could you help me with a problem I have with my project... ? i got rid of all the errors, however when i try sending the data from AS3 to PHP it wont send.... for example if i enter data into my form it wont send to php... could you help in any way for me to connect it all up... I've been trying to get this form to work for the past week....? DP – DP187 Mar 10 '13 at 23:38
  • @DP187 could u explain your issue in detail? – Hare Kumar Mar 11 '13 at 05:00
  • yes, I have a project which i am currently doing where a user enters data into a form.... this form is created in adobe flash using action script 3... I've completed all my coding, however still when i try to send the data over from AS3 to PHP to MySQL, it wont send... I'm using wampserver and i'm sure i have all the files in the correct place... I've got no errors in PHP no errors in flash so i'm completely lost on where the problem would be... any help at all would be great... I'll post the AS3 coding right now... – DP187 Mar 11 '13 at 12:55
  • @dP187 You please post the code. – Hare Kumar Mar 11 '13 at 13:45
  • I have the code is above...^^^ – DP187 Mar 11 '13 at 14:36
  • @DP187 I guess, u r having issue in your code due to name of the form. Since in your AS3 code you are using "firstname_txt" as a form name attribute to grab the data. But in the php code you are trying to access it with ***userFirstName***. So, you please make sure that you are using the correct name. It would be much better if you post your html form code as well. – Hare Kumar Mar 11 '13 at 14:48
  • what html code would that be ? I'm creating a interactive flash site... from what I've been told all i need is Flash html file, PHP file and connection to mysql... and the "firstname_txt" is the instants name of the element of the form? i'm a little confused now? – DP187 Mar 11 '13 at 15:09