将数据添加到数据库并使用JSON

用户名

我正在尝试将产品添加到CMS上的数据库中,我想尝试在另一页上显示产品及其图像。

就将对象添加到数据库而言,一切正常,但是我希望上传的图像以数据库中产品的ID命名,该图像被本地存储在目录中,以后将链接至该目录。记录已被添加到数据库中,但是图像上传器无法正常工作,并且在提交表单时出现以下错误:

我的表布局如下所示:

id l名称l数量l描述l价格


1 l钥匙l 5 l解锁东西l 6

注意:未定义的变量:第17行的L:\ xampp \ htdocs \ 655487 \ addSQL.php中的querynew

注意:未定义的变量:第18行的L:\ xampp \ htdocs \ 655487 \ addSQL.php中的querynew

警告:mysqli :: query():第18行的L:\ xampp \ htdocs \ 655487 \ addSQL.php中的空查询

注意:未定义的变量:第18行的L:\ xampp \ htdocs \ 655487 \ addSQL.php中的querynew

这是通过以下方式提交产品的表格:

        <fieldset><legend><span> Add a product to the database </span></legend>

        <form  id ="productsform" method="post" onsubmit="return false;" enctype="multipart/form-data">


        <label> Enter a product name:               <input  type="text"     id="name"           name="name"/>           </label>

        <label> Enter a product quantity:           <input  type="number"   id="quantity"       name="quantity"/>       </label>

        <label> Enter a product description:        <input  type="text"     id="description"    name="description"/>    </label>

        <label> Enter a product price:              <input  type="text"     id="price"          name="price"/>          </label>

        <label> Upload a image of the product:      <input  type="file"     id="file1"          name="file"></label>

        <input id="submit" name="submit" type="button" class="reg" value="Add Product">

        <div id="status"></div>

这是后面的脚本addSQL.php:

 <?php
     include("dbase/config_database.php");

//Stores all information passed through AJAX into the query
$name = $_POST['name'];
$quantity = $_POST['quantity'];
$description = $_POST['description'];
$price = $_POST['price'];


//Adds information to database
$query = "INSERT INTO products (name, quantity, description, price) VALUES ('$name','$quantity','$description','$price')";
//Runs the query
$result = $mysqli->query($query) OR die("Failed query $query");
echo $mysqli->error."<p>";

$querynew - ("SELECT id as 'collectid' from products WHERE name = '$name'and quantity = '$quantity'and description ='$description'and price = '$price'");
$resultnew = $mysqli->query($querynew) OR die("Failed query $querynew");

while($info = mysqli_fetch_array( $resultnew)){
    $productid = $info['collectid'];
}

$image = $_FILES['file1']['name'];
$type = $_FILES['file1']['type'];
$size = $_FILES['file1']['size'];
$tmp_name = $_FILES['file1']['tmp_name'];

$imgpath = "images/".$productid.".jpg";

// Run the move_uploaded_file() function here
$moveResult = move_uploaded_file($tmp_name, $imgpath);
// Evaluate the value returned from the function if needed

$querytwo = ("SELECT * FROM products WHERE name = '$name' and quantity = '$quantity' and description = '$description' and price = '$price'");
$resulttwo = $mysqli ->query($querytwo) OR die ("Failed query $querynew");

$info = array();
while($row = mysqli_fetch_assoc($resulttwo)){
    $product = array("id" => $row ['id'],
        "name" => $row ['name'],
        "quantity" => $row ['quantity'],
        "description" => $row ['description'],
        "price" => $row ['price'],

);

    array_push($info,$product);
}

$json_output = json_encode($info);
echo $json_output;

?>

任何帮助将不胜感激!

丹尼尔

您有错字,除了注意到以下内容,我没有测试过您的脚本

$ querynew-(“ SELECT id as'collectid ....

应该

$ querynew =(“” SELECT id as'collectid ...

这就是您的错误,希望对您有所帮助

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章