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

时间:2014-04-10 12:49:30

标签: php ajax file-upload

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

只要将对象添加到数据库,一切正常但我希望我上传的图像以数据库中产品的ID命名,图像被本地存储在以后链接到的目录中。记录正在添加到数据库确定,但图像上传器无法正常工作,我在提交表单时遇到以下错误:

我的表格布局如下:

id l name l quantity l description l price


1 l key 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;

&GT;

Anyhelp将不胜感激!

1 个答案:

答案 0 :(得分:1)

你有一个错字,除了注意到

之外我还没有测试过你的脚本

$ querynew - (&#34; SELECT id as&#39; collectid ....

应该是

$ querynew =(&#34; SELECT id as&#39; collectid ...

这就是你的错误,希望它有所帮助