De code om afbeelding te uploaden

<?php				
if ( isset($_SESSION['username']) ) {
echo "<b><a>
  <form enctype='multipart/form-data' action='YourPhotos2.php?id=$id&&username=$username' method='post'>
  <input type='hidden' name='MAX_FILE_SIZE' value='99999999' />
  <div><input name='userfile' type='file' /></div>
  <div><input type='submit' value='Submit' /></div>
  </form>";
	}
else {
echo"";}?>

de code om het op te slaan in database:

<?php
    session_start(); ?>
<?php
if(isset($_GET['username']))
{ 
  $username = $_GET['username'];
  // getting info from db
}?>
<?php
if(isset($_GET['id']))
{ 
  $id = intval($_GET['id']);
  // getting info from db
}
?>
<?php
/*** check if a file was submitted ***/
if(!isset($_FILES['userfile']))
    {
    echo '<p>Please select a file</p>';
    }
else
    {
    try    {
        upload();
        /*** give praise and thanks to the php gods ***/
        echo '<p>Thank you for submitting</p>';
        }
    catch(Exception $e)
        {
        echo '<h4>'.$e->getMessage().'</h4>';
        }
    }
?><?php
/**
 *
 * the upload function
 * 
 * @access public
 *
 * @return void
 *
 */
function upload(){
/*** check if a file was uploaded ***/
if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false)
    {
    /***  get the image info. ***/
    $size = getimagesize($_FILES['userfile']['tmp_name']);
    /*** assign our variables ***/
    $type = $size['mime'];
    $imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb');
    $size = $size[3];
    $name = $_FILES['userfile']['name'];
    $maxsize = 99999999;


    /***  check the file is less than the maximum file size ***/
    if($_FILES['userfile']['size'] < $maxsize )
        {
        /*** connect to db ***/
        $dbh = new PDO("mysql:host=localhost;dbname=sfriends", 'root', 'superbart');

                /*** set the error mode ***/
                $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            /*** our sql query ***/
        $stmt = $dbh->prepare("INSERT INTO testblob (image_type ,image, image_size, image_name, user_id) VALUES (? ,?, ?, ?, ?)");

        /*** bind the params ***/
        $stmt->bindParam(1, $type);
        $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
        $stmt->bindParam(3, $size);
        $stmt->bindParam(4, $name);
		$stmt->bindParam(5, $id);
        /*** execute the query ***/
        $stmt->execute();
        }
    else
        {
        /*** throw an exception is image is not of type ***/
        throw new Exception("File Size Error");
        }
    }
else
    {
    // if the file is not less than the maximum allowed, print an error
    throw new Exception("Unsupported Image Format!");
    }
}
?>

Maar als ik dat probeer geeft hij het volgende aan:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null.

Hoe kan dit?
Goed lezen wat er staat: Column 'user_id' cannot be null.

$id is nergens gedefineerd binnen je functie.

Daarbij:
- images opslaan in een db? :/
- functie definieren binnen een if/try statement (en daarna maar 1x uitvoeren)? :/
Bart even een tip voor jou. Gebruik in je bestandsnamen GEEN hoofdletters.
action='YourPhotos2.php?id=$id&&username=$username' method='post'

Ook de filesize in je HTML zetten is een erg stomme zet, doe dit in PHP want de gebruiker kan nu via het formulier zelf de bestandsgrootte insetellen, dus word je server lekker volgezet met grote bestanden.
input type='hidden' name='MAX_FILE_SIZE' value='99999999'
Het uploaden van een afbeelding is gelukt. Maar heb nu een nieuw iets.
Ik ben bezig met het maken van een zoekfunctie voor het zoeken van een gebruiker.


<?php
$term = $_POST['term'];
 
if ($result = mysqli_query($con,"SELECT COUNT(*) FROM users WHERE username=$term")) { 
	$row = mysqli_fetch_assoc($result);  
    if ($row['COUNT(*)'] == 0) {
    while($row = mysqli_fetch_array($result))      {
    echo '<br/> First Name: '.$row['username'];
    echo '<br/><br/>';
    }
 
echo "";
}} 
else { 
    // no 
    // print status message 
    echo "No rows found!"; 
}
// free result set memory 
mysqli_close($con);
?>

dit is de php code van de zoekfunctie.
Als ik naar een naam zoek die ook echt in 'users' staat, zegt hij:"No rows found".
terwijl hij als hij een gebruiker vind, hem wel hoort te weergeven.
1. overzichtelijker programmeren zou handig zijn
2. SQL-injection is mogelijk!
3. moet je zoeken in een tabel niet met LIKE-keyword van SQL doen?
en met quotes en ''?

WHERE username = '$term'
of
WHERE username LIKE '%$term%'

(ander punt: beveilig je invoer: http://wiki.pfz.nl/sqlinjectie )

Ander punt:

if ( isset($_SESSION['username']) ) {

hiermee beveilig je dat het form alleen getoond wordt als iemand ingelogd is.

Echter: je kijkt nergens meer naar als je de upload verwerkt.
Als iemand de action aanpast tot action='YourPhotos2.php?id=10&&username=bart'

dan zal dat zomaar verwerkt worden.
En dat terwijl bij het verwerken ook de $_SESSION gewoon beschikbaar is. Je hoeft dit dus niet zichtbaar mee te sturen.

[size=xsmall]Toevoeging op 16/10/2015 10:39:15:[/size]

Bart de kinkelaar op 16/10/2015 10:36:39

Dan laat hij een blanco pagina zien.


Dan heb je een fatal error aan je broek hangen (parse error) en heb je je error reporting uit staan.
Zet die aan, of zoek de error in je log files (gewoon op scherm zetten is natuurlijk een heel stuk eenvoudiger)
Error reporting staat aan?

Reageren