Condintional statements when posting a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

Condintional statements when posting a form

Post by mikes1471 »

Hi Guys

I have an upload form as displayed below

Code: Select all

<?php
 
include('design/header.php');
require('connect.php');
 
$id = $_SESSION['id'];
 
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
 
//This applies the function to our file
$ext = findexts ($_FILES['uploaded']['name']) ;
 
//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
function str_rand($length = 8, $seeds = 'alphanum')
{
    // Possible seeds
    $seedings['alpha'] = 'abcdefghijklmnopqrstuvwqyz';
    $seedings['numeric'] = '0123456789';
    $seedings['alphanum'] = 'abcdefghijklmnopqrstuvwqyz0123456789';
    $seedings['hexidec'] = '0123456789abcdef';
   
    // Choose seed
    if (isset($seedings[$seeds]))
    {
        $seeds = $seedings[$seeds];
    }
   
    // Seed generator
    list($usec, $sec) = explode(' ', microtime());
    $seed = (float) $sec + ((float) $usec * 100000);
    mt_srand($seed);
   
    // Generate
    $str = '';
    $seeds_count = strlen($seeds);
   
    for ($i = 0; $length > $i; $i++)
        {
        $str .= $seeds{mt_rand(0, $seeds_count - 1)};
        }
    return $str;
}
 
//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = str_rand(12, 'ABCDEFGHJKLMNPQRSTUVWXYZ0123456789').".";
 
//This assigns the subdirectory you want to save into... make sure it exists!
$target = "store/";
 
//This combines the directory, the random file name, and the extension
$target = $target . $ran2.$ext;
 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
 
$sql = "INSERT INTO `users`
                    (`userid`,`location`)
                    VALUES ('".$id."','".$ran2.$ext."')";
$res = mysql_query($sql) or die(mysql_error());
echo "The file has been uploaded as ".$ran2.$ext;
}
else
{
echo "<br /><font size='6' face='arial' color='#33cc00'>Picture Upload</font>";
}
?>
 
<table width='30%'><tr><td>
<form enctype="multipart/form-data" action="upload.php" method="POST"><br />
Please choose a file: <input name="uploaded" type="file" /><br /><br />
Make this my default picture<input name="radiobutton" type="radio" value="radiobutton" />Yes <input name="radiobutton" type="radio" value="radiobutton" checked="checked" /> 
No<br />
<input type="submit" value="Upload" />
</td>
</tr>
</form>
 
<?
include("design/footer.php");
?>
and am looking to add a conditional statement that says if the user selects 'No' from the 'make this my default picture' area of the form then post

Code: Select all

$sql = "INSERT INTO `users`
                    (`userid`,`location`)
                    VALUES ('".$id."','".$ran2.$ext."')";
However, if the user selects 'Yes' I want the form to post

Code: Select all

$sql = "INSERT INTO `users`
                    (`userid`,`location`,`defaultimage`)
                    VALUES ('".$id."','".$ran2.$ext."','1')";
The problem is I'm not entirely sure how to write the code, I've looked into if..else statements and have tried to specify what Ive explained in this forum but am going wrong somewhere, would really appreciate help on this one
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

Re: Condintional statements when posting a form

Post by mikes1471 »

This is the closest I've got to coding this upload function the way I want, sadly it doesnt work:

Code: Select all

<?php
 
include('design/header.php');
require('connect.php');
 
$id = $_SESSION['id'];
 
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
 
//This applies the function to our file
$ext = findexts ($_FILES['uploaded']['name']) ;
 
//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
function str_rand($length = 8, $seeds = 'alphanum')
{
    // Possible seeds
    $seedings['alpha'] = 'abcdefghijklmnopqrstuvwqyz';
    $seedings['numeric'] = '0123456789';
    $seedings['alphanum'] = 'abcdefghijklmnopqrstuvwqyz0123456789';
    $seedings['hexidec'] = '0123456789abcdef';
   
    // Choose seed
    if (isset($seedings[$seeds]))
    {
        $seeds = $seedings[$seeds];
    }
   
    // Seed generator
    list($usec, $sec) = explode(' ', microtime());
    $seed = (float) $sec + ((float) $usec * 100000);
    mt_srand($seed);
   
    // Generate
    $str = '';
    $seeds_count = strlen($seeds);
   
    for ($i = 0; $length > $i; $i++)
        {
        $str .= $seeds{mt_rand(0, $seeds_count - 1)};
        }
    return $str;
}
 
//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = str_rand(12, 'ABCDEFGHJKLMNPQRSTUVWXYZ0123456789').".";
 
//This assigns the subdirectory to save to
$target = "store/";
 
//This combines the directory, the random file name, and the extension
$target = $target . $ran2.$ext;
 
$buttonyes = 'unchecked';
$buttonno = 'unchecked';
 
if (isset($_POST['upload'])) {
 
$selected_radio = $_POST['radiobutton'];
 
if ($selected_radio =='buttonyes') {
$sql = "INSERT INTO `users`
                    (`userid`,`location`,`defaultimage`)
                    VALUES ('".$id."','".$ran2.$ext."','1')";
}
else if ($selected_radio =='buttonno') {
$sql = "INSERT INTO `users`
                    (`userid`,`location`)
                    VALUES ('".$id."','".$ran2.$ext."')";
}
}
 
//if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
//{
 
//$sql = "INSERT INTO `users`
//                    (`userid`,`location`,`defaultimage`)
//                    VALUES ('".$id."','".$ran2.$ext."','1')";
//$res = mysql_query($sql) or die(mysql_error());
//echo "The file has been uploaded as ".$ran2.$ext;
//}
else
{
echo "<br /><font size='6' face='arial' color='#33cc00'>Picture Upload</font>";
}
?>
 
<table width='30%'><tr><td>
<form enctype="multipart/form-data" action="upload.php" method="POST"><br />
Please choose a file: <input name="uploaded" type="file" /><br /><br />
Make this my default picture<input name="radiobutton" type="radio" value="yes" <?PHP print $buttonyes; ?>/>Yes <input name="radiobutton" type="radio" value="no" checked="checked" <?PHP print $buttonno; ?>/> 
No<br />
<input type="submit" name="upload" value="Upload" />
</td>
</tr>
</form>
 
<?
include("design/footer.php");
?>
Post Reply