Jump to content

Recommended Posts

hi all

on my page i have a text input form , what i am after is how to input the word from the form into the right colom of the user table for the word that was input and if they type a word that is not in the lis of alowable word get an error message saying word not found !

 

my sudo code is

 

if

text field1

 

  = "word1"

    then enter "word1" into database table 'user' - 'colom word1' where user is 'loged on user'

 

elseif

  = "word2"

    then enter "word2" into database table 'user' - 'colom word2' where user is 'loged on user'

 

elseif

  = "word3"

    then enter "word3" into database table 'user' - 'colom word3' where user is 'loged on user'

 

else

fail

echo "no word matches keep looking"

 

end

 

 

using old php4 and mysql4.1 ( or somthing like that "phpdev" prog ) for testing on home computer with Apache

ps total newbie with php/mysql

ok well thats all good but how would i impliment it ??

please explain and or show a example,

remember im new to this.

 

 

this is as far as i have got

 

<?php require_once('Connections/conn.php'); ?>
<?php
$uuser = $_SESSION['MM_Username'];
$input = $_POST['word']



if ($input == "hello") {$SQL = "UPDATE phpbb_user SET gword1='$input' WHERE user='MM_Username'";
}
elseif ($input == "choice") {$SQL = "UPDATE phpbb_user SET gword2='$input' WHERE user='MM_Username'";
}
elseif ($input == "hatter") {$SQL = "UPDATE phpbb_user SET gword3='$input' WHERE user='MM_Username'";
}
elseif ($input == "disk") {$SQL = "UPDATE phpbb_user SET gword4='$input' WHERE user='MM_Username'";
}
else 
echo 'word not reconised, try again'
?>


<form action="" method="post"><input name="word" type="text"><input name="" type="button"></form>

 

 

so how would i transfer that into an array

and where you said white did you mean  while ??

 

sorry been looking at code and web pages for last 3 days trying to get this ! and have had many attempts

 

Or if your options remain small, reduced overhead:

 

switch($input) {

  case 'hello':
    $SQL = ...
    break;

  case 'choice':
    $SQL = ...
    break;
    
  case 'hatter':
    $SQL = ...
    break;
    
  case 'disk':
    $SQL = ...
    break;
    
  default:
    echo "Word not recognized, try again.";
    break;

}

 

Or if your options remain small, reduced overhead:

 

switch($input) {

  case 'hello':
    $SQL = ...
    break;

  case 'choice':
    $SQL = ...
    break;
    
  case 'hatter':
    $SQL = ...
    break;
    
  case 'disk':
    $SQL = ...
    break;
    
  default:
    echo "Word not recognized, try again.";
    break;

}

 

 

I like that best.

if(isset($_POST['submit']))
{
   $input = $_POST['input'];
   $whitelist = array("word1", "word2", "word3");
   if (in_array($input, $whitelist)) {
      $sql = "UPDATE phpbb_user SET gword2='$input' WHERE user='MM_Username'";
  echo "UPDATE Successful 
Query: $sql";
   } else {
      echo "$input is invalid...";
   }
}
?>

</pre>
<form action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="POST">


<

 

Notes:

- Use mysql_real_escape_string to santize the string (prevent SQL Injections).

- You may want to keep everything lowercase so there's no case sensitivity issues.  Just keep everything lower case in the array and use strtolower() on the input.

 

ok thanks heaps

ill give that a try and see how i go

have got 20 words to check so which would be better the switch or the array ??

 

 

The array, unless you have a special case for each word but it seems like you just want to check to see if the input is in the list, and if it is, use it in the search.  If that's the case, the array is better.

ok thanks heaps

ill give that a try and see how i go

have got 20 words to check so which would be better the switch or the array ??

 

 

The array, unless you have a special case for each word but it seems like you just want to check to see if the input is in the list, and if it is, use it in the search.  If that's the case, the array is better.

 

 

each word is for a corresponding column in the database , the words from the column  are displayed in another page so as to let the user know what words they have so far !

 

anyways this gives me a better understanding and ill research some more as well

each word is for a corresponding column in the database , the words from the column  are displayed in another page so as to let the user know what words they have so far !

 

Yeah sorry, didn't really notice that.

 

Here:

 

if(isset($_POST['submit']))
{
   $input = $_POST['input'];
   $whitelist = array(1=>'word1', 2=>'word2', 3=>'word3');
   if (in_array($input, $whitelist)) {
      $sql = "UPDATE phpbb_user SET gword" . array_search($input, $whitelist) . "='$input' WHERE user='MM_Username'";
     echo "UPDATE Successful 
Query: $sql";
   } else {
      echo "$input is invalid...";
   }
}
?>

</pre>
<form action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="POST">


<

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.