I am trying to update and insert a created thumbnail into a MYSQL BLOB.
I have tried almost anything but can not get it to insert the created picture to the Database.
function update($email)
{
if(file_exists("$email.jpg"))
{
$image = "$email.jpg";
$tbl = 'tpctmembers';
$query1 = "SELECT image FROM $tbl WHERE email = '$email'";
$result = queryMysql($query1);
$rows = mysql_num_rows($result);
if($rows > 0)
{
$query2 = "UPDATE $tbl SET image ='$image' WHERE email ='$email'";
queryMysql($query2);
}
else
{
$query3 = "INSERT into $tbl(image) VALUES('$image') where email = '$email'";
queryMysql($query3);
}
}
}
Thank You !
You need to read the image file, and insert that data, not the string naming the file. See this tutorial.
Related
I want to have a separate query that updates only the imagename and imagetext when the imagefile is left blank and update all the imagename,imagetext and the image itself when the user selects a new image here is my query:
if (isset($_POST['update'])) {
$update_id = $_POST["id"];
$update_imagename = $_POST['imagename'];
// Get image name
$update_imagefile = $_FILES['imagefile']['name'];
// Get text
$update_imagetext = mysqli_real_escape_string($db, $_POST['imagetext']);
// image file directory
$target = "images/portfolio/" . basename($update_imagefile);
$update_query = "UPDATE `images` SET `imagename`='$update_imagename',`imagetext`='$update_imagetext',`imagefile`= '$update_imagefile' WHERE `imageid`='$update_id'"; //delete query
$run = mysqli_query($db, $update_query);
if ($run) {
//javascript function to open in the same window
echo "<script>window.open('artworksupdate.php?=image has been updated','_self')</script>";
}
to determine if you have uploadFile, just do !empty($_POST['imagefile']), following you have sample.
if (!empty($_POST)) {
$update_id = $_POST["id"];
// Get image name
$update_imagename = $_POST['imagename'];
// Get image description
$update_imagetext = $_POST['imagetext'];
// Determine if we have input file set
$haveUploadedFile = !empty($_POST['imagefile']);
if( $haveUploadedFile ) {
// [...] do your upload stuff here.
$update_imagefile = 'myAwesomeImage.png';
$update_query = "UPDATE `images` SET `imagename`='$update_imagename',`imagetext`='$update_imagetext',`imagefile`= '$update_imagefile' WHERE `imageid`='$update_id'"; //delete query
}
else {
$update_query = "UPDATE `images` SET `imagename`='$update_imagename',`imagetext`='$update_imagetext' WHERE `imageid`='$update_id'"; //delete query
}
$run = mysqli_query($db, $update_query);
//[...]
}
I have a function that checks if user data exists in database and returns email id of user. If it doesnt exits then it inserts and should return the inserted user email.
Part of my function
function checkUser($userdata){
$oauth_uid = $userdata->id;
$email = $userdata->emailAddress;
$check = $this->db->query("SELECT * FROM $this->userTable WHERE oauth_uid = '".$oauth_uid."' AND email = '".$email."'");
if(mysqli_num_rows($check) > 0){
$result = $check->fetch_array(MYSQLI_ASSOC);
$query = "UPDATE $this->userTable SET fname = '".$userdata->firstName."', lname = '".$userdata->lastName."', email = '".$userdata->emailAddress."', location = '".$userdata->location->name."', country = '".$userdata->location->country->code."', picture_url = '".$userdata->pictureUrl."', profile_url = '".$userdata->publicProfileUrl."', modified = '".date("Y-m-d H:i:s")."' WHERE id = ".$result['id'];
$this->db->query($query);
return $result['id']; //this works it returns email of user if exists
}else{
$query = "INSERT INTO
$this->userTable(oauth_provider,oauth_uid,fname,lname,email,location,country,picture_url,profile_url,created,modified)
VALUES('linkedin','".$userdata->id."','".$userdata->firstName."','".$userdata->lastName."','".$userdata->emailAddress."','".$userdata->location->name."','".$userdata->location->country->code."','".$userdata->pictureUrl."','".$userdata->publicProfileUrl."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
$this->db->query($query);
$id = $this->db->insert_id;
$check = $this->db->query("SELECT * FROM $this->userTable WHERE oauth_uid = '".$id."'");
if(mysqli_num_rows($check) > 0){
$result = $check->fetch_array(MYSQLI_ASSOC);
return $result['id']; //this doesnt work. It inserts the data into database but doesn't return anything.
}
}
}
The if statement works properly. It checks if user exists then returns the email.
The else part works partially. It inserts the user into database but does not return the email
how can i make this work.
Thanks.
Your code says you want to return the user id and not the user email, if so
Use
return $check->insert_id;
Instead of the if(mysqli_num_rows($check) > 0) block
But if it is the user email you intend to get, then just return it from the userdata object
return $userdata->emailAddress;
Hope it helps
I'm kind of a noob so I don't know if I'm missing something small. The images are uploaded to the database just fine, but using this just gets me a blank square. I don't know if I'm missing something obvious?
$name = $_SESSION['username'];
$sql="SELECT pic FROM userinfo WHERE username = '$name'";
$result = mysqli_query($link,$sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$pic = base64_encode($row['pic']);
echo "<img height='300' width='300' style='padding-left:10px;' src='data:image/jpeg;base64,".$pic."'>";
}
}
The upload code, I'm 95% sure that the issue is somewhere in the upload process. I commented out what I was previously using to send the query.
$name = $_SESSION['username'];
$bio = $_POST['bio'];
$sql="UPDATE userinfo SET pic='$pic' WHERE username='$name'";
$stmt = $link->prepare($sql);
$stmt->bind_param("b", $pic);
$pic = base64_encode($_FILES['fileToUpload']['tmp_name']);
//$query = mysqli_query($link,$sql);
$stmt->execute();
im using php to insert into the review table.ive given the variables $email, $starcount, $bookid fixed values for now just to test the file. the $res query checks to see if there is a row with that book id and email in it. if theres not The $sql query inserts it, and then the $nex query loops through taking any starcount columns where the book column = $book.
if i change the the email at the top of the file it should insert into the new info database and pull out the new and existing starcount, but it does not post, it just returns the already existing starcount. i dont understand why its not working .... im using the array to return to my file.
<?php
mysql_connect("localhost","root","");
mysql_select_db("FunReads");
$email = "sd";
$starcount = "2";
$bookid = "5";
$res = mysql_query("SELECT * FROM Review WHERE book_id='$bookid' AND user_email='$email'");
if (mysql_num_rows($res) != 0) {
$array[]= array("starcount" => "already entered");
} else {
$sql = mysql_query("INSERT INTO Review(book_id,starcount,user_email) values('.$bookid.','.$starcount.','.$email')");
$nex = mysql_query("SELECT * FROM Review WHERE book_id='$bookid'");
while($row = mysql_fetch_array($nex)){
$star = $row["starcount"];
$array[] = array("starcount" => $star);
}
}
echo json_encode($array);
//echo "[{"name":"n1","city":"NY"},{"name":"n2","city":"Paris"}, ...]
?>
It seems to me "book_id" in "Review" table is primary key, as you have tried to add it multiple time, system shows the error duplicate key. Check the error & post it. Also check whether insert query is working or not.
you should not pass the primary key value manually
try this it will helps you
<?php
mysql_connect("localhost","root","");
mysql_select_db("FunReads");
$starcount="2";
$email = "vinodh#gmail.com";
$res=mysql_query("SELECT * FROM Review WHERE email ='$email'");
if(mysql_num_rows($res)!=0){
$array[]= array("starcount" => "already entered");
}else{
$sql=mysql_query("INSERT INTO Review (starcount,email) values('.$starcount.','.$email')");
$nex=mysql_query("SELECT * FROM Review WHERE email='$email'");
while($row=mysql_fetch_array($nex)){
$star = $row["starcount"];
$array[] = array("starcount" => $star);
}
}
echo json_encode($array);
?>
I just updated your code and it is working fine for me.
<?php
mysql_connect("localhost","user","");
mysql_select_db("xyz");
$email = "hari#gmail.com";
$starcount = "2";
$bookid = "5";
$sql = "SELECT * FROM review WHERE book_id='$bookid' AND user_email='$email'";
$res = mysql_query($sql);
if (mysql_num_rows($res) != 0) {
$array[]= array("starcount" => "already entered");
} else {
$sql = "INSERT INTO review(book_id,starcount,user_email) values('$bookid','$starcount','$email')";
$sql = mysql_query($sql);
$nex = mysql_query("SELECT * FROM review WHERE book_id='$bookid'");
while($row = mysql_fetch_array($nex)){
$star = $row["starcount"];
$array[] = array("starcount" => $star);
}
}
echo json_encode($array);
sample output :
[{"starcount":"2"},{"starcount":"3"},{"starcount":"1"},{"starcount":"2"},{"starcount":"1"}]
I updated the insert query, please try to update the same and test.
I want to get the database picture path so that i can used it and store the same file-name path in the image folder so both can be matched up. Actually i am using a unique-id so that pictures have unique names but don't know how to use it rightly. Any help would be appreciated.
$insert = "UPDATE USER_LOGIN SET PICTURE = '".uniqid().$_FILES['file']['name']."' WHERE USERNAME = '".$_COOKIE['username']."'";
$result = oci_parse($con, $insert);
// Executes a statement.
$check = oci_execute($result);
$UploadDirectory = '/wamp/www/img/Users/Users/'.$row['PICTURE'];
Working Solution:
if($row)
{
$Filename = uniqid().$_FILES['file']['name'];
$DirectoryPath = '/wamp/www/img/Users/Users/'.$Filename;
if(move_uploaded_file($_FILES['file']['tmp_name'], $DirectoryPath))
{
$insert = "UPDATE USER_LOGIN SET PICTURE = '".$Filename."' WHERE USERNAME = '".$_COOKIE['username']."'";
$result = oci_parse($con, $insert);
// Executes a statement.
$check = oci_execute($result);
if($check)
{
echo "Saved";
// Commit the changes to the table.
oci_commit($con);
}
else
{
// Rollback changes to table.
oci_rollback($con);
}
}
else
{
//die('error uploading File!');
}
}