Jump to content

POST .. GET


mrlankee

Recommended Posts

Hi,

 

I'm trying to figure this out.. I am creating a user search on my website thats connected to mysql.  What I have does not work :\

<form action="useraccount.php" method="post">
<input type="text" name="username"/>
<input type="submit" value="submit"/>
</form>
<?php

$username = $_POST['nome'];
$pdo = new PDO('mysql:host=myhost;dbname=mydb', 'user', 'pw');

if ($pdo->query("SELECT * from TABLENAME where username='".$username."'")->fetchColumn() > 0){
foreach($pdo->query("SELECT * from TABLENAME where username='".$username."'") as $row) {
echo $row['Player_Name'] . $row['Time_Online'];
}
$pdo = null;
}
else{
echo "Sorry, that username does not exist in our database.";
}
?>
Link to comment
Share on other sites

www.rpproject.net/useraccount.php 

As you can see.. no matter what you enter.. the "else" does not show up as "Sorry, that username does not exist in our database.";"

 

I cannot figure out whats going on.. exact code.

<form action="account.php" method="GET">
In-Game Name:<br>
<input type="text" name="username" value="Username">
<br>
<input type="submit" value="Submit">
</form>
       <?php

$username = $_POST['username'];
$pdo = new PDO('mysql:host=xxxxx;dbname=xxxx', 'xxxx', 'xxxx');


if ($pdo->query("SELECT * from TABLENAME where username='".$username."'")->fetchColumn() > 0){
foreach($pdo->query("SELECT * from TABLENAME where username='".$username."'") as $row) {
echo $row['Player_Name'] . $row['Time_Online'];
}
$pdo = null;
}
else{
echo "Sorry, that username does not exist in our database.";
}
?>

Any ideas?

Link to comment
Share on other sites

 

 

Sorry what i ment is .. If i enter username Alpha instead of Bravo.. it'll show Alpha's row's

There is a problem in your logic. If you enter any value in the form that exists in the database - it's going to return that row's value because you asked it to with this select statement.

SELECT * from TABLENAME where username='".$username."'"
Edited by hansford
Link to comment
Share on other sites

Try:

$username = $_POST['username'];
$pdo = new PDO('mysql:host=xxxxx;dbname=xxxx', 'xxxx', 'xxxx');
$sql = "SELECT COUNT(*) from TABLENAME where username='".$username."'";

if ($result = $pdo->query($sql)
{
    if ($result->fetchcolumn() > 0)
    {
        $sql = "SELECT * from TABLENAME where username='".$username."'";
        
        foreach ($pdo->query($sql) as $row)
        {
            echo $row['Player_Name'] . ' ' . $row['Time_Online'];
        }
    }
}
else 
{
    echo "Sorry, that username does not exist in our database.";
}

Link to comment
Share on other sites

Why do a fetchcolumn at all? Check the result of the operation, don't waste time on a retrieval of one piece of data. Of course if your db does not support rowcount() then stick with what you have, but here is how I would write your code:

 

<form action="useraccount.php" method="post">
<input type="text" name="username"/>
<input type="submit" value="submit"/>
</form>
<?php
$username = $_POST['username'];  // you retrieve form data using the html's name= attribute
$pdo = new PDO('mysql:host=myhost;dbname=mydb', 'user', 'pw');

// assign your query statement to a var so you can view it if you need to debug it
$q = "SELECT * from TABLENAME where username='$username'";

if (!$pdo->query($q)
{
   echo "Query failed - query is $q";// only do this during development - not in prod.
   exit();
}
if ($pdo->rowcount()== 0)
{
   echo "Sorry, that username does not exist in our database.";
   exit();
}
// now get your results
while ($row = $pdo->fetch_assoc(PDO::FETCH_ASSOC))
{
   echo $row['Player_Name']," ",$row['Time_Online'];
}
exit();
Link to comment
Share on other sites

ginerjm - posted the preferred way to code this - smooth, elegant - with error handling. Just test that rowCount() works on the particular DB you're using. I use rowCount(), but because of the manual "warning", I am in the habit of using fetchColumnt() - however, it is an extra query and slows things down and usually unnecessary.

Link to comment
Share on other sites

I switched up the code

 

still getting the error

 

Parse error: syntax error, unexpected '{' in /home/xxxxxxxxxxxxxxx/xxxxx/account.php on line 100

 

 

 

9100.png

 

 

I did add a ?> to close out the PHP after the image was taken.

Edited by mrlankee
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.