Results 1 to 14 of 14

Thread: MySQL Insert

  1. #1
    Join Date
    Oct 2006
    Posts
    31

    MySQL Insert

    I hate to continually ask what I feel like are like repetitive questions but I cannot see anything wrong with this query I am trying to execute. If anyone would be so kind to help, I would really appreciate it.

    Assume the variables are filled, and the column names are right, is there anything wrong with this:

    $result = mysql_query("INSERT INTO users (group, user_name, password, first_name, last_name, gender, email_address) VALUES ('$group', '$user_name', '$password', '$first_name', '$last_name', '$gender', $email_address')");

  2. #2
    Join Date
    Jan 2006
    Location
    Scotland
    Posts
    214
    Yes....

    $email_address'
    It must read '$email_address' with opening comma !

  3. #3
    Join Date
    Oct 2006
    Posts
    31
    *kicks self for overlooking*... I made the change but still it wont execute.

    Updated Query
    $result = mysql_query("INSERT INTO users (group, user_name, password, first_name, last_name, gender, email_address) VALUES ('$group', '$user_name', '$password', '$first_name', '$last_name', '$gender', '$email_address')");

  4. #4
    Join Date
    Jan 2006
    Location
    Scotland
    Posts
    214
    Sorry, I cannot see anything wrong with this query...maybe someone more versed at mysql queries can help....

  5. #5
    Join Date
    Mar 2006
    Posts
    965
    If you post what this variable: $email_address is equal to, it might be possible to determine the cause of the problem.

  6. #6
    Join Date
    Oct 2006
    Posts
    31
    lilman[at]gmail[dot]com

  7. #7
    Join Date
    Apr 2002
    Location
    Denver, Colorado
    Posts
    269
    I'm no PHP guru, but when variables are put into quotes, don't they become a literal string? example: when $email_address is put in quotes, instead of the value of $email_address being inserted, "$email_address" is inserted into the DB?

  8. #8
    Join Date
    Mar 2006
    Posts
    965
    lilman[at]gmail[dot]com
    Replace:

    PHP Code:
    $email_address "lilman[at]gmail[dot]com"
    with:

    PHP Code:
    $email_address = (stripslashes(trim("[email protected]")));

    $replace_at_sign = (preg_match("/^.$/i"$email_address)) ? str_replace("@""[at]"$email_address) : "";
    $replace_dot_sign = (preg_match("/@/i"$email_address)) ? str_replace(".""[dot]"$email_address) : "";
    $check_sign_characters = (isset($replace_dot_sign) && isset($replace_at_sign)) ? $replace_at_sign.$replace_dot_sign ""
    Then, from your SQL statement, replace your $email_address variable with $check_sign_characters. Then, remove the quotes from that variable. No need to use them in those cases.

  9. #9
    Join Date
    Oct 2006
    Posts
    31
    I guess I didn't get what you were asking for, the $email_address is the value of $_POST["email_address"] but I did what you suggested and still it will not execute the query.

  10. #10
    Join Date
    Mar 2006
    Posts
    965
    That's what I asked for - the first time actually.

    After replacing the above,

    replace that line:

    PHP Code:
    $email_address = (stripslashes(trim("[email protected]"))); 
    with:

    PHP Code:
    $email_address = (isset($_POST['email_address'])) ? (stripslashes(trim($_POST['email_address']))) : ""

  11. #11
    Join Date
    Oct 2006
    Posts
    31
    Tried it but it still will not execute.

  12. #12
    Join Date
    Oct 2006
    Posts
    31
    Here is the entire code, maybe it is something other than mysql_query()

    $group = $_POST['group'];
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $user_name = $_POST['user_name'];
    $email_address = (isset($_POST['email_address'])) ? (stripslashes(trim($_POST['email_address']))) : "";
    $password = $_POST['password'];
    $password2 = $_POST['password2'];
    $gender = $_POST['gender'];
    if(mysql_query("INSERT INTO users (group, user_name, password, first_name, last_name, gender, email_address) VALUES (".$group.", ".$user_name.", ".$password.", ".$first_name.", ".$last_name.", ".$gender.", ".$email_address.")"))
    echo "successful in registering you an account";
    else
    {
    echo "couldn't insert";
    }

  13. #13
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342
    PHP Code:
    $result mysql_query("INSERT INTO users (group, user_name, password, first_name, last_name, gender, email_address) VALUES ('$group', '$user_name', '$password', '$first_name', '$last_name', '$gender', '$email_address')") or $error mysql_error();

    if(
    $error != NULL){echo "Error: $error";} 
    Give us the error.

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  14. #14
    Join Date
    Oct 2006
    Posts
    31
    I figured out the problem. There is a keyword in MySQL group which was causing problems for me because I named a column group. Problem resolved. Thanks for the help

Posting Permissions

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