#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0

    Queries not working


    Any query that I've written isn't working and I can not for the life of me figure out why... To be honest I'm struggling to transition from mysql and php 4 to php 5 and mysqli, but as far as I can tell my queries are fine here is an example below. I'd appreciate some help, I have less than 2 hours to get this done! Thanks

    PHP Code:
    if(isset($_POST['submit'])){ $name $_POST["name"]; $email $_POST["email"]; $subject $_POST["subject"]; $message $_POST["message"]; $sql "INSERT INTO contact (name, email, subject, message ) VALUES ('$name' , '$email' , '$subject' , '$message')"$query mysqli_query($connection$sql); 
    Last edited by neg89; June 24th, 2013 at 02:52 PM. Reason: code block
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0
    I've also tried to use replace instead of insert, no joy.
  4. #3
  5. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by neg89
    Any query that I've written isn't working and I can not for the life of me figure out why... To be honest I'm struggling to transition from mysql and php 4 to php 5 and mysqli, but as far as I can tell my queries are fine here is an example below. I'd appreciate some help, I have less than 2 hours to get this done! Thanks

    PHP Code:
    if(isset($_POST['submit'])){ $name $_POST["name"]; $email $_POST["email"]; $subject $_POST["subject"]; $message $_POST["message"]; $sql "INSERT INTO contact (name, email, subject, message ) VALUES ('$name' , '$email' , '$subject' , '$message')"$query mysqli_query($connection$sql); 
    First, add some error output after your query.
    print mysqli_error ($connection);

    Second, you need to escape the values you're getting from those $_POST variables. You're open to SQL Injection right now.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    I have no idea what "isn't working" means. What exactly is the problem? Do you get an error message? Does the database connection even work? Can you do simple queries like "SELECT 1"?

    We're not psychic, you know?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0
    Hey there,


    Are you sure your host is updated to the current PHP version and is letting you USE queries?

    Check your PHP Config.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0
    Thanks, this script is just to test whether i could write a simple query that would work, my other stuff is secure. I've been using error checks and there is nothing coming up, it's like the form is submitting but it isnt. The form action is good, the connection is good, the query appears to be good... but nothing...
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by Hightax
    Hey there,


    Are you sure your host is updated to the current PHP version and is letting you USE queries?

    Check your PHP Config.
    Yep, its a dedicated server, all up to date and other websites i have on the server use queries just fine. In fact other queries in this same directory work just fine, it's just every query ive written today.
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Well, the code isn't really suitable for testing database queries, because there are too many other factors.

    Write a simple script which only sets up the connection and then does a single query. Nothing else.

    You should also activate MySQLi error reporting (and PHP error reporting, of course) to make sure you're not missing any error message:

    PHP Code:
    <?php

    // report all errors
    error_reporting(-1);

    // enable exceptions in MySQLi driver 
    $mysqli_driver = new mysqli_driver(); 
    $mysqli_driver->report_mode MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT

    // connect to database
    $database = new mysqli('localhost''user''pw''db');

    // do query
    $query $database->query('SELECT 1 AS test');

    var_dump$query->fetch_assoc() );
    What do you get?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by Jacques1
    Well, the code isn't really suitable for testing database queries, because there are too many other factors.

    Write a simple script which only sets up the connection and then does a single query. Nothing else.

    You should also activate MySQLi error reporting (and PHP error reporting, of course) to make sure you're not missing any error message:

    PHP Code:
    <?php

    // report all errors
    error_reporting(-1);

    // enable exceptions in MySQLi driver 
    $mysqli_driver = new mysqli_driver(); 
    $mysqli_driver->report_mode MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT

    // connect to database
    $database = new mysqli('localhost''user''pw''db');

    // do query
    $query $database->query('SELECT 1 AS test');

    var_dump$query->fetch_assoc() );
    What do you get?
    It connected fine, I got an array, that's what I'm saying though, the query seems fine, and I know everything else is fine but it's like the query doesn't work.
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Do you wanna solve the problem? Or do you wanna keep wondering about how this is oh so strange and that everything works fine and yet somehow doesn't?

    We need hard facts, not claims and assumptions. You said that "any query that I've written isn't working", and I gave you a query which appearently does work. Now it's time to figure out what the difference between those two cases is.

    Pick a concrete script that "doesn't work" (not your testing stuff). Add the error reporting just like in my code. Run the script like you normally would. Are you getting any errors? If not, check if the faulty query is even reached during code execution. Tell us the result and post the code here.

    Debugging means investigating the problem systematically. Statements like "it all seems fine" or "it all worked yesterday" don't get you anywhere.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,971
    Rep Power
    375
    have you got the closing } for the if statament?

    echo out the query, try it in mysql directly and see what happens.
  22. #12
  23. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    No. He needs to debug his real code, not some random test script. The test script is far too complicated and may be full of other issues. What's the point of repairing that?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo