Jump to content

Recommended Posts

Hi, this script is the action of a form I have. I need it to match the gen_id that was submitted to that what is listed in the database. If it matches, I want it to allow the user to proceed and then show an error if it doesnt.

 

This is what I have got. Currently it just says cannot query. Any help is much appreciated.

 

<?php

 

# I have added these variables, as this is what will be submitted by the form.

$username = "paul123";

$gen_id = "JDWYK";

 

# This is the script

 

$db = "moveitho_sitebuilder";

 

$link = mysql_connect('localhost', 'moveitho_paul', 'test');

if ( ! $link ) {

$dberror = mysql_error();

return false;

}

if ( ! mysql_select_db( $db, $link ) ) {

$dberror = mysql_error();

return false;

}

$result = mysql_query("SELECT username, gen_id FROM users WHERE username='%s' AND gen_id='%s'");

if ($result == $username && $gen_id) {

print "PIN matched";

}

else {

    die('Could not query:' . mysql_error());

}

 

mysql_close($link);

?>

Link to comment
https://forums.phpfreaks.com/topic/146776-solved-sql-and-php-help/
Share on other sites

Your not actually using the posted variables in your query amongst other vaious errors.

 

<?php

if (isset($_POST['submit'])) {

  // collect values from form.
  $username = mysql_real_escape_string($_POST['username']);
  $gen_id = mysql_real_escape_string($_POST['gen_id']);

  # This is the script

  $db = "moveitho_sitebuilder";

  mysql_connect('localhost', 'moveitho_paul', 'test') or die(mysql_error());
  mysql_select_db( $db) or die(mysql_error());

  if ($result = mysql_query("SELECT username, gen_id FROM users WHERE username='$username' AND gen_id='$gen_id'")) {
    if (mysql_num_rows($result)) {
      echo "PIN matched";
    }
  }

}

?>

u have lot of things to take care there....

 

$result = mysql_query("SELECT username, gen_id FROM users WHERE username='%s' AND gen_id='%s'");
if ($result == $username && $gen_id) {

 

please read manual for

 

- how to fire mysql query

- how to fetch a row from database ( this is different than firing a query)

- if loop syntax.

Hi, thanks for your reply. I had to change the script slightly as it resulted in errors unless the database was connected to first.  The only thing I am having trouble with now is showing a 'PIN successful' or 'PIN error' message.

 

This is what I have got now:

 

if (isset($_POST['submit'])) {

 

  // collect values from form.

 

 

$link = mysql_connect('localhost', 'moveitho_paul', 'test');

if ( ! $link ) {

$dberror = mysql_error();

return false;

}

if ( ! mysql_select_db( $db, $link ) ) {

$dberror = mysql_error();

return false;

}

$username = mysql_real_escape_string($_POST['username']);

  $gen_id = mysql_real_escape_string($_POST['gen_id']);

 

  if ($result = mysql_query("SELECT username, gen_id FROM users WHERE username='$username' AND gen_id='$gen_id'")) {

    if (mysql_num_rows($result)) {

      echo "PIN matched";

    } else {

echo "PIN did not match";

  }

 

}

 

It either shows an error for an unexpected else, or just shows the PIN did not match. Does anyone know how to fix this?

 

Thanks

Can anyone help on this one? I have changed the code around and cant seem to get the right result from it. Here is what I have got now:

 

if (isset($_POST['submit'])) {

 

  // collect values from form.

 

 

$db = "moveitho_sitebuilder";

 

  mysql_connect('localhost', 'moveitho_paul', 'test') or die(mysql_error());

  mysql_select_db( $db) or die(mysql_error());

 

$username = mysql_real_escape_string($_POST['username']);

  $gen_id = mysql_real_escape_string($_POST['gen_id']);

 

  if ($result = mysql_query("SELECT username, gen_id FROM users WHERE username='$username' AND gen_id='$gen_id'")) {

    if ( ! mysql_query($result)) {

      echo "PIN matched";

    } else {

echo "PIN did not match";

  }

 

}

}

 

Help is much needed and very appreciated.

You are querying the result, which is probably throwing an error I hope.

 

if ($result = mysql_query("SELECT username, gen_id FROM users WHERE username='$username' AND gen_id='$gen_id'")) {
    if (mysql_num_rows($result) > 0) {
          echo "Pin Matched";
    }else {
          echo "Pin did not match.";
    }
}else{
    echo "SQL Error: " . mysql_error();
}

Hi premiso, thanks for your reply. I have now got the following code, but it just states that PIN did not match no matter what the entry is. Any idea on what is wrong?

 

if (isset($_POST['submit'])) {

   $db = "moveitho_sitebuilder";

  mysql_connect('localhost', 'moveitho_paul', 'test') or die(mysql_error());
  mysql_select_db( $db) or die(mysql_error());
  
   $username = mysql_real_escape_string($_POST['username']);
  $gen_id = mysql_real_escape_string($_POST['gen_id']);

  if ($result = mysql_query("SELECT username, gen_id FROM users WHERE username='$username' AND gen_id='$gen_id'")) {
    if (mysql_num_rows($result) > 0) {
          echo "Pin Matched";
    }else {
          echo "Pin did not match.";
    }
}else{
    echo "SQL Error: " . mysql_error();
}
}

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.