Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation query wont run in page

    ok i need help with this page that updates a mysql database but when it runs i get no errors and it looks like it ran but it never does. also the query is right because if i take it from the page it runs fine.

    php part of code:

    <?php




    $username="xxxxxx";
    $password="xxxxxx";
    $database="xxxxxxx";

    $id=$_POST['id'];
    $name=$_POST['name'];
    $wage=$_POST['wage'];
    $date=$_POST['date'];
    $job=$_POST['job'];
    $type=$_POST['type'];
    $des=$_POST['des'];
    $hours=$_POST['hours'];



    //get query setup to run

    $query = "UPDATE timecard SET `name` = '$name' , `wage` = '$wage' , `date` = '$date' , `job` = '$job' , `type` = '$type' , `des` = '$des' , `hours` = '$hours' , id = '$id' WHERE id = '$id'";



    //connect to mysql

    mysql_connect(localhost,$username,$password);



    //send and run query

    $result = mysql_query($query);


    mysql_close();

    print $result;

    //just for test delete later

    print $query;

    ?>

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,631
    Thanks
    4
    Thanked 147 Times in 138 Posts
    look in the php manual, you want to use mysql_fetch_array or mysql_fetch_assoc and adapt your script with that.

    also note you have no error messaging whatsoever, so you should look into that as well. look at the use of mysql_error() in the php manual as well.

  • #3
    New Coder
    Join Date
    Jul 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Change

    PHP Code:
    $result mysql_query($query); 
    to

    PHP Code:
    $result mysql_query($query) or die(mysql_error()); 
    and run the script to see the mysql errors.

    If you have magic quotes on ignore the following:
    Also I noticed you do not have any addslashes on your variables. If one of the posts contains a character like ' or " it could be trying to read it as part of the code. ie mysql injection.
    PHP and MYSQL
    $string = "3Ip*hKEpanKI#8dmUys&*KqlIJ*P8D";
    $new_string = ereg_replace("[^a-z]", "", $string);
    echo "<a href=http://www.$new_string.org>$new_string</a>";

  • #4
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the help, i will go look at the functions later by why does mysql not give an error for it being incorrect. when i use the error function VERY CONFUSED

  • #5
    New Coder
    Join Date
    Jul 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by KingOfKYA View Post
    Thank you for the help, i will go look at the functions later by why does mysql not give an error for it being incorrect. when i use the error function VERY CONFUSED
    It won't give you the error message unless you ask for it as I posted above.
    Why? because sometimes you may not want other to see your errors for security reasons.
    PHP and MYSQL
    $string = "3Ip*hKEpanKI#8dmUys&*KqlIJ*P8D";
    $new_string = ereg_replace("[^a-z]", "", $string);
    echo "<a href=http://www.$new_string.org>$new_string</a>";

  • #6
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sry about my confusing post what i meant is i tried it and it returns no error

  • #7
    New Coder
    Join Date
    Jul 2007
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by KingOfKYA View Post
    sry about my confusing post what i meant is i tried it and it returns no error
    Oh yeah cool.

    Sorry none of these posts have been helpful, here is another attempted debug. I noticed you have.

    PHP Code:
    //connect to mysql 

    mysql_connect(localhost,$username,$password); 
    but I dont see

    PHP Code:
    mysql_select_db("$database"); 
    Do you have that in your script?
    PHP and MYSQL
    $string = "3Ip*hKEpanKI#8dmUys&*KqlIJ*P8D";
    $new_string = ereg_replace("[^a-z]", "", $string);
    echo "<a href=http://www.$new_string.org>$new_string</a>";

  • #8
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    And this
    Code:
    mysql_connect(localhost,$username,$password);
    should be
    Code:
    mysql_connect('localhost',$username,$password);
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #9
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    Actually, I think there was no mysql_error() because there was no connection to the mysql server. The mysql_query(...) statement would have attempted to create a connection using default values, failed, and issued a PHP Warning message. Error output is likely turned off in php.ini and you don't see anything.

    Either check your web server log for errors and/or turn on full PHP error reporting. When learning a programming language or writing and debugging code, making use of all the error reporting that the language provides will help you find syntax errors, problems with variables (typos, referencing non-existent or un-initialized variables), and failed calls to functions...

    Aerospace_Eng's post will likely correct the connection error and phpandmysql's mysql_select_db(...) code will properly select a database.

    I realize you want to write code quickly to see what the results are, but by writing minimal code that has no error checking logic of the returned values from the mysql_connect(), mysql_select_db(), and mysql_query() function calls (the code examples in the PHP manual under each of these functions shows one possible way of doing this using "if" tests of the returned value - most people prefer the "or die(...)" method) also gives you no indication of why it does not work and results in a blank page that leaves you wondering what is going on.

    Edit: Once you get the query working, I notice a logic problem. You are setting/getting the "id" from the form. This is used in WHERE id = '$id'. If you ever change this to a new value in the form, the query will not find any matching rows and the UPDATE won't change anything (or it will change some other row matching the newly entered value.) You probably don't want to allow the id to be changed on the form as this is the "key" you are using to insure which record is being updated. The $id should probably be passed to this code in a session variable so that it is not visible or alterable by anyone and you should probably not have the id be in the list of SET... values.

    You might want to look at using the mysql_affected_rows() function to check if the UPDATE changed any rows (note that if all the new values in the UPDATE are the same as the existing values, the UPDATE won't change anything and mysql_affected_rows() will be zero too) - http://php.net/mysql_affected_rows
    Last edited by CFMaBiSmAd; 07-16-2007 at 01:50 PM. Reason: additional info
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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