mysql update 2 blobs at once not working - mysql

i'm having an issue when trying to update two BLOB fields in a row using mysql and php commands.
Inserting the BLOBs into the row seems to work no problem, here is what I do.
$logotemp = $_FILES['eventlogo']['tmp_name'];
$thumbnailtemp = $_FILES['eventthumbnail']['tmp_name'];
$openlogo = fopen($logotemp, 'r');
$openthumbnail = fopen($thumbnailtemp, 'r');
$logo = fread($openlogo, filesize($logotemp));
$logo = addslashes($logo);
$thumbnail = fread($openthumbnail, filesize($thumbnailtemp));
$thumbnail = addslashes($thumbnail);
fclose($openlogo);
fclose($openthumbnail);
So I have two form file inputs, and those files are read, and then set as the variables $log and $thumbnail. I then use the following command to enter this into the DB:
$qry = "INSERT INTO $table (`Event Logo`, `Venue Logo`) VALUES ('$logo', '$thumbnail')";
$result = mysql_query($qry);
if(!$result) {
die(mysql_error());
}
The above works fine, although I have trimmed out the rest of the fields that also get filled. The query works, and I can return the images to a page, and then display them along with all of the other information from that row.
I then want to edit the row, so created a new php file called edit.php, which is a copy of the php file used above, called new.php.
This means that the form is identical, and when the page is displayed, the value for each input is prefilled with the info from the database, the logo and thumbnail are shown next to the upload fields.
If I then run a query to update the row, using almost the same code as above, it always enteres an empty value into both blobs, essentially deleting the uploaded images. Here is what happens:
$id = $_POST['eventid'];
$logotemp = $_FILES['eventlogo']['tmp_name'];
$openlogo = fopen($logotemp, 'r');
$logo = fread($openlogo, filesize($logotemp));
$thumbnailtemp = $_FILES['eventthumbnail']['tmp_name'];
$openthumbnail = fopen($thumbnailtemp, 'r');
$thumbnail = fread($openthumbnail, filesize($thumbnailtemp));
fclose($openlogo);
fclose($openthumbnail);
So once again, the form fields are still called eventlogo and eventthumbnail, and the variables are still $logo and $thumbnail. I then use the following query to update the row:
$qry = "UPDATE $table SET `Event Name` = '$name', `Date` = '$date', `Time` = '$time', `Venue` = '$venue', `Price` = '$price', `Open To` = '$opento', `Rep Name` = '$repname', `Rep Email` = '$repemail', `Address` = '$address', `Website` = '$website', `Phone` = '$phone', `Description` = '$description', `Event Logo` = '$logo', `Venue Logo` = '$thumbnail' WHERE `Event ID` = '$id'";
I have left in the other variables that are updated this time.
$result = mysql_query($qry);
if(!$result) {
die(mysql_error());
}
When the query runs, it will update any other fields I want, except for the two image BLOB fields at the end. Considering I copied and pasted the code for the upload fields, the code to read the contents of that field, and then manually typed out a query to update those fields, I can't see what's going wrong.
Am I missing something obvious? Any help is greatly appreciated.
Thanks, Eds

Cant find the code I sorted this with, but I think I ended up just updating one at a time.

Related

importing data from another database based on field value

I have a website where I am trying to setup a page where someone would have to enter a number into a field, which would correspond to a field in the other database, and import (add) the records from that database, into a table of another database, and I am having trouble with that.
I know how to update and add data from table to table within the same database, but just unsure how I would do that from another database.
This is the code that I would use to add data to another table
$data = array();
$keyvalues = array();
$data["Number"] = $values['Number'];
$data["Rider Name"] = $values['Rider Name'];
$data["Horse Name"] = $values['Horse Name'];
$data["Division Name"] = $values['Division Name'];
$data["Level"] = $values['Level'];
$data["Division Type"] = $values['Division Type'];
$data["Show Type"] = $values['Show Type'];
$data["Dressage Test"] = $values['Dressage Test'];
$keyvalues["Number"] = $values['Number'];
if ($values['Number'] = $values['Number']){
DB::Insert("Scoring", $data, $keyvalues );
}
But not sure how I would do it from another database. Any help would be appreciated.

MYSQL DELETE INNER JOIN only shows deletes after page reload

I have managed to get working a Duplicate field Check using INNER JOIN with PHP MYSQL , however the delete only shows after a page reload , the 1st time i load the page the duplicate entry's are shown then i reload the page and the DELETES are no longer showing. i dont understand why, does this code show any reason for this to happen? a page refresh does not happen no matter what code i use, nothing is run during the IF ($RowsDC >1) check other than the DELETE statement
here is the code:
while($row = $stat->fetch()){
//checking for duplicates
try {
$db2 = new PDO("mysql:host=localhost;dbname=classifieds2", 'root', ''); // 1. set database with this instead of conect - or change conect to this
$db2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$queryDC="SELECT * FROM listings WHERE listID = ? AND UID = ? ";
$statDC=$db2->prepare($queryDC);
$statDC->execute(array("$listID","$UID"));
$ResultDC = $statDC->fetchAll();
$RowsDC = count($ResultDC);
if ($RowsDC >1){ $db2A = new PDO("mysql:host=localhost;dbname=classifieds2", 'root', ''); // 1. set database with this instead of conect - or change conect to this
$db2A->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query01DC="DELETE a FROM listings a INNER JOIN listings a2 WHERE a.id < a2.id AND a.listID = a2.listID ";
$stat01DC=$db2A->prepare($query01DC);
$stat01DC->execute();
??? here i have tried to refresh the page but it does not no matter how i try nothing is run ???
}
}
catch (PDOException $e){
$_SESSION['message']="Database Error #vbmDUPCHECK2 <br> <h6><h6>Signed Out For Security Reasons</h6></h6>";
$_SESSION['loggedin']='000';
echo $errorcall;
die();
exit();
}
}//WHILE END

How to solve sql error " product portfolio has diversified to encompass a highly successful multi-brand' at line 1"

I am kind of new one for mysql and php. a week ago this code worked perfectly and when now I am trying it shows this error message
Error : You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 's product portfolio has diversified to encompass a highly
successful multi-brand' at line 1
I search how to solve that after spending a whole day, but couldn't figure it out.
I have tried similar questions here in stackoverflow, Yet I am stucked here.
A help would be really admired
Given below is my code
<?php
if(isset($_POST['upload']))
{ $company_name =$_POST['company_name'];
$service =$_POST['service'];
$email =$_POST['email'];
$password =$_POST['password'];
$details =$_POST['details'];
$fileName = $_FILES['Filename']['name'];
$fileName1 = $_FILES['Filename1']['name'];
$fileName2 = $_FILES['Filename2']['name'];
$fileName3 = $_FILES['Filename3']['name'];
$fileName4 = $_FILES['Filename4']['name'];
$target = "company_images/";
$fileTarget = $target.$fileName;
$fileTarget1 = $target.$fileName1;
$fileTarget2 = $target.$fileName2;
$fileTarget3 = $target.$fileName3;
$fileTarget4 = $target.$fileName4;
$tempFileName = $_FILES["Filename"]["tmp_name"];
$tempFileName1 = $_FILES["Filename1"]["tmp_name"];
$tempFileName2 = $_FILES["Filename2"]["tmp_name"];
$tempFileName3 = $_FILES["Filename3"]["tmp_name"];
$tempFileName4 = $_FILES["Filename4"]["tmp_name"];
$result = move_uploaded_file($tempFileName,$fileTarget);
$result1 = move_uploaded_file($tempFileName1,$fileTarget1);
$result2 = move_uploaded_file($tempFileName2,$fileTarget2);
$result3 = move_uploaded_file($tempFileName3,$fileTarget3);
$result4 = move_uploaded_file($tempFileName4,$fileTarget4);
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="pdf_uploads/";
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);//anthima
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$query = "INSERT INTO company_details( company_name,service, email, password, details,image_path,file_name,image_path1,file_name1,image_path2,file_name2,image_path3,file_name3,file,type,size,image_path4,file_name4) VALUES ('$company_name','$service','$email','$password','$details','$fileTarget','$fileName','$fileTarget1','$fileName1','$fileTarget2','$fileName2','$fileTarget3','$fileName3','$final_file','$file_type','$new_size','$fileTarget4','$fileName4')";
$con->query($query) or die("Error : ".mysqli_error($con));
mysqli_close($con);
}
}
?>
<?php
Given below is the test data error
VALUES ('singer','Hardware','singer#gmail.com','singer','Singer has been in Sr' at line 1
Because you never sanitize anything and put the data straight into your query,
$company_name =$_POST['company_name'];
$service =$_POST['service'];
$email =$_POST['email'];
$password =$_POST['password'];
$details =$_POST['details'];
...
$query = "INSERT INTO
company_details( company_name,service, email, password, details,image_path,file_name,image_path1,file_name1,image_path2,file_name2,image_path3,file_name3,file,type,size,image_path4,file_name4)
VALUES (
'$company_name','$service','$email','$password','$details','$fileTarget','$fileName','$fileTarget1','$fileName1','$fileTarget2','$fileName2','$fileTarget3','$fileName3','$final_file','$file_type','$new_size','$fileTarget4','$fileName4'
)";
your problem is most likely in the data
's product portfolio has diversified to encompass a highly successful multi-brand
Maybe you have unscaped apostrophes in your data, so you're kinda SQL-injecting yourself. The query ends before the string shown in the error.
The solution is to escape special chars before inserting like in this question: How do I escape only single quotes?
In your case, start with the details
$details = addcslashes($_POST['details'], "'");
or
$details = addslashes($_POST['details']);
But keep adding test scenarios for your code. E.g. what happens if company name gets something like Mc'Donaldson? What is the set of chars you want to accept for each field? Then you will know how to validate those fields and create your functions (or reuse something)

Magento, i need define first product image as thumbnail

I had imported my old oscommerce site with 15000 produts for magento!
But unfortably, the thumbnails are dont defined automatically
http://imgur.com/cNdXd
i can do maually, but 15000 produts is a larg number and i need sped a big amout of time for do it
if anywone can give-me a mysql command to set the first produts image as Base Image, Small Image and Thumbnail will be awesome
thanks
Try update them via api:
$proxy = new SoapClient('http://magentohost/api/soap/?wsdl');
$sessionId = $proxy->login('apiUser', 'apiKey');
$products = $proxy->call($sessionId, 'product.list');
foreach ($products as $product) {
$images = $proxy->call($sessionId, 'product_media.list', $product['sku'])
$imageFilename = $images[0]['file'];
$proxy->call($sessionId, 'product_media.update', array(
$product['sku'],
$imageFilename,
array('types' => array('image', 'thumbnail')
));
}
Related documentation links:
http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_product_attribute_media
http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_product#catalog_product.list
Im having a problem with this SQL code i got somewhere which is supposed to set your first image as image, small image and thumbnail:
UPDATE catalog_product_entity_media_gallery AS mg,
catalog_product_entity_media_gallery_value AS mgv,
catalog_product_entity_varchar AS ev
SET ev.value = mg.value
WHERE mg.value_id = mgv.value_id
AND mg.entity_id = ev.entity_id
AND ev.attribute_code IN ('image', 'small_image', 'thumbnail')
AND mgv.position = 1;
Doesn't change any rows...
Also, my attribute_id's for image, small_image, thumbnail and media_gallery in Magento CE v1.6.2 are 106,109,493,703 using EMSupermarket template.
The following code also does nothing:
UPDATE catalog_product_entity_media_gallery AS mg,
catalog_product_entity_media_gallery_value AS mgv,
catalog_product_entity_varchar AS ev
SET ev.value = mg.value
WHERE mg.value_id = mgv.value_id
AND mg.entity_id = ev.entity_id
AND ev.attribute_id IN (106,109,493)
AND mgv.position = 1;
Anyone how to do this correctly with my id's?
What happened with my oscommerce product import was that the imported product images only end up under the field name media_image, but not image, small_image or thumbnail so i want to copy the media_image values over to the other 3, any ideas how to do this?
I had the same problem. I found this page and ran that SQL command, and also did not get any results. But I modified it to work for me. Here is what I did.
First of all, I changed line 7 from:
AND ev.attribute_code IN ('image', 'small_image', 'thumbnail')
to
AND ev.attribute_ID = '74'
the 74 is my unique attribute id for 'image'. You will have a different number here. You find it by looking it up in mySQL...
75 was for 'small_image' and 76 for 'thumbnail'.
So I ran this code 3 times, 1 for each of those attribute number. Each time I change line 7 ev.attribute_ID = '75' :
UPDATE catalog_product_entity_media_gallery AS mg,
catalog_product_entity_media_gallery_value AS mgv,
catalog_product_entity_varchar AS ev
SET ev.value = mg.value
WHERE mg.value_id = mgv.value_id
AND mg.entity_id = ev.entity_id
AND ev.attribute_ID = '76'
AND mgv.position = 1;

MySQL Update Statement + File Upload

Been staring at this all day and can't seem to figure out why my update statement fails to update the field 'image_filename':
$fileName = $_FILES['image_filename'];
if($fileName["name"] <> ""){
$imageFile = $fileName['name'];
$destination = "../../../../assets/resources/images/".$fileName['name'];
move_uploaded_file($fileName['name'], $destination);
}
$updateSQL = sprintf("UPDATE content SET image_filename='$imageFile' WHERE id=%s",
GetSQLValueString($_POST['resource_id'], "int"));
mysql_select_db($database_conn_talent, $conn_talent);
$Result1 = mysql_query($updateSQL, $conn_talent) or die(mysql_error());
Can a SQL pro tell me what I"m missing? Much thanks in advance for your feedback!
You appear to be building a query, but never executing it. Also, Drupal'll handle all the sprintfing for you, if you let it.
$query = "UPDATE content SET image_filename='$imageFile' WHERE id=%i";
db_query($query, $_POST['resource_id']);