Jump to content

Archived

This topic is now archived and is closed to further replies.

jimmyelewis

Checking Variable

Recommended Posts

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 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>';
}

[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 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>';
}
[/code]

Share this post


Link to post
Share on other sites
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 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>';
}
?>[/code]

Ray

Share this post


Link to post
Share on other sites
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 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>';
}
?>[/code]

Ray

Share this post


Link to post
Share on other sites

×

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.