Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2007
    Location
    england
    Posts
    536
    Tokens
    0

    Default [PHP] What's wrong with this....

    I have the following code, whatit's supposed to do is display the data from the mySQL database and display in a text box, but it isn't.

    PHP Code:
    <?php
    include "config.php";
     
    $id $_GET["id"]; // Be sure to use a good clean function to do it with. If you have one uncomment the next line
    # $id = Clean($_GET["id"]);
     
    if($_GET["action"] == "edit") {
     
    $name $_POST["name"];
    # $field = clean($_POST["field"]);
     
    $grade $_POST["grade"];
     
    $comment $_POST["comment"];
     
    mysql_query("UPDATE petition SET name='$name', grade='$grade', comment='$comment' WHERE id='$id'") or die(mysql_error());
     
    echo 
    "Record updated!";
     
    exit;
    }
     
    $get mysql_query("SELECT * FROM table WHERE id='$id' LIMIT 0,1");
    $row mysql_fetch_array($get);
     
    ?>
    <form action="?action=edit" method="post">
    Name: <input name="name" type="text" value="<?php 
    $get_current 
    mysql_fetch_array(mysql_query("select name from petition where id = '$id'"));
    echo(
    '$get_current["name"]');
    ?>"/><br />
    Grade: <input name="grade" type="text" value="<?php 
    $get_current 
    mysql_fetch_array(mysql_query("select grade from petition where id = '$id'"));
    echo(
    '$get_current["grade"]');
    ?>"/><br />
    Comment:<br />
    <input name="comment" type="text" value="<?php 
    $get_current 
    mysql_fetch_array(mysql_query("select comment from petition where id = '$id'"));
    echo(
    '$get_current["comment"]');
    ?>"/><br />
    <input type="submit" value="Edit Comment!" />
     
    </form>
    Moved by Agesilaus (Forum Moderator) from Design and Development: Please post in the correct forum in future.
    Last edited by Agesilaus; 14-12-2007 at 12:33 PM.


    Selling DJ/Habbo layout, more info here.


  2. #2
    Join Date
    Apr 2007
    Location
    england
    Posts
    536
    Tokens
    0

    Default

    Please merge.

    Heres an image of what that code looks like on a webpage:


    Selling DJ/Habbo layout, more info here.


  3. #3
    Join Date
    Sep 2006
    Location
    Hobart, Australia
    Posts
    593
    Tokens
    0

    Default

    Lets take a look at the actual form:

    PHP Code:
    <form action="?action=edit" method="post">
    Name: <input name="name" type="text" value="<?php 
    $get_current 
    mysql_fetch_array(mysql_query("select name from petition where id = '$id'"));
    echo(
    '$get_current["name"]');
    ?>"/><br />
    Grade: <input name="grade" type="text" value="<?php 
    $get_current 
    mysql_fetch_array(mysql_query("select grade from petition where id = '$id'"));
    echo(
    '$get_current["grade"]');
    ?>"/><br />
    Comment:<br />
    <input name="comment" type="text" value="<?php 
    $get_current 
    mysql_fetch_array(mysql_query("select comment from petition where id = '$id'"));
    echo(
    '$get_current["comment"]');
    ?>"/><br />
    <input type="submit" value="Edit Comment!" />
     
    </form>
    We have three identical queries. Kind of pointless, considering we can do it all from one query. Much better to go something like:


    PHP Code:
    <?PHP

    $query 
    mysql_query("SELECT * FROM petition WHERE id='$id'");

    $get_current mysql_fetch_assoc($query);

    ?>

    <form action="?action=edit" method="post">
    Name: <input name="name" type="text" value="<?php echo $get_current["name"]; ?>"/><br />
    Grade: <input name="grade" type="text" value="<?php echo $get_current["grade"]; ?>"/><br />
    Comment:<br />
    <input name="comment" type="text" value="<?php echo $get_current["comment"]; ?>"/><br />
    <input type="submit" value="Edit Comment!" />
     
    </form>
    You don't need quotation marks when echoing variables. You CAN use double quotes ("), however single quotes (') will just echo the variable in a literal sense.

    ie.

    PHP Code:

    $var 
    'lol';

    echo 
    $var// Will echo 'lol'

    echo "$var"// Will echo 'lol'

    echo '$var'// Will echo '$var' 
    Going back to your script, this line looks a little weird:

    PHP Code:
    $get mysql_query("SELECT * FROM table WHERE id='$id' LIMIT 0,1"); 
    Does the table 'table' actually exist, or was it just a dummy text from an example?

  4. #4
    Join Date
    Apr 2007
    Location
    england
    Posts
    536
    Tokens
    0

    Default

    Okay, thanks. Now, the only problem is the fact it doesn't update the name, grade and comment, nor does it fetch the old name, grade and comment and put them in the text box.

    Heres the code:
    PHP Code:
    <?php
    include "config.php";
     
    $id $_GET["id"]; // Be sure to use a good clean function to do it with. If you have one uncomment the next line
    # $id = Clean($_GET["id"]);
     
    if($_GET["action"] == "edit") {
     
    $name $_POST["name"];
    # $field = clean($_POST["field"]);

    $grade $_POST["grade"];

    $comment $_POST["comment"];

    $query mysql_query("SELECT * FROM petition WHERE id='$id'");

    $get_current mysql_fetch_assoc($query);

    mysql_query("UPDATE petition SET name='$name', grade='$grade', comment='$comment' WHERE id='$id'") or die(mysql_error());

    echo 
    "Record updated!";
     
    exit;
    }

    ?>

    <form action="?action=edit" method="post">
    Name: <input name="name" type="text" value="<?php echo $get_current["name"]; ?>"/><br />
    Grade: <input name="grade" type="text" value="<?php echo $get_current["grade"]; ?>"/><br />
    Comment:<br />
    <input name="comment" type="text" value="<?php echo $get_current["comment"]; ?>"/><br />
    <input type="submit" value="Edit Comment!" />
     
    </form>


    Selling DJ/Habbo layout, more info here.


  5. #5
    Join Date
    Apr 2007
    Location
    england
    Posts
    536
    Tokens
    0

    Default

    Bump. ^^

    Edited by Agesilaus (Forum Moderator): Please do not bump threads, wait for a reply instead.
    Last edited by Agesilaus; 13-12-2007 at 09:01 PM.


    Selling DJ/Habbo layout, more info here.


  6. #6
    Join Date
    May 2005
    Location
    San Francisco, CA
    Posts
    7,160
    Tokens
    2,331

    Latest Awards:

    Default

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    include "config.php";
     
    $id $_GET["id"]; // Be sure to use a good clean function to do it with. If you have one uncomment the next line
    # $id = Clean($_GET["id"]);

    $query mysql_query("SELECT * FROM `petition` WHERE `id` = '".$id."'") or die("Error: ".mysql_error());

    $get_current mysql_fetch_assoc($query) or die("Error: ".mysql_error());
     
    if(
    $_POST["action"] == "edit") {
     
    $name $_POST["name"];
    # $field = clean($_POST["field"]);

    $grade $_POST["grade"];

    $comment $_POST["comment"];

    $handle mysql_query("UPDATE `petition` SET `name`= '".$name."', `grade` = '".$grade."', `comment` = '".$comment."' WHERE `id` = '".$id."'") or die(mysql_error());

    echo 
    "Record updated!";
     
    exit;
    }

    ?>

    <form action="" method="post">
    <input type="hidden" name="action" value="edit" />
    Name: <input name="name" type="text" value="<?php echo $get_current["name"]; ?>"/><br />
    Grade: <input name="grade" type="text" value="<?php echo $get_current["grade"]; ?>"/><br />
    Comment:<br />
    <input name="comment" type="text" value="<?php echo $get_current["comment"]; ?>"/><br />
    <input type="submit" value="Edit Comment!" />
     
    </form>
    </body>
    </html>
    Last edited by Invent; 13-12-2007 at 09:11 PM.

Posting Permissions

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