0

I'm assuming this is another noob mistake, I've got a form that for some reason has two copies of it, in the sense that there should be the one but there's an exact copy beneath. I've taken the PHP out to check that's what it is and it reverts back to just the one.

Thanks for any help, it's appreciated!

<?php 
    $con = mysql_connect( 'localhost', 'user', 'pass' );
    if( !$con ) {
        die( 'Could not connect: ' . mysql_error() );
    } else {
        user = $_SESSION['username'];
        mysql_select_db( 'db_name', $con );
        $result = mysql_query( "SELECT * FROM users WHERE username='$user'" );
        while( $row = mysql_fetch_array( $result ) ) {
?>
            <form> </form>
<?php
        }
    }
?>
Gwenc37
  • 2,064
  • 7
  • 18
  • 22
Honest Objections
  • 773
  • 1
  • 6
  • 13
  • 3
    [Please, stop using mysql_* functions](http://stackoverflow.com/q/12859942/1238019) in new code, they are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Instead of, have a look on [prepared statements](http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html), and use [Mysqli](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/en/book.pdo.php). – zessx Jul 18 '14 at 09:47
  • There are two users with username = $user ? Probably. – Clément Malet Jul 18 '14 at 09:48
  • It's inside a while loop,
    will print for every record in the dataset.
    – Jim Jul 18 '14 at 09:48
  • take it outside the loop – user1978142 Jul 18 '14 at 09:48

2 Answers2

1

Try this:

<?php 
$con = mysql_connect( 'localhost', 'user', 'pass' );
if( !$con ) {
    die( 'Could not connect: ' . mysql_error() );
} else {
    $user = $_SESSION['username'];
    mysql_select_db( 'db_name', $con );
    $result = mysql_query( "SELECT * FROM users WHERE username='$user'" );

    echo "<form>"; // open form tag outside of loop

    while( $row = mysql_fetch_array( $result ) ) {
?>


<?php
    }

    echo "</form>"; // close form tag when loop completes

}
?>
Jim
  • 576
  • 2
  • 11
0

Try using this

$result = mysql_query( "SELECT * FROM users WHERE username='$user' LIMIT 1" ); 

Probably you might have 2 users with same username.

Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • 1
    Please don't do this, you're hiding the problem, not fixing it. Create a unique constraint on the username field instead, that way this cannot happen in future. – scragar Jul 18 '14 at 09:52
  • 3
    @scragar I understand. and according to the best practice you are correct as well. But as we do not have the schema of the table and DB this is the best which suited to solve the question. But I appreciate your concern. Please try to take your downvote back – Mohit S Jul 18 '14 at 09:54
  • I've not downvoted anyone, your code correctly answers the question, and although I don't like the method that is not a legit reason to downvote(which is supposed to be reserved for answers that fail to solve the problem). – scragar Jul 18 '14 at 09:56
  • Thank you for all the help, I had manually added this user into the database, typically from the website it's set-up so you must have a unique username (probably why I hadn't considered it) so in this instance it would be the optimal fix(for what I need)! Thanks again! – Honest Objections Jul 18 '14 at 10:21