Jump to content


Photo

Checking Variable


  • Please log in to reply
5 replies to this topic

#1 jimmyelewis

jimmyelewis
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 30 March 2006 - 03:27 PM

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.

if (isset($_POST['submit'])) { //handle the form
//Opens database connection
$dbc = mysql_connect('host', 'user', 'pass');
//Selects database
mysql_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 query
if (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 form
echo '<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>';
}



#2 jmag

jmag
  • Members
  • PipPip
  • Member
  • 22 posts
  • LocationSweden

Posted 30 March 2006 - 03:43 PM

you could use strpos() to check for it pretty easily. something like this might work:

if(isset($_POST["tool_icon"]) && strpos($_POST["tool_icon"], ".gif") != FALSE) {
  //enter stuff into database
}
else {
  //echo the form again
}

hope it helps.

#3 jimmyelewis

jimmyelewis
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 30 March 2006 - 04:14 PM

I changed it to the following code; however, it still adds it to the database no matter what tool_icon ends in.

if (isset($_POST['submit'])) { //handle the form
if(isset($_POST["tool_icon"]) && strpos($_POST["tool_icon"], ".gif") != FALSE) {
  //enter stuff into database
//Opens database connection
$dbc = mysql_connect('host', 'user', 'pass');
//Selects database
mysql_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 query
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 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 form
echo '<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>';
}


#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 30 March 2006 - 06:58 PM

Problem is, you are running your insert query before you check the extension.

move your query into where the check is
<?
if (isset($_POST['submit'])) { //handle the form
//Opens database connection
$dbc = mysql_connect('host', 'user', 'pass');
//Selects database
mysql_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 query
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 form
echo '<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>';
}
?>

Ray

#5 jimmyelewis

jimmyelewis
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 30 March 2006 - 07:28 PM

I just tried that and it still adds it even if it doesnt end in .gif.

#6 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 30 March 2006 - 08:46 PM

OK i changed a little as far as the error checking goes and changed the check but it works fine
<?
if (isset($_POST['submit'])) { //handle the form
if (eregi('.gif', $_POST['tool_icon']) == TRUE){
//Opens database connection
$dbc = mysql_connect('host', 'user', 'pass');
//Selects database
mysql_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 query
mysql_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 form
echo '<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>';
}
?>

Ray




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users