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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Coder
    Join Date
    Sep 2009
    Location
    Long Island NY
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy Help with "Invalid ID"

    Hi Guys! I'm new here. I have a website micomunidad.com and I have a section called "answers". I can post questions but when answering them I get "Invalid ID".

    I'm learning PHP but this is a little to advance for me.

    This is what it shows at the view_question.php file:

    <?php
    include("includes/top_new.php");
    include("classes/answers.class.php");
    $answers = new answers;
    $question_id = $_GET["question_id"];
    if($question_id == Null || !is_numeric($question_id))
    {
    exit("Invalid ID");
    }
    $question_set = $answers->get_question($question_id);
    ?>
    <!-- Top.php -->
    <html>

    I would like to know what is causing it! I check the dB and it looks fine.

    At the answer.class.php it shows this:

    function add_answer($member_id,$question_id,$answer,$subject,$rating)
    {
    $time = strtotime("now");
    $date = date("d/m/Y g:h:s");

    $sql="insert into answers_answers";
    $sql.="(member_id";
    $sql.=", question_id";
    $sql.=", answer_text";
    $sql.=", answer_subject";
    $sql.=", rating";
    $sql.=", added_date";
    $sql.=", added_time)";

    $sql.=" values($member_id";
    $sql.=", $question_id";
    $sql.=", '$answer'";
    $sql.=", '$subject'";
    $sql.=", $rating";
    $sql.=", '$date'";
    $sql.=", '$time')";

    $res = mysql_query($sql);
    if($res)
    {
    return 1;
    }
    else
    {
    return 0;
    }

    Thank you for your time!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Can you put this before the if statement and post the result:
    PHP Code:
    printf("Request: %s, Querystring: %s\n"$_SERVER['REQUEST_METHOD'], $_SERVER['QUERY_STRING']); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    The code you have posted could output "Invalid ID" here:
    PHP Code:
    $question_id $_GET["question_id"];
    if(
    $question_id == Null || !is_numeric($question_id))
    {
    exit(
    "Invalid ID");

    It is not output by the code which works with the DB. It is output if question_id is not present in the URL query string or question_id is not numeric. What I mean your URL's must always look like
    Code:
    http://micomunidad.com/view_question.php?question_id=5
    (I used "5" but it could be any appropriate number).

    If question_id is not set i.e. the URL looks like
    Code:
    http://micomunidad.com/view_question.php
    or question_id is not numeric i.e. the URL looks like
    Code:
    http://micomunidad.com/view_question.php?question_id=foo
    you would get "Invalid ID" output by your code.

    The code you have posted does not allow to say with which GET parameters view_question.php is called. I am afraid we would need to see more code to understand why question_id is absent or is not numeric.

  • Users who have thanked SKDevelopment for this post:

    ElMaitron (09-19-2009)

  • #4
    New Coder
    Join Date
    Sep 2009
    Location
    Long Island NY
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy

    Thank you SKDevelopment

    My URLs always look like (this is a question ID sample):

    http://www.micomunidad.com/view_ques...question_id=16 but when trying to answer I get "Invalid ID"

    I have the folder with all files. If anybody is willing to help I would give them the MOD so we can solve this issue. It cost me over $100.00 but, inthe name of learning! I really want to learn!!!
    Last edited by ElMaitron; 09-19-2009 at 10:35 PM.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by Fou-Lu View Post
    Can you put this before the if statement and post the result:
    PHP Code:
    printf("Request: %s, Querystring: %s\n"$_SERVER['REQUEST_METHOD'], $_SERVER['QUERY_STRING']); 
    What is the output from the above?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    I have looked at your site. When the button "Submit Your Answer", the JavaScript function add_answer() sends an AJAX request to the PHP script ajax_scripts/add_answer.php.

    The AJAX request to ajax_scripts/add_answer.php is sent by POST, not by GET.

    I'll try to guess, but I could be wrong (since I did not see the code of add_answer.php): if add_answer.php contains similar checking code like view_question.php, I mean
    PHP Code:
    $question_id $_GET["question_id"];
    if(
    $question_id == Null || !is_numeric($question_id))
    {
    exit(
    "Invalid ID");

    then "Invalid ID" would be certainly returned. This is because in add_answer.php it is necessary to get question_id from $_POST, not from $_GET.

    Still this is just a guess. I have not seen the code of add_answer.php, so I could not say for sure. Could you post the code of the file

    ajax_scripts/add_answer.php

    Just in case: Of course if the code contains logins, passwords or any other sensitive data, they must be replaced e.g. with "***" before posting.

    2 Fou-Lu: I am sorry to say that, but if answer.class.php is invoked in ajax_scripts/add_answer.php (which would be logical - this is where the answer is saved...), then I would bet the answer to your question would be
    Code:
    Request: POST, Querystring:
    (since ajax_scripts/add_answer.php is called by POST which follows from the the JavaScript code at the posted hyperlink). This is just a guess though ... I hope ElMaitron would say if I was wrong about it ...
    Last edited by SKDevelopment; 09-20-2009 at 12:32 PM.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by SKDevelopment View Post
    I have looked at your site. When the button "Submit Your Answer", the JavaScript function add_answer() sends an AJAX request to the PHP script ajax_scripts/add_answer.php.

    The AJAX request to ajax_scripts/add_answer.php is sent by POST, not by GET.

    I'll try to guess, but I could be wrong (since I did not see the code of add_answer.php): if add_answer.php contains similar checking code like view_question.php, I mean
    PHP Code:
    $question_id $_GET["question_id"];
    if(
    $question_id == Null || !is_numeric($question_id))
    {
    exit(
    "Invalid ID");

    then "Invalid ID" would be certainly returned. This is because in add_answer.php it is necessary to get question_id from $_POST, not from $_GET.

    Still this is just a guess. I have not seen the code of add_answer.php, so I could not say for sure. Could you post the code of the file

    ajax_scripts/add_answer.php

    Just in case: Of course if the code contains logins, passwords or any other sensitive data, they must be replaced e.g. with "***" before posting.

    2 Fou-Lu: I am sorry to say that, but if answer.class.php is invoked in ajax_scripts/add_answer.php (which would be logical - this is where the answer is saved...), then I would bet the answer to your question would be
    Code:
    Request: POST, Querystring:
    (since ajax_scripts/add_answer.php is called by POST which follows from the the JavaScript code at the posted hyperlink). This is just a guess though ... I hope ElMaitron would say if I was wrong about it ...
    We'll need confirmation, but that was what I was expecting as output. Its simply the wrong request method.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #8
    New Coder
    Join Date
    Sep 2009
    Location
    Long Island NY
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    What is the output from the above?
    I did but I did not see anything!

  • #9
    New Coder
    Join Date
    Sep 2009
    Location
    Long Island NY
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by SKDevelopment View Post
    I have looked at your site. When the button "Submit Your Answer", the JavaScript function add_answer() sends an AJAX request to the PHP script ajax_scripts/add_answer.php.

    The AJAX request to ajax_scripts/add_answer.php is sent by POST, not by GET.

    I'll try to guess, but I could be wrong (since I did not see the code of add_answer.php): if add_answer.php contains similar checking code like view_question.php, I mean
    PHP Code:
    $question_id $_GET["question_id"];
    if(
    $question_id == Null || !is_numeric($question_id))
    {
    exit(
    "Invalid ID");

    then "Invalid ID" would be certainly returned. This is because in add_answer.php it is necessary to get question_id from $_POST, not from $_GET.

    Still this is just a guess. I have not seen the code of add_answer.php, so I could not say for sure. Could you post the code of the file

    ajax_scripts/add_answer.php

    Just in case: Of course if the code contains logins, passwords or any other sensitive data, they must be replaced e.g. with "***" before posting.

    2 Fou-Lu: I am sorry to say that, but if answer.class.php is invoked in ajax_scripts/add_answer.php (which would be logical - this is where the answer is saved...), then I would bet the answer to your question would be
    Code:
    Request: POST, Querystring:
    (since ajax_scripts/add_answer.php is called by POST which follows from the the JavaScript code at the posted hyperlink). This is just a guess though ... I hope ElMaitron would say if I was wrong about it ...
    This is the code found at ajax_scripts/add_answer.php:

    <?php
    session_start();
    header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // disable IE caching
    header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
    header( "Cache-Control: no-cache, must-revalidate" );
    header( "Pragma: no-cache" );
    include("../includes/db-conn.php");

    include("../classes/answers.class.php");
    $answers = new answers;

    $member_id = $_SESSION["member_id"];
    $question_id = addslashes($_POST["question_id"]);
    $rating = addslashes($_POST["rating"]);
    if($question_id == Null || !is_numeric($question_id) || $member_id == Null || !is_numeric($member_id) || !is_numeric($rating))
    {
    exit("Invalid ID");
    }

    $subject = addslashes($_POST["subject"]);
    $answer = addslashes($_POST["answer"]);

    // add comment
    if($answer == Null || $member_id == Null)
    {
    print "Invalid entry, Answer was not added, please try again.";
    }
    else
    {
    $res = $answers->add_answer($member_id,$question_id,$subject,$answer,$rating);
    if($res==1)
    {
    print "Answer posted succesfully";
    }
    else
    {
    print "A mysql error occured and answer was not added at this time, please try again later!";
    }
    }
    ?>

  • #10
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Try to replace
    PHP Code:
    exit("Invalid ID"); 
    in ajax_scripts/add_answer.php with:
    PHP Code:
    exit("Invalid ID: question_id=$question_id; rating=$rating; member_id=$member_id"); 
    and please show the result here. Then please reverse the change.

    Maybe $_SESSION["member_id"] is not set or is not numeric ?

  • #11
    New Coder
    Join Date
    Sep 2009
    Location
    Long Island NY
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by SKDevelopment View Post
    Try to replace
    PHP Code:
    exit("Invalid ID"); 
    in ajax_scripts/add_answer.php with:
    PHP Code:
    exit("Invalid ID: question_id=$question_id; rating=$rating; member_id=$member_id"); 
    and please show the result here. Then please reverse the change.

    Maybe $_SESSION["member_id"] is not set or is not numeric ?
    I get Invalid ID: question_id=15, rating=undefined, member_id=171

    Please check out attached image



    Thankx!
    Last edited by ElMaitron; 09-21-2009 at 07:56 PM.

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Rating is indicated as undefined, making it null. !is_numeric($rating) when $rating is null returns false, causing you to drop into the exit. That can be resolved with replacing this:
    PHP Code:
    $question_id addslashes($_POST["question_id"]);
    $rating addslashes($_POST["rating"]); 
    with this:
    PHP Code:
    $question_id = isset($_POST['question_id']) ? (int)$_POST['question_id'] : 0;
    $rating = isset($_POST['rating']) ? (int)$_POST['rating'] : 0
    Note that since you're treating these as integer values, you're safer casting to an integer than trying to addslashes to the text. If the text doesn't pass an atoi than it will return 0.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #13
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    For me
    Code:
    var rating = document.getElementById("rating").value;
    in the function add_answer() works in FF but does not work in IE8.

    So your code should work in FF, but not in IE with which you are testing according to the image.

    The problem is taking place despite the value for rating is always selected, the IE does not get it from document.getElementById("rating").value. And I could not get it (even with different JS) till I changed the id for select from rating to rating1. This is strange since you have 2 elements with name "rating" - select element and meta-tag, but only 1 element with id "rating".

    Still after I changed
    Code:
    <select id="rating" name='rating' size='1'>
    to
    Code:
    <select id="rating1" name='rating' size='1'>
    and used the function add_answer() with the following code:
    Code:
    function add_answer(question_id)
    {
    	var subject = document.getElementById("subject").value;
    	var answer = document.getElementById("answer").value;
    	var el = document.getElementById("rating1");
    	var rating1 = el.options[el.selectedIndex].value;
     
    	if(answer == '')
    	{
    		alert("You did not enter any answer, please enter one to continue");
    	}
    	else
    	{
         	var url="ajax_scripts/add_answer.php";
    
         	var params="question_id=" + escape(question_id) + "&subject=" + escape(subject) + "&answer=" + escape(answer) + "&rating=" + escape(rating1);
         	http.abort();
         	http.open("POST", url);
         	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
         	http.setRequestHeader("Content-length", params.length);
         	http.setRequestHeader("Connection", "close");
         	http.send(params);
         	http.onreadystatechange=response_add_answer;
    	}
    }
    the code seemed to work in both browsers ...

    Could you check if such a modification would work for you ?

    Edit1: corrected typo with id="rating" and id="rating1" - please use the current code.

    Edit2: 2 Fou-Lu: Your code is good of course! But "undefined" is clearly produced at the client side. If the OP does only the server side correction, the script will work for him. But if the JavaScript works not correctly in IE, the ratings would be always saved with zero values in IE, since they would be submitted as "undefined" and converted to 0. So your code is good, but the client-side correction is necessary anyway.
    Last edited by SKDevelopment; 09-21-2009 at 09:05 PM. Reason: typo with id="rating" and id="rating1" - please use the current code

  • #14
    New Coder
    Join Date
    Sep 2009
    Location
    Long Island NY
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    SKDevelopment ,
    I did the changes you made. When someone answers a question, it says: "Answer posted successfully". See image below:



    And when I view the question, I see that someone has posted an answer but it won't say the answer. See image below:



    Also, when I clicked "Top Answered" and click the question under Posted answers, it tells me "No Answers have been posted for this question yet!
    " see image below:

    [IMG][/IMG]

    This is great! Thank you guys!

  • #15
    New Coder
    Join Date
    Sep 2009
    Location
    Long Island NY
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    So what's next? Can you guys tell me?


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

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