In my project I have on a page a form to collect data that will be inserted in the database and in a folder of the server. My problem is that in this form I have a support for uploading images. Then in the PHP part to move this information to the database and to the server folder the file I have arranged to do this job is not working.
Here's my form:
<form action="insert.php" method="post" enctype="multipart/form-data">
Nome do produto:<br>
<p><input type="text" name="NomeProduto" value="" placeholder="Username"></p>
<br>Escolha a categoria:<br>
<input type=text name="Categoria" list=browsers >
<datalist id=browsers >
<?php
//este php serve so para mostrar as categorias disponiveis na base de dados nesta datalist
include 'ligarbd.php';
$namesql = "SELECT nomeCategoria FROM categorias";
if($namereg = mysql_query($namesql))
{
while($name = mysql_fetch_array($namereg)){
echo'<option> '.$name['nomeCategoria'];
}}
?>
</datalist>
<br><br>
Selecione a imagem:<br>
<input type="file" name="uploadFile" id="fileToUpload"><br>
<br><br><p class="submit"><input type="submit" name="commit" value="Concluir"></p>
</form>
Here is my insert.php file:
<?php
$dbhost = 'dados pessoais';
$dbuser = 'dados pessoais';
$dbpass = 'dados pessoais';
$db_name = 'dados pessoais';
$ftp_user = 'dados pessoais';
$ftp_pass = 'dados pessoais';
$ftp_server = "dados pessoais";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name")or die("cannot select DB");
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
// login with username and password
$login_result = ftp_login($ftp_conn, $ftp_user, $ftp_pass);
// check connection
if ((!$ftp_conn) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user";
}
$NomeProduto= $_POST['NomeProduto'];
$Categoria= $_POST['Categoria'];
$uploadDir = 'img/produtos/padaria/';
$uploadDir2 = 'img/produtos/pastelaria/';
$target_file = $uploadDir . basename($_FILES["uploadFile"]["name"]);
$target_file2 = $uploadDir2 . basename($_FILES["uploadFile"]["name"]);
//se a categoria for 2 vai para a pasta da padaria, se não vai para a pasta pastelaria (categoria=1)
if($Categoria == 2) {
if (move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $target_file)) {
// If file has uploaded successfully, store its name in data base
$sql ="INSERT INTO produto (NomeP,Codcategoria) VALUES ('".$NomeProduto."','"$Categoria."')";
if(mysql_query($query_image))
{
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
}
else
{
echo 'File name not stored in database';
}
}}
else if (move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $target_file2)) {
// If file has uploaded successfully, store its name in data base
$sql ="INSERT INTO produto (NomeP,Codcategoria) VALUES ('".$NomeProduto."','"$Categoria."')";
if(mysql_query($query_image))
{
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
}
else
{
echo 'File name not stored in database';
}
}
?>