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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)