duanchuopi2298 2016-05-23 17:10
浏览 60
已采纳

PHP和mysql将表行中的一个值添加到变量中

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.

  • 写回答

1条回答 默认 最新

  • dtla92562 2016-05-23 17:15
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用