SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Forms and Php and SQL

    Hello.

    Let me explain what im doing since I dont know why the error is popping up...

    I want to rate a picture and using a submit button assert the data into my database. I want to do this without having it refresh the page. The submit button seems to give me the errors.




    I have an images file that displays a file based on what a user selects from the main page. Like this....

    $output = "<img width=\"400\" src=\"images1/";
    $output .= $file_name;
    $output .= "\">";
    echo $output;

    After the image is displayed I have a rate the pictures form. Like this.


    <form method="post" action="<?=$PHP_SELF?>">

    <select>
    <OPTION SELECTED VALUE="http://www.feistypics.com">Rate This Picture</OPTION>
    <OPTION VALUE="0">1 - Bad </OPTION>
    <OPTION VALUE="1">2 - Poor </OPTION>
    <OPTION VALUE="2">3 - Ok </OPTION>
    <OPTION VALUE="3">4 - Good </OPTION>
    <OPTION VALUE="4">5 - Excellent </OPTION>
    </select>

    <input type="submit" name="submitrate" value="Submit">
    </form>

    <?
    if ( $submitrate == Submit )
    {
    $count = mysql_query ("SELECT * FROM lookCount WHERE IDref = $id ");
    $newcount = (($count + 3) /5);
    mysql_query ("UPDATE lookCount set count = $newcount where IDref = $id ");
    ?>

    Now Im not entirely sure what the "<?=$PHP_SELF?>"> line does so I need clarification on that but. When someone clicks submit... I get 2 different errors in my code.

    http://www.feistypics.com Is the Main Page
    http://www.feistypics.com/timages.php?id=133
    Replace the "t" with "c" to see what Im working on i.e.
    http://www.feistypics.com/cimages.php?id=133

    If you try to submit using the cimages page it refreshes all wrong with errors. Im sure its simple solution but I havent found the answer.

    Pls Assist.

  2. #2
    SitePoint Evangelist =X='s Avatar
    Join Date
    Jun 2001
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    first of all I'd like to point out that you can write this a bit shorter
    PHP Code:
    $output "<img width=\"400\" src=\"images1/";
    $output .= $file_name;
    $output .= "\">";
    echo 
    $output
    to:
    PHP Code:
    print "<img width=\"400\" src=\"images1/$file_name\">"
    Now Im not entirely sure what the "<?=$PHP_SELF?>"> line does
    by pressing the submit button, the action="" will tell the server what page to submit this data to.

    so you could use "cimages.php" instead -> $PHP_SELF will give you the name of your current page

    I didn't see any errors after submitting, didn't receive anything like "your vote has been submitted" either, so I'm not sure if it worked or not :/
    PHP-Webservices - Profesional Hosting and Programming of sites.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for clarifying that stuff up.

    I know that the editings of the counter data isnt work.

    Also after you choose submit, didnt you see it load http://www.feistypics.com/cimages.php

    That page wont load without an ID being supplied to it.

    When i click submit it goes to this cimages.php page. I dont want it to leave this page or I want to be able to send it to the same page it is on.

  4. #4
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. Your <select> box doesn't have a name... it needs to be more like <select name="rate"> and then you will be able to use $rate in your script.

    2. You need to somehow pass the ID of the picture in the form, like this - <input type="hidden" name="id" value="XXX"> - you change XXX with whatever is right according to your script.

    3. This code:
    PHP Code:
    $count mysql_query ("SELECT * FROM lookCount WHERE IDref = $id "); 
    $newcount = (($count 3) /5); 
    You need to:
    a) mysql_fetch_array() the $count before you could use the information from that query.
    b) Use $count['count'] in your formula, not just $count.

    4. And last but not least, you need to quote Submit in the following line:
    PHP Code:
    if ( $submitrate == Submit 
    besides, I would use this instead (I don't know why, I just feel better with it ):
    PHP Code:
    if ( $submitrate != '' 
    Edit:
    One more thing... in the formula for calculating the new count:
    PHP Code:
    $newcount = (($count 3) /5); 
    shouldn't you use the value from the <select> box as well here?
    Last edited by FireFly; Jan 18, 2002 at 14:09.

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent points. oversites on my part definitely. Nice Job.

    Only part I am still sketchy about is how to pass the ID in the URL once I submit.

  6. #6
    SitePoint Evangelist =X='s Avatar
    Join Date
    Jun 2001
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    2 ways, either you do this by

    <form method="post" action="<?=$PHP_SELF?id=$id?>">

    or as firefly said, pass it on with a hidden field

    2. You need to somehow pass the ID of the picture in the form, like this - <input type="hidden" name="id" value="XXX"> - you change XXX with whatever is right according to your script.
    so <input type="hidden" name="id" value="$id">

    this will result in the same image being showed...
    PHP-Webservices - Profesional Hosting and Programming of sites.

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Made that change on the action="<?=$PHP_SELF?id=$id?>">

    now i get parse error.

  8. #8
    SitePoint Evangelist =X='s Avatar
    Join Date
    Jun 2001
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not on my own box now so I can't test what would work, but there are a few possibilities
    either

    make
    $url = $PHP_SELF . "?id=" . $id;
    or
    $url = "$PHP_SELF?id=$id";
    or
    $url = $PHP_SELF . "?id=$id";

    and then do action="<?= $url ?>">

    can't say by heart what would work, let me know
    PHP-Webservices - Profesional Hosting and Programming of sites.

  9. #9
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well those three all work for sending the correct URL but for some reason mySQL is fighting me.

    Bunch of parse errors.

    Weirdo... Gonna work on it a bit. Launch me an Email if you have an inspiration. gatorben@ufl.edu

  10. #10
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    line thirty where i get the error has this code

    $mysql_link = mysql_connect($dbhost,$dbuser,$dbpass);
    $column = mysql_list_fields($dbname,$dbtable,$mysql_link);
    $sql = "SELECT * FROM $dbtable where ID = $id";
    $result = mysql_db_query($dbname,$sql);
    while($value = mysql_fetch_array($result))
    {
    $desc = $value["description"];
    $file_name = $value["file_name"];

  11. #11
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by B1011011
    Made that change on the action="<?=$PHP_SELF?id=$id?>">

    now i get parse error.
    I think that the <?=$foo ?> syntax only works for variables.

    B1011011, what error exactly are you getting now? (and it would help to know what line 30 is )

  12. #12
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by B1011011
    Made that change on the action="<?=$PHP_SELF?id=$id?>">

    now i get parse error.
    If your not using PHP4 then that sytax for echoing a variable won't work.
    try that line as <form method="post" action="<?php echo $PHP_SELF?id=$id; ?>"> and see if it works.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  13. #13
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <form method="post" action="<?php echo $PHP_SELF.'?id='.$id; ?>">
    since "?id" is a string.

  14. #14
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And so isn't $PHP_SELF.
    You only need to put quotes around string variables when running queries, not to echo values.
    If you did, this wouldn't work
    PHP Code:
    $var "Hi!";
    echo 
    $hi
    Though i didn't for the quotes for the echo statement.
    PHP Code:
    <form method="post" action="<?php echo "$PHP_SELF?id=$id"?>">
    Last edited by Defender1; Jan 20, 2002 at 00:58.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  15. #15
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But you didn't put quotes around your echo statement:
    Originally posted by Defender1
    <form method="post" action="<?php echo $PHP_SELF?id=$id; ?>">

  16. #16
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That new line the post method doesnt work either.

    errors are

    Supplied argument is not a valid MySQL result resource in /home/fiestypics.com/www/cimages.php on line 30


    Supplied argument is not a valid MySQL result resource in /home/fiestypics.com/www/cimages.php on line 48

    Code is at that part just at above. It starts at line 30

  17. #17
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea yea, typo.

    and check to make sure you're actually connecting to your database.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*


Bookmarks

Posting Permissions

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