简体   繁体   中英

PHP and mysql adding one value from table row to a variable

So i have a problem i'm racking my brain about for days. We have a project where we are making an internet store, and i got to the part where i need to put the products in a database. We need to put a few fields that are inputed by a form via the admin and the inserted picture needs to be moved the the store folder and its name should correspond to the product ID from the products table. In the database should be the image url so we can link it to the store page.

I have major problems here because i can't access the database and copy the ID of a product to add to the image. My table also outputs nothing, but i blame that on bad wamp settings on my old computer since new one is being repaired.

<?php 
session_start();
if (isset($_POST['unos'])){
$veza=new mysqli("localhost","G03","sifra","internetprodavnica");
mysqli_set_charset($veza,"utf8");
$naziv=$_POST['ime'];
$kratakopis=$_POST['kratakopis'];
$dugiopis=$_POST['dugiopis'];
$cena=$_POST['cena'];
$kategorija=$_POST['kategorija'];


$Id=mysqli_query("SELECT IDProizvoda FROM products ORDER BY IDProizvoda DESC LIMIT 1");


if ($_FILES['slika']['type'] = 'image/jpg'){ $type=".jpg"; }
if ($_FILES['slika']['type'] = 'image/jpeg'){ $type=".jpeg"; }
$path= "slike/".$Id.$type;
  if (isset($_FILES['slika'])) {
     if(is_uploaded_file($_FILES['slika']['tmp_name'])) {
      $rezultat=move_uploaded_file($_FILES['slika']['tmp_name'], $path); }}

$upit="INSERT INTO internetprodavnica.proizvodi(ime,kratakopis,dugiopis,cena,kategorija,slika) VALUES ('$naziv','$kratakopis','$dugiopis','$cena','$kategorija','$path')";
$rez=$veza->query($upit); }

?>

Sorry if its foreign language, i don't thing not translating all the variable names will have an impact on your understanding of my code.

Variable path is the images url name. Folder slike(meaning images) and added id and extension type. I wanted to check the type but evidently it just changes the type to that of the last if question,it doesn't particularly bother me, i still either copies the image with blank name or outputs an error. There in an error anyway, but the image often copies regardless.

If i remove the mysqli_query image name becomes

SELECT IDProizvoda FROM proizvodi ORDER BY IDProizvoda DESC LIMIT 1

I think the problem is with the line

$Id=mysqli_query("SELECT IDProizvoda FROM products ORDER BY IDProizvoda DESC LIMIT 1");

Help is greatly appreciated since no matter when i try it doesn't work.

You need to put your connection first:

$Id=mysqli_query($veza, "SELECT IDProizvoda FROM products ORDER BY IDProizvoda DESC LIMIT 1");

Or you can access as Object, with:

$Id=$veza->query("SELECT IDProizvoda FROM products ORDER BY IDProizvoda DESC LIMIT 1");

http://php.net/manual/en/mysqli.query.php

In your case, you need to fetch data to use enxt:

$query=$veza->query("SELECT IDProizvoda FROM products ORDER BY IDProizvoda DESC LIMIT 1");
$ID=$query->fetch_object()->IDProizvoda;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM