loxfear Posted August 23, 2014 Share Posted August 23, 2014 ok i added this script in the bottom of my other uploader script. what i wanted to do was, to upload an imagefile to an folder called uploads and in the same time add the name of the file to an database called uploads, together with the next id of another database i dont get any error msg, the page is just blank. <?php include 'sqlconnect.php'; $result = mysql_query(" SHOW TABLE STATUS LIKE 'aktiviteter' "); $data = mysql_fetch_assoc($result); $next_increment = $data['Auto_increment']; $sql = sprintf( "INSERT INTO aktiviteter (`title`, `firma`, `beskrivelse`, `information`, `pris`, `varighed`, `antal`, `adresse`, `by`, `postnummer`, `telefon`, `email`, `hjemmeside`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", mysqli_real_escape_string($con, $_POST['title']), mysqli_real_escape_string($con, $_POST['firma']), mysqli_real_escape_string($con, $_POST['beskrivelse']), mysqli_real_escape_string($con, $_POST['information']), mysqli_real_escape_string($con, $_POST['pris']), mysqli_real_escape_string($con, $_POST['varighed']), mysqli_real_escape_string($con, $_POST['antal']), mysqli_real_escape_string($con, $_POST['adresse']), mysqli_real_escape_string($con, $_POST['by']), mysqli_real_escape_string($con, $_POST['postnummer']), mysqli_real_escape_string($con, $_POST['telefon']), mysqli_real_escape_string($con, $_POST['email']), mysqli_real_escape_string($con, $_POST['hjemmeside']) ); if (!mysqli_query($con, $sql)) { die('Error: ' . mysqli_error($con)); } echo "Aktiviteten er uploaded"; if(isset($_POST['upload'])) { $allowed_filetypes = array('.jpg','.jpeg','.png','.gif'); $max_filesize = 10485760; $upload_path = 'uploads/'; $targetId = $next_increment $filename = $_FILES['billede']['name']; $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); if(!in_array($ext,$allowed_filetypes)) die('The file you attempted to upload is not allowed.'); if(filesize($_FILES['billede']['tmp_name']) > $max_filesize) die('The file you attempted to upload is too large.'); if(!is_writable($upload_path)) die('You cannot upload to the specified directory, please CHMOD it to 777.'); if(move_uploaded_file($_FILES['billede']['tmp_name'],$upload_path . $filename)) { $query = "INSERT INTO uploads (name, target) VALUES ($filename, $targetId)"; mysql_query($query); echo 'Your file upload was successful!'; } else { echo 'There was an error during the file upload. Please try again.'; } } mysqli_close($con); Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 23, 2014 Share Posted August 23, 2014 your code produces a fatal php parse/syntax error due to a missing ;. i'm not going to tell you where, because your development system should have php's error_reporting set to E_ALL and display_errors set to ON in your php.ini so that all php errors will be reported and displayed. for this kind of error, php can point you to the correct area of the code (usually the actual problem causing a syntax error is immediately prior to where the syntax error is reported) and you should be able to find things like missing punctuation. edit: next you have a functional problem in getting the current autoincrement value from a table, increment it, and using it. if you have concurrent requests to your page, you will end up with wrong values. the way you would do this is, after the INSERT query runs for the aktiviteter table, use mysqli_insert_id() to get the actual id that was generated. next, you are mixing mysql_ (no i) and mysqli_ (with an i) functions. you need to use all mysqli_ (with and i) functions. Quote Link to comment Share on other sites More sharing options...
loxfear Posted August 23, 2014 Author Share Posted August 23, 2014 ill look into it later, cant figure out how to chek for errors in sublime 2, but it must be possible ill look trough my insanly messy code and clean it up thx so much Quote Link to comment Share on other sites More sharing options...
loxfear Posted August 24, 2014 Author Share Posted August 24, 2014 well i fixed the missing ; but that only changed so that half of my code works.the file doesn't get uploaded. so is my approach totally wrong? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 24, 2014 Share Posted August 24, 2014 the file doesn't get uploaded how do you know that? what sort of symptom or error do you get? there's a dozen different things that could prevent an upload from working, starting with the form not having the needed enctype attribute all the way through to file/folder permissions preventing move_uploaded_file() from being able to move the file to the destination folder. is your code even testing if the file got uploaded without any errors, before trying to use the uploaded file information? Quote Link to comment 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.