jimmyelewis Posted March 30, 2006 Share Posted March 30, 2006 Hey I have the following code. I would like for it to check to see if a form has been submitted and if it has then to check the tool_icon variable to make sure the user included an image extension. Right now it displays the form and it doesn't matter if the varible ends in .gif it adds it to the database anyway. What's wrong in my code? Thanks.[code] if (isset($_POST['submit'])) { //handle the form//Opens database connection$dbc = mysql_connect('host', 'user', 'pass');//Selects databasemysql_select_db('dbname');//Define the query$query = "INSERT INTO admin_tools (tool_name, tool_icon, tool_link, tool_cat) VALUES ('{$_POST['tool_name']}', '{$_POST['tool_icon']}', '{$_POST['tool_link']}', '{$_POST['tool_cat']}')";//Runs the queryif (pregi_match(".gif$", $_POST['tool_icon'])){if (mysql_query ($query)) { echo 'The tool has been added.'; } else { echo '<p>Could not add the tool because <b>' . mysql_error() . '</b>. The query was ' .$query . '.</p>'; } mysql_close(); } else{ echo '<p>Please include image extension.</p><form action="index.php" method="post"><p>Tool Name: <input type="text" name="tool_name" size="40" maxsize="50" value="' . $_POST['tool_name'] . '"/></p><p>Tool Icon Name: <input type="text" name="tool_icon" size="40" maxsize="50" value="' . $_POST['tool_icon'] . '"/></p><p>Tool Link: <input type="text" name="tool_link" size="40" maxsize="150" value="' . $_POST['tool_link'] . '"/></p><p>Tool Category: <select name="tool_cat"> <option value="Inventory">Inventory</option> <option value="Network Tools">Network Tools</option> <option value="Reference">Reference</option> <option value="User Account Tools">User Account Tools</option> <option value="Web Tools">Web Tools</option> <option value="' . $_POST['tool_cat'] . '" selected="selected">' . $_POST['tool_cat'] . '</option> </select></p><input type="submit" name="submit" value="Add Tool" /></form>'; } } else {// Display formecho '<form action="index.php" method="post"><p>Tool Name: <input type="text" name="tool_name" size="40" maxsize="50" /></p><p>Tool Icon Name: <input type="text" name="tool_icon" size="40" maxsize="50" /></p><p>Tool Link: <input type="text" name="tool_link" size="40" maxsize="150" /></p><p>Tool Category: <select name="tool_cat"> <option value="Inventory">Inventory</option> <option value="Network Tools">Network Tools</option> <option value="Reference">Reference</option> <option value="User Account Tools">User Account Tools</option> <option value="Web Tools">Web Tools</option> </select></p><input type="submit" name="submit" value="Add Tool" /></form>';}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6180-checking-variable/ Share on other sites More sharing options...
jmag Posted March 30, 2006 Share Posted March 30, 2006 you could use strpos() to check for it pretty easily. something like this might work:[code]if(isset($_POST["tool_icon"]) && strpos($_POST["tool_icon"], ".gif") != FALSE) { //enter stuff into database}else { //echo the form again}[/code]hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/6180-checking-variable/#findComment-22314 Share on other sites More sharing options...
jimmyelewis Posted March 30, 2006 Author Share Posted March 30, 2006 I changed it to the following code; however, it still adds it to the database no matter what tool_icon ends in.[code]if (isset($_POST['submit'])) { //handle the formif(isset($_POST["tool_icon"]) && strpos($_POST["tool_icon"], ".gif") != FALSE) { //enter stuff into database//Opens database connection$dbc = mysql_connect('host', 'user', 'pass');//Selects databasemysql_select_db('db');//Define the query$query = "INSERT INTO admin_tools (tool_name, tool_icon, tool_link, tool_cat) VALUES ('{$_POST['tool_name']}', '{$_POST['tool_icon']}', '{$_POST['tool_link']}', '{$_POST['tool_cat']}')";//Runs the queryif (mysql_query ($query)) { echo 'The tool has been added.'; } else { echo '<p>Could not add the tool because <b>' . mysql_error() . '</b>. The query was ' .$query . '.</p>'; } mysql_close(); } else { //echo the form again echo '<p>Please include image extension.</p><form action="index.php" method="post"><p>Tool Name: <input type="text" name="tool_name" size="40" maxsize="50" value="' . $_POST['tool_name'] . '"/></p><p>Tool Icon Name: <input type="text" name="tool_icon" size="40" maxsize="50" value="' . $_POST['tool_icon'] . '"/></p><p>Tool Link: <input type="text" name="tool_link" size="40" maxsize="150" value="' . $_POST['tool_link'] . '"/></p><p>Tool Category: <select name="tool_cat"> <option value="Inventory">Inventory</option> <option value="Network Tools">Network Tools</option> <option value="Reference">Reference</option> <option value="User Account Tools">User Account Tools</option> <option value="Web Tools">Web Tools</option> <option value="' . $_POST['tool_cat'] . '" selected="selected">' . $_POST['tool_cat'] . '</option> </select></p><input type="submit" name="submit" value="Add Tool" /></form>'; } } else {// Display formecho '<form action="index.php" method="post"><p>Tool Name: <input type="text" name="tool_name" size="40" maxsize="50" /></p><p>Tool Icon Name: <input type="text" name="tool_icon" size="40" maxsize="50" /></p><p>Tool Link: <input type="text" name="tool_link" size="40" maxsize="150" /></p><p>Tool Category: <select name="tool_cat"> <option value="Inventory">Inventory</option> <option value="Network Tools">Network Tools</option> <option value="Reference">Reference</option> <option value="User Account Tools">User Account Tools</option> <option value="Web Tools">Web Tools</option> </select></p><input type="submit" name="submit" value="Add Tool" /></form>';}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6180-checking-variable/#findComment-22320 Share on other sites More sharing options...
craygo Posted March 30, 2006 Share Posted March 30, 2006 Problem is, you are running your insert query before you check the extension.move your query into where the check is[code]<?if (isset($_POST['submit'])) { //handle the form//Opens database connection$dbc = mysql_connect('host', 'user', 'pass');//Selects databasemysql_select_db('dbname');if (pregi_match(".gif$", $_POST['tool_icon'])){//Define the query$query = "INSERT INTO admin_tools (tool_name, tool_icon, tool_link, tool_cat) VALUES ('{$_POST['tool_name']}', '{$_POST['tool_icon']}', '{$_POST['tool_link']}', '{$_POST['tool_cat']}')";//Runs the queryif (mysql_query ($query)) { echo 'The tool has been added.'; } else { echo '<p>Could not add the tool because <b>' . mysql_error() . '</b>. The query was ' .$query . '.</p>'; } mysql_close(); } else{ echo '<p>Please include image extension.</p><form action="index.php" method="post"><p>Tool Name: <input type="text" name="tool_name" size="40" maxsize="50" value="' . $_POST['tool_name'] . '"/></p><p>Tool Icon Name: <input type="text" name="tool_icon" size="40" maxsize="50" value="' . $_POST['tool_icon'] . '"/></p><p>Tool Link: <input type="text" name="tool_link" size="40" maxsize="150" value="' . $_POST['tool_link'] . '"/></p><p>Tool Category: <select name="tool_cat"> <option value="Inventory">Inventory</option> <option value="Network Tools">Network Tools</option> <option value="Reference">Reference</option> <option value="User Account Tools">User Account Tools</option> <option value="Web Tools">Web Tools</option> <option value="' . $_POST['tool_cat'] . '" selected="selected">' . $_POST['tool_cat'] . '</option> </select></p><input type="submit" name="submit" value="Add Tool" /></form>'; } } else {// Display formecho '<form action="index.php" method="post"><p>Tool Name: <input type="text" name="tool_name" size="40" maxsize="50" /></p><p>Tool Icon Name: <input type="text" name="tool_icon" size="40" maxsize="50" /></p><p>Tool Link: <input type="text" name="tool_link" size="40" maxsize="150" /></p><p>Tool Category: <select name="tool_cat"> <option value="Inventory">Inventory</option> <option value="Network Tools">Network Tools</option> <option value="Reference">Reference</option> <option value="User Account Tools">User Account Tools</option> <option value="Web Tools">Web Tools</option> </select></p><input type="submit" name="submit" value="Add Tool" /></form>';}?>[/code]Ray Quote Link to comment https://forums.phpfreaks.com/topic/6180-checking-variable/#findComment-22378 Share on other sites More sharing options...
jimmyelewis Posted March 30, 2006 Author Share Posted March 30, 2006 I just tried that and it still adds it even if it doesnt end in .gif. Quote Link to comment https://forums.phpfreaks.com/topic/6180-checking-variable/#findComment-22392 Share on other sites More sharing options...
craygo Posted March 30, 2006 Share Posted March 30, 2006 OK i changed a little as far as the error checking goes and changed the check but it works fine[code]<?if (isset($_POST['submit'])) { //handle the formif (eregi('.gif', $_POST['tool_icon']) == TRUE){//Opens database connection$dbc = mysql_connect('host', 'user', 'pass');//Selects databasemysql_select_db('dbname');//Define the query$query = "INSERT INTO admin_tools (tool_name, tool_icon, tool_link, tool_cat) VALUES ('{$_POST['tool_name']}', '{$_POST['tool_icon']}', '{$_POST['tool_link']}', '{$_POST['tool_cat']}')";//Runs the querymysql_query ($query) or die (mysql_error());mysql_close(); echo 'The tool has been added.'; } else { echo '<p>Please make sure icon is a .gif file <br>Please include image extension.</p><form action="'.$_SERVER['PHP_SELF'].'" method="post"><p>Tool Name: <input type="text" name="tool_name" size="40" maxsize="50" value="' . $_POST['tool_name'] . '"/></p><p>Tool Icon Name: <input type="text" name="tool_icon" size="40" maxsize="50" value="' . $_POST['tool_icon'] . '"/></p><p>Tool Link: <input type="text" name="tool_link" size="40" maxsize="150" value="' . $_POST['tool_link'] . '"/></p><p>Tool Category: <select name="tool_cat"> <option value="Inventory">Inventory</option> <option value="Network Tools">Network Tools</option> <option value="Reference">Reference</option> <option value="User Account Tools">User Account Tools</option> <option value="Web Tools">Web Tools</option> <option value="' . $_POST['tool_cat'] . '" selected="selected">' . $_POST['tool_cat'] . '</option> </select></p><input type="submit" name="submit" value="Add Tool" /></form>'; } } else {// Display formecho '<form action="'.$_SERVER['PHP_SELF'].'" method="post"><p>Tool Name: <input type="text" name="tool_name" size="40" maxsize="50" /></p><p>Tool Icon Name: <input type="text" name="tool_icon" size="40" maxsize="50" /></p><p>Tool Link: <input type="text" name="tool_link" size="40" maxsize="150" /></p><p>Tool Category: <select name="tool_cat"> <option value="Inventory">Inventory</option> <option value="Network Tools">Network Tools</option> <option value="Reference">Reference</option> <option value="User Account Tools">User Account Tools</option> <option value="Web Tools">Web Tools</option> </select></p><input type="submit" name="submit" value="Add Tool" /></form>';}?>[/code]Ray Quote Link to comment https://forums.phpfreaks.com/topic/6180-checking-variable/#findComment-22420 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.