Jump to content

forumnz

Members
  • Posts

    735
  • Joined

  • Last visited

Everything posted by forumnz

  1. Hi, I am trying to upload an image and turn a copy into a thumb. I had it working when it was within it's own script and the form action was SELF but now I'm trying to make it part of another form and I'm POSTing the data. I have changed various $_FILES to $_POSTS etc.. I don't know if thats the problem, but I just can't get it to work.. Here is the snippet: <?php define ("MAX_SIZE","3000"); define ("WIDTH","200"); define ("HEIGHT","200"); // this is the function that will create the thumbnail image from the uploaded image // the resize will be done considering the width and height defined, but without deforming the image function make_thumb($img_name,$filename,$new_w,$new_h) { //get image extension. $ext=getExtension($img_name); //creates the new image using the appropriate function from gd library if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) $src_img=imagecreatefromjpeg($img_name); if(!strcmp("png",$ext)) $src_img=imagecreatefrompng($img_name); if(!strcmp("gif",$ext)) $src_img=imagecreatefromgif($img_name); //gets the dimmensions of the image $old_x=imageSX($src_img); $old_y=imageSY($src_img); // next we will calculate the new dimmensions for the thumbnail image // the next steps will be taken: // 1. calculate the ratio by dividing the old dimmensions with the new ones // 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable // and the height will be calculated so the image ratio will not change // 3. otherwise we will use the height ratio for the image // as a result, only one of the dimmensions will be from the fixed ones $ratio1=$old_x/$new_w; $ratio2=$old_y/$new_h; if($ratio1>$ratio2) { $thumb_w=$new_w; $thumb_h=$old_y/$ratio1; } else { $thumb_h=$new_h; $thumb_w=$old_x/$ratio2; } // we create a new image with the new dimmensions $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); // resize the big image to the new created one imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); // output the created image to the file. Now we will have the thumbnail into the file named by $filename if(!strcmp("png",$ext)) imagepng($dst_img,$filename); elseif(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) imagejpeg($dst_img,$filename); else imagegif($dst_img,$filename); //destroys source and destination images. imagedestroy($dst_img); imagedestroy($src_img); } // This function reads the extension of the file. // It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } // This variable is used as a flag. The value is initialized with 0 (meaning no error found) //and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded. $errors=0; // checks if the form has been submitted //reads the name of the file the user submitted for uploading $image=$_POST['image']['name']; // if it is not empty if ($image) { // get the original name of the file from the clients machine $filename = stripslashes($_POST['image']['name']); // get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); // if it is not a known extension, we will suppose it is an error, print an error message //and will not upload the file, otherwise we continue if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo '<h1>Unknown extension!</h1>'; $errors=1; } else { // get the size of the image in bytes // $_POST[\'image\'][\'tmp_name\'] is the temporary filename of the file in which the uploaded file was stored on the server $size=getimagesize($_POST['image']['tmp_name']); $sizekb=filesize($_POST['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($sizekb > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name=time().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="../logo/".$image_name; $copied = copy($_POST['image']['tmp_name'], $newname); //we verify if the image has been uploaded, and print error instead if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; } else { // the new thumbnail image will be placed in images/thumbs/ folder $thumb_name='../logo/thumbs/thumb_'.$image_name; // call the function that will create the thumbnail. The function will get as parameters //the image name, the thumbnail name and the width and height desired for the thumbnail $thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT); }} } ?> The error: Notice: Undefined index: image in /nfs/html/process.php on line 104 Please help! Much appreciated! Sam
  2. I'm not a total newb in PHP and I do the following all the time, but for one reason or another this code snippet won't work.. why not? <?php $sqlc = mysql_query("SELECT * FROM current_org WHERE user_hash='$uh' AND hash_key='$hk' ORDER BY date DESC LIMIT 1") or die(mysql_error); $num = mysql_num_rows($sqlc); echo $num; while($rowc = mysql_fetch_array($sqlc)) { if($num == '0') { echo "Hey!"; } else{ echo "Test!"; } } ?> All it does is display something like: 1Test! 1Test! 0000000000000 Meaning that there are two rows existing.. but where is the echoed "Hey!"? Thanks
  3. I have a timestamp in a database the problem is that it's 10 hours behind the right time. This is the format "2009-05-17 15:59:53". How can I add 10 hours to this? Thanks Sam
  4. Hey! I have dates in my database (datetime) like 2009-04-30 20:45:45. This is fine, however the time is 17 hours behind the time in my country.. how can I easily modify this each time I retrieve these dates from the database? Thanks Sam
  5. I'm using Javascript with PHP. How can I return success:true to the JS fro the PHP. It is queried using AJAX. Thanks by the way, I tried return {success:true};
  6. Thanks guys! The - just needed replacing with something else (I just deleted it). Much appreciated, Sam
  7. That's cool Ken2k7 - you learnt something new! Thanks gevans. I did that and I get: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-comp-1007 = '1122', ext-comp-1008 = 'Nissan Light', ext-comp-1009 = '2', ext-co' at line 1 I can't see anything wrong though. I don't need to use backticks (I had a similar query yesterday that worked)
  8. Actually Ken2k7, Thorpe helped me with a similar issue yesterday and he did suggest INSERT INTO table SET... It worked. Anyone have any suggestions?
  9. <?php $arr = json_decode($form, true); $sql = "INSERT INTO acc_rec_invoice SET hash_key = '$hk', inv_key = '$ik', "; foreach ($arr as $k => $v) { $sql .= "$k = '$v', "; } $sql = substr($sql, 0, -2); mysql_query($sql); ?> This is the snippet of the code. When I echo $sql, the output is: INSERT INTO acc_rec_invoice SET hash_key = '', inv_key = '4d3ace164d2fc28cd7efee6eb28fc1fd', toperson = 'Sam Walker', date1 = '10 May 2009', date2 = '24 May 2009', invnumber = '006', refer = 'Products', tax_s = 'inclusive', ext-comp-1007 = '1122', ext-comp-1008 = 'Nissan Light', ext-comp-1009 = '2', ext-comp-1010 = '69', ext-comp-1011 = 'acc3', ext-comp-1012 = 'tax3' Should work shouldn't it? I do have all the fields in the database. Thanks
  10. Hey thanks this is the query I have at the moment.. The $sql echoed: mysql_query("INSERT INTO acc_rec_inv SET qty = '66', price = '5', item = 'ggg', des = 'g', acc = 'h', tax = 'h'"); mysql_query("INSERT INTO acc_rec_inv SET qty = '66', price = '5', item = 'ggg', des = 'g', acc = 'h', tax = 'h'"); It looks right to me, but how can I actually 'run' the variable instead of echoing it?
  11. Or not? That wouldn't work would it..?
  12. It looks like: INSERT INTO acc_rec_inv SET qty = '66', price = '5', item = 'ggg', des = 'g', acc = 'h', tax = 'h'INSERT INTO acc_rec_inv SET qty = '66', price = '5', item = 'ggg', des = 'g', acc = 'h', tax = 'h' I guess then I should just change it to complete each query separately?
  13. Hi thorpe Thanks, but for some reason that doesn't work (I fixed the substr), but it isn't inserting anything... WHy could this be?
  14. Thanks for the help. I am almost there. I have the right amount of columns etc.. Just when there is more than one set of data, in adds more fields within the VALUES (here) - that means that there isn't the right amount of columns... Here is what I have: <?php foreach ($d as $val) { foreach ($val as $arr) { $in .= "'" . $arr . "', "; $ina = "('$hk', '$ik', '$lk', " . $in . ")"; $l = array(", )"); $ina = str_replace($l, ")", $ina); } $sess_hash = (rand() . rand() . rand() . rand() . rand() . rand() . rand()); $sess_hash = (md5($sess_hash)); $lk = $sess_hash; echo $ina; mysql_query("INSERT INTO acc_rec_inv (hash_key, inv_key, line_key, item, des, qty, price, acc, tax) VALUES $ina")or die(mysql_error()); } ?> What do you think? Thanks
  15. Oops! This is better: <?php foreach ($d as $val) { foreach ($val as $arr) { $in .= "'$" . $arr . "', "; } $sess_hash = (rand() . rand() . rand() . rand() . rand() . rand() . rand()); $sess_hash = (md5($sess_hash)); $lk = $sess_hash; echo $in; mysql_query("INSERT INTO acc_rec_inv (hash_key, inv_key, line_key, item, des, qty, price, acc, tax) VALUES ('$hk', '$ik', '$lk', '$in')")or die(mysql_error()); } ?> That sort of works, but since I have 2 rows to insert, I have 2 times as much data, so I need to somehow split the $in between (). Is this easy? Thanks
  16. Oh I see - getting there! I have this at the moment (snippet): <?php foreach ($d as $val) { foreach ($val as $arr) { $sess_hash = (rand() . rand() . rand() . rand() . rand() . rand() . rand()); $sess_hash = (md5($sess_hash)); $lk = $sess_hash; mysql_query("INSERT INTO acc_rec_inv (hash_key, inv_key, line_key, item, des, qty, price, acc, tax) VALUES ('$hk', '$ik', '$lk', '$arr')")or die(mysql_error()); } } ?> But I get this error: Column count doesn't match value count at row 1 Should I have the query outside of the second array?
  17. Ok thanks I'm starting to understand. I'm just taking it step by step at the moment, here is what I have: <?php $e = '[{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"},{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"}]'; $d = json_decode($e, true); $d = (print_r($d)); foreach ($d as $val) { echo $val . " "; } ?> I know the error has something to do with the print_r part, but how can I fix it? Thanks
  18. I wish to insert an array into a database. I am really new to this so please help me. Here is my array output: Array ( [0] => Array ( [qty] => 66 [price] => 5 [item] => ggg [des] => g [acc] => h [tax] => h ) [1] => Array ( [qty] => 66 [price] => 5 [item] => ggg [des] => g [acc] => h [tax] => h ) ) What can I do?
  19. I'm sorry, I'm just so confused. I don't really know what you are saying. Would you suggest I keep going with the function I've got or do something different?
  20. It's not complicated. Start here w3schools.com/php
  21. Yeah, is my function even being used?
  22. I'm having big problems trying to insert an array into a database. Here is what I have so far: <?php $e = '[{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"},{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"}]'; $d = json_decode($e, true); function mysql_insert_array($d) { foreach ($d as $field=>$value) { $fields[] = '`' . $field . '`'; $values[] = "'" . mysql_real_escape_string($value) . "'"; } $field_list = join(',', $fields); $value_list = join(', ', $values); mysql_query("INSERT INTO acc_rec_inv (" . $field_list . ") VALUES (" . $value_list . ")")or die(mysql_error()); } ?> What could be the problem? Thanks
  23. Oh whoops! Here is what I have now: <?php function mysql_insert_array($data) { foreach ($d as $field=>$value) { $fields[] = '`' . $field . '`'; $values[] = "'" . mysql_real_escape_string($value) . "'"; } $field_list = join(',', $fields); $value_list = join(', ', $values); mysql_query("INSERT INTO acc_rec_inv (" . $field_list . ") VALUES (" . $value_list . ")")or die(mysql_error()); } ?> It still doesn't work.. why is that? Thanks
  24. Nope, it doesn't, but it doesn't insert anything either. What could be going on?
  25. I'm trying to insert multiple rows into a database using an array, but it doesn't want to work.. why? <?php $e = '[{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"},{"qty":66,"price":5,"item":"ggg","des":"g","acc":"h","tax":"h"}]'; $d = json_decode($e, true); //echo $d; $sess_hash = (rand() . rand() . rand() . rand() . rand() . rand() . rand()); $sess_hash = (md5($sess_hash)); $ik = $sess_hash; $sess_hash = (rand() . rand() . rand() . rand() . rand() . rand() . rand()); $sess_hash = (md5($sess_hash)); $lk = $sess_hash; $hk = "hashkey123"; function mysql_insert_array($data) { foreach ($d as $field=>$value) { $fields[] = '`' . $field . '`'; $values[] = "'" . mysql_real_escape_string($value) . "'"; } $field_list = join(',', $fields); $value_list = join(', ', $values); $query = ("INSERT INTO acc_rec_inv (hash_key, inv_key, line_key, " . $field_list . ") VALUES ('$hk', '$ik', '$lk', " . $value_list . ")")or die(mysql_error()); return $query; } ?> Yes there is a valid connection. Thanks
×
×
  • Create New...

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.