Jump to content

Can't insert into mysql table


josephbupe

Recommended Posts

Hi,

 

The following code was written by someone else. It allows me to upload images to a directory while saving image name in the mysql table.

 

I also want the code to allow me save other data (surname, first name) along with the image name into the table, but my try is not working, only the images get uploaded.

 

What am I missing here?

if(isset($_POST['upload']))
{

$path=$path.$_FILES['file_upload']['name'];

if(move_uploaded_file($_FILES['file_upload']['tmp_name'],$path))
{
echo " ".basename($_FILES['file_upload']['name'])." has been uploaded<br/>";
echo '<img src="gallery/'.$_FILES['file_upload']['name'].'" width="48" height="48"/>';
$img=$_FILES['file_upload']['name'];
$query="insert into imgtables (fname,imgurl,date) values('$fname',STR_TO_DATE('$dateofbirth','%d-%m-%y'),'$img',now())";
if($sp->query($query)){
echo "<br/>Inserted to DB also"; 
}else{
echo "Error <br/>".$sp->error; 
}
}
else
{
echo "There is an error,please retry or check path";
}
}

?>

joseph

Link to comment
Share on other sites

Ok, I got it working after some modifications:

if(isset($_POST['upload'])){
        $familyname = trim($_POST['familyname']);
        $firstname = trim($_POST['firstname']);
        $othernames = trim($_POST['othernames']);
        $dateofbirth = trim($_POST['$dateofbirth']);
        $img = trim($_POST['imgurl']);

$path=$path.$_FILES['file_upload']['name'];
   
if(move_uploaded_file($_FILES['file_upload']['tmp_name'],$path))
{
echo " ".basename($_FILES['file_upload']['name'])." has been uploaded<br/>";
echo '<img src="gallery/'.$_FILES['file_upload']['name'].'" width="80" height="100"/>';
$img=$_FILES['file_upload']['name'];
    $query="insert into imgtables (familyname,firstname,othernames,dateofbirth,imgurl,date) values('$familyname','$firstname','$othernames',STR_TO_DATE('$dateofbirth','%d-%m-%y'),'$img',now())"; echo($familyname);
    if($sp->query($query)){
     echo "<br/>Inserted to DB also";   
    }else{
        echo "Error <br/>".$sp->error;       
    }

}
else
{
echo "There is an error,please retry or ckeck path";
}
}

I am trying to convert to prepared statement.

 

Regards.

 

Joseph

Edited by josephbupe
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.