Jump to content

Recommended Posts

Here is the snipped of code I'm using:

if (!isset($_POST['Submit']))
    form();
else
if (!blank_field())
      echo "fields left blank";
    else
    if (checklength($_POST[password], 5, 15))
      echo "password length incorrect";

if (connect_to_db()) {
    $query = "select username from accounts where username='[username]'";

    $result = $mysql_query($query, $link);
    if ($result) // true = user already exists
        echo "User already exists";
    else
        echo "You are a new user";
}

The blank fields and wrong password length are working fine. I am using mysql 5.0.27, php 5.2.0, and apache 2.2.3, if that matters.

I created the database and added in 2 users using the command line. I have a form that asks for a username and password, then this part of the code is supposed to check the form username with the mysql username for duplicates.

I don't get any errors, even though I am typing in the same name, which should result in "User already exists". All it does is lets me enter the information, and keeps going.

Any pointers? Do I need to post the entire php file?
Link to comment
https://forums.phpfreaks.com/topic/29087-problem-with-mysql_query/
Share on other sites

Ok I did that. Now my code looks like this:

if (connect_to_db()) {
    $query = "select username from accounts where username='{$_POST['username']}'";
    $result = $mysql_query($query, $link) or die(mysql_error());
      echo "$result";
//    if ($result) // true = user already exists
//        echo "User already exists";
//    else
//        echo "You are a new user";
}

I'll remove the comments once I know it's working. Anyway, there are no errors, the page goes blank.

I get no results at all. I keep looking over google for examples like this, but can't find anything. What I'm trying to do is have the form pass a username to php, which calls mysql, does a search for the username, and says it is exists. For the life of me, I cannot find any tutorials online where someone else has done this. I can't imagine I'm the first.
OK, here's a complete code, assuming that the fields in your form are called username and password and you're using the post method.

[code]<?php
// Have the username and password been filled out
if (!empty($_POST['username']) && !empty($_POST['password'])){
  // Query the database
  $sql = "SELECT username FROM users WHERE username = '{$_POST['username']}'";

  // Check if we got a result returned
  $result = mysql_query($sql);
  if (!$result){
      echo "Couldn't execute: $sql<br><br>\n" . mysql_error();
  }
  else {
      // Does the user exist, how many rows were retrurned
      if (mysql_num_rows($result) > 0){
        echo "Username already in use\n";
      }
      else {
        echo "Username doesn't exist\n";
      }
  }
}
else {
  // Your code to echo the form goes here
}
?>[/code]

Regards
Huggie
Huggie... I replaced my code with yours. Yours is much cleaner. However, I am still not getting any database feedback. I type in my username on the form, which I manually added to mysql on my linux server, but after I click submit, I get nothing. No errors, not even the echo statement saying if the username exists.

Everything is running fine on my linux machine. I just checked it. I can't figure this out.

Here is the script:

<?php

// Have the username and password been filled out

function form(){
        echo "<table width=\"100%\" height=\"5%\" bgcolor=\"cyan\">";
        echo "<tr>";
        echo "<td bgcolor=\"white\" width=\"20%\"><font color=\"green\">&nbsp;&nbsp;&nbsp;<font size=+1><b>Step 1</b></font></td>";
        echo "<td>&nbsp;&nbsp;&nbsp;<font size=+1 color=\"white\"><b>Step 2</b></font></td>";
        echo "<td>&nbsp;&nbsp;&nbsp;<font size=+1 color=\"white\"><b>Step 3</b></font></td>";
        echo "<td>&nbsp;&nbsp;&nbsp;<font size=+1 color=\"white\"><b>Step 4</b></font></td>";
        echo "<td>&nbsp;&nbsp;&nbsp;<font size=+1 color=\"white\"><b>Step 5</b></font></td>";
        echo "</tr>";
        echo "<tr bgcolor=\"white\">";
        echo "<td bgcolor=\"red\" width=\"20%\">&nbsp;&nbsp;&nbsp;<b><i><font size=+1>LOG IN</i></b></td>";
        echo "<td></td>";
        echo "<td></td>";
        echo "<td></td>";
        echo "<td></td>";
        echo "</tr>";
        echo "</table>";

        echo "<br><br><br><br><br><br><br><br><br><center>";
        echo "<table border=\"0\" bgcolor=\"#66ccff\" cellpadding=\"10\">";
        echo "<form action='" .$_SERVER['PHP_SELF'] ."' method='post'>";
        echo "<tr><td>What username do you want?</td><td><input type='text' name='username'></td>";
        echo "<tr><td>Pick a password (5-15 characters):</td><td><input type='password' name='password'></td>";
        echo "<tr><td colspan=2 align=center><br><input type='image' name='Submit' value='Submit' src='submit.gif'>";
        echo "</form>";
        echo "</table></center>";
}
       
function connect_to_db() {
    $link = mysql_connect('localhost', 'user', 'password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
        }

    $db_selected = mysql_select_db('mydb', $link) or die(mysql_error());
    if (!$db_selected) {
        echo "Cannot use database<br>";
        echo "mysql_error()";
        }
}

function blank_field(){
    while(list($name,$val)=each($_POST)){
        if (!$val)
            return FALSE; // if fields are blank
    }
    return TRUE;
}

if (!isset($_POST['Submit']))
    form();
else

if (connect_to_db()) {

if (!empty($_POST['username']) && !empty($_POST['password'])){
  // Query the database
  $sql = "SELECT username FROM users WHERE username = '{$_POST['username']}'";

  // Check if we got a result returned
  $result = mysql_query($sql);
  if (!$result){
      echo "Couldn't execute: $sql<br><br>\n" . mysql_error();
  }
  else {
      // Does the user exist, how many rows were retrurned
      if (mysql_num_rows($result) > 0){
        echo "Username already in use\n";
      }
      else {
        echo "Username doesn't exist\n";
      }
      echo "here is the form";
      } // end else
} // end if
} // end while
?>

What could I possibly be missing? I at least expected it to say if the username exists, but it doesn't.
Ok I got it working. Even the error reporting thing didn't help. What it turned out to be is in the connect_to_db function, after the part where it dies if it fails, I needed an "else" there, and at the end of the function, it wasn't returning a value, which apparently the main loop was expecting. I added the else, return TRUE, and now it works.

Thank you!
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.