Jump to content

Archived

This topic is now archived and is closed to further replies.

josephbupe

Can't insert into mysql table

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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