C
C
cvbcvb cvbcvbcvbcvb2015-06-20 11:32:24
PHP
cvbcvb cvbcvbcvbcvb, 2015-06-20 11:32:24

How to upload and change the file name?

Hi all.
I need to make a form in which the user enters his name and selects a photo, clicks on a button and this data is entered into a MySQL database, into a table with columns: id, name, img. The image itself is saved to the authors folder, which is located at the root of the server. So, the file should be saved with a name in the id-name format. Tobish, take the current id from the database and put the name of the image as on the user's computer. Here is my code:

<?php

  $connect = mysql_connect('localhost', 'ualex', 'XD2phMET') or die(mysql_error());
  mysql_select_db('testdb1');

  if(isset($_POST['submit'])){
    $name = ($_POST['username']);
    $filename = ($_FILES['filename']['name']);
    $id = mysql_query("SELECT id FROM authors")or die(mysql_error());

    if(empty($filename)){
      die('Upload tour image');
    }elseif (empty($name)) {
      die('Enter your name');
    }else{
      move_uploaded_file($_FILES['filename']['tmp_name'], "/authors".$filename);
      $query = mysql_query("INSERT INTO authors VALUES('','$name','$id-$filename')")or die(mysql_error());
    }
  }

?>


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>TEST</title>

  <link rel="stylesheet" href="/libs/bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" href="/style.css">
</head>
<body>

  <h1>Регистрация</h1>
  <div class="form_cont">
    <form method="post" enctype="multipart/form-data">
  <div class="form-group">
    <label for="name_inp">Ваше имя</label>
    <input type="text" name="username" class="form-control" id="name_inp" placeholder="Ваше имя">
  </div>
  <div class="form-group">
    <label for="file_inp">Ваше изображение</label>
    <input type="file" name="filename" size="9" id="file_inp">
  </div>
  <button type="submit" name="submit" class="btn btn-default">Добавить</button>
</form>
  </div>


<script src="/libs/jquery/jquery-1.11.2.min.js"></script>
<script src="/libs/bootstrap/js/bootstrap.min.js"></script>	
</body>
</html>

help me please!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mykola, 2015-06-20
@iSensetivity

$id = mysql_query("SELECT id FROM authors")or die(mysql_error());

You make a selection not of 1 id, but of several.

A
Archakov Dennis, 2015-06-20
@archakov06

}else{
      move_uploaded_file($_FILES['filename']['tmp_name'], "/authors".$filename);
      $query = mysql_query("INSERT INTO authors VALUES('','$name','$id-$filename')")or die(mysql_error());
    }

Your request is incorrectly composed here, namely, you did not specify the fields. Example:
INSERT INTO authors (field1, field2, field3) VALUES('test1','$name','$id-$filename')
"
Specify which fields you need, and then drive values ​​into them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question