1
xsell
Uploading to server and write Name to Mysql
  • 2010/1/20 7:26

  • xsell

  • Quite a regular

  • Posts: 245

  • Since: 2008/9/2 1


Hello

This is My code , Will Upload a file to my site and write the name of the file to my Database ..

The Quastion is , the Uploading Is Opetional , I do not want the user get error after sumbit, that no file was Uploaded , since its not Optional ..

2- this code is updating to the MySQL , so the problem i have , if there already a name exist and the user did not upload file , the code updates the row with the embty value .
i wanted it that if the user did not upload file to keep the "file" row as is no change to the name , and the Change only if the user Uploaded file ..

form
xoops_load('xoopsformloader');
$sform = new XoopsThemeForm(_MD_TEST_ADD"form""upload.php");

$sform->setExtra("enctype='multipart/form-data'");
$sform->addElement(new XoopsFormText(_MD_TEST_NAME'name'5030));

$sform->addElement(new XoopsFormFile(_MD_TEST_ATTACHMENTS'xoops_upload_file'0), false);

$button_tray = new XoopsFormElementTray('' ,'');
$button_tray->addElement(new XoopsFormButton('''submit'_MD_TEST_SEND'submit'));
$sform->addElement($button_tray);
$sform->display();


upload.php

//Upload things
    
$uploading=($_FILES['xoops_upload_file']['name']);

   
$idHtmlSpecialchars($_GET['id']);

  include_once 
'../../../class/uploader.php';
  
$allowed_mimetypes = array('image/gif''image/jpeg');
$maxfilesize 10485760;
$target "upload/";

 
$uploader = new XoopsMediaUploader($target$allowed_mimetypes$maxfilesize);
 
 if (
$uploader->fetchMedia($_POST["xoops_upload_file"][0])) {
 
$uploader->setTargetFileName($uploader->mediaName);

    if (!
$uploader->upload()) {
    
       
redirect_header(XOOPS_URL "/modules/test/index.php" 6$uploader->getErrors());
    } else {
      
//  echo ""._MD_TEST_ADDED."";
    
}
} else {
   
redirect_header(XOOPS_URL "/modules/test/index.php" 5$uploader->getErrors());
}
}
//Upload things

 
$query =mysql_query("UPDATE  ".$xoopsDB->prefix("test")." SET name='$name', file='$file'  WHERE id='$id' ")
  or die(
"can't edit the test tble");

2
culex
Re: Uploading to server and write Name to Mysql
  • 2010/2/3 12:42

  • culex

  • Module Developer

  • Posts: 711

  • Since: 2004/9/23


Maybe I am misunderstanding but what if you keept the updateing table part inside an if function ?

Something like this...

if ($file!='') {
 
$query =mysql_query("UPDATE  ".$xoopsDB->prefix("test")." SET name='$name', file='$file'  WHERE id='$id' "
} else {
$query =mysql_query("UPDATE  ".$xoopsDB->prefix("test")." SET name='$name' WHERE id='$id' "
}

Login

Who's Online

344 user(s) are online (265 user(s) are browsing Support Forums)


Members: 0


Guests: 344


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits