crazzypappy Posted April 3, 2009 Share Posted April 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152479-text-form-data-to-mysql-depending-on-input-and-user/ Share on other sites More sharing options...
Maq Posted April 4, 2009 Share Posted April 4, 2009 You should just create a white list array and check if the user typed in a valid word with in_array(). Quote Link to comment https://forums.phpfreaks.com/topic/152479-text-form-data-to-mysql-depending-on-input-and-user/#findComment-800819 Share on other sites More sharing options...
crazzypappy Posted April 4, 2009 Author Share Posted April 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152479-text-form-data-to-mysql-depending-on-input-and-user/#findComment-800923 Share on other sites More sharing options...
unrelenting Posted April 4, 2009 Share Posted April 4, 2009 $whitelist = array("word1", "word2", "word3"); if (in_array($input, $whitelist)) { } Quote Link to comment https://forums.phpfreaks.com/topic/152479-text-form-data-to-mysql-depending-on-input-and-user/#findComment-800927 Share on other sites More sharing options...
xtopolis Posted April 4, 2009 Share Posted April 4, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/152479-text-form-data-to-mysql-depending-on-input-and-user/#findComment-800930 Share on other sites More sharing options...
unrelenting Posted April 4, 2009 Share Posted April 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152479-text-form-data-to-mysql-depending-on-input-and-user/#findComment-800931 Share on other sites More sharing options...
Maq Posted April 4, 2009 Share Posted April 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152479-text-form-data-to-mysql-depending-on-input-and-user/#findComment-800933 Share on other sites More sharing options...
crazzypappy Posted April 4, 2009 Author Share Posted April 4, 2009 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 ?? Quote Link to comment https://forums.phpfreaks.com/topic/152479-text-form-data-to-mysql-depending-on-input-and-user/#findComment-800943 Share on other sites More sharing options...
Maq Posted April 4, 2009 Share Posted April 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152479-text-form-data-to-mysql-depending-on-input-and-user/#findComment-800944 Share on other sites More sharing options...
xtopolis Posted April 4, 2009 Share Posted April 4, 2009 If the list has the potential to expand, you can use the array search method. Be careful of Maq's example as it doesn't take into account that you increment the columns each time. (gword1, gword2..etc) Quote Link to comment https://forums.phpfreaks.com/topic/152479-text-form-data-to-mysql-depending-on-input-and-user/#findComment-800945 Share on other sites More sharing options...
crazzypappy Posted April 4, 2009 Author Share Posted April 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152479-text-form-data-to-mysql-depending-on-input-and-user/#findComment-800980 Share on other sites More sharing options...
Maq Posted April 4, 2009 Share Posted April 4, 2009 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"> < Quote Link to comment https://forums.phpfreaks.com/topic/152479-text-form-data-to-mysql-depending-on-input-and-user/#findComment-800990 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.