我第一次使用AJAX向我的mysql数据库发送一个简单的查询,这将添加一个“产品”。但是它似乎没有将请求发送到我的数据库。 我有3个文件addProduct.php,这是表单所在的位置,test.js是请求的javascript / ajax,我的addProductTodatabase是查询所在的位置。
(只是在装箱中我搞砸了文件结构)
表单使用此代码..
<form name ="myForm" class = "hidden" id = "addProduct">
<h2 class = "under"> Next step: </h2>
<h4> Fill in the following information </h4>
<label class = "under" > Product Name <input type="text" name="ProductName" id = "productName"> </label>
<label class = "under" > Product category <input type="text" name="ProductCategory" id = "productCat"> </label>
<label class = "under" > Suitable age plus <input type="text" name="ProductAge" id = "productAge"> </label>
<label class = "under" >Add a Product discrition </label>
<textarea id = "productArea" name ="ProductDis" rows="15" cols="100"></textArea>
<label class = "under" > Product Price <input type="text" name="ProductPrice" id = "productPrice"> </label>
<label class = "under" > Number available <input type="text" name="ProductInStock" id = "productAvailable"> </label>
<label class = "under" > Image location <input type ="file" name= "ImageLocation" id= "imageLocation"> </label>
<input type ="button" onclick = "queryTest()" value = "Submit to database">
</form>
<div id = "loading">this should update </div>
test.js使用此代码..
function queryTest()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('loading').innerHTML = "Please wait...";
//document.getElementById("loading").innerHTML=xmlhttp.responseText;
}
}
//document.getElementById('loading').innerHTML = "Please wait...";
// gets the variables and passes it onto the php page in the url bar
var productName = document.getElementById('productName').value;
var productCat = document.getElementById('productCat').value;
var productAge = document.getElementById('productAge').value;
var productDis = document.getElementById('productArea').value;
var productPrice = document.getElementById('productPrice').value;
var productStock = document.getElementById('productAvailable').value;
var productImage = document.getElementById('imageLocation').value;
var queryString = "?ProductName=" + productName + "&ProductCategory=" + productCat;
queryString += "&ProductAge=" + productAge + "&ProductDis=" + productDis;
queryString += "&ProductPrice=" + productPrice + "&ProductInStock=" + productStock;
queryString += "&ImageLocation=" + productImage;
xmlhttp.open("GET","../mysql/addProductTodatabase.php" + queryString,true);
xmlhttp.send(null);
}
和addProductToDatabase使用此...
<?php
include "databaseLogIn.php";
//get variables from page before
$ProductName = $_GET["ProductName"];
$ProductCategory = $_GET["ProductCategory"];
$ProductAge = $_GET["ProductAge"];
$ProductDis = mysql_real_escape_string($_GET["ProductDis"]);
$ProductPrice = $_GET["ProductPrice"];
$ProductInStock = $_GET["ProductInStock"];
include "/imageHander/imagePointerDownloader.php";
//adds to table
$sql ="INSERT INTO Product ( ProductName, ProductCategory, SuitableAge, ProductDiscription, ProductPrice, ProductAvailable)
VALUES ('$ProductName','$ProductCategory','$ProductAge','$ProductDis','$ProductPrice','$ProductInStock')";
mysql_query($sql,$con);
// gets the product id
$query="SELECT * FROM Product";
$result=mysql_query($query);
$ItemID=mysql_numrows($result);
$PictureCaption = "this is a picture";
// adds picture caption
//adds a image to the table related to the product
$sql="INSERT INTO ProductPictures (ItemID, PictureCaption, ImagePointer)
VALUES ('$ItemID', '$PictureCaption', '$newFile')";
//echo $sql;
mysql_query($sql,$con)
mysql_close($con)
?>
欢迎任何帮助,谢谢你的阅读。
答案 0 :(得分:0)
试试 addProductTodatabase.php :
<?php
include "databaseLogIn.php";
//get variables from page before
$ProductName = mysql_real_escape_string($_GET["ProductName"],$con);
$ProductCategory = mysql_real_escape_string($_GET["ProductCategory"],$con);
$ProductAge = mysql_real_escape_string($_GET["ProductAge"],$con);
$ProductDis = mysql_real_escape_string($_GET["ProductDis"],$con);
$ProductPrice = mysql_real_escape_string($_GET["ProductPrice"],$con);
$ProductInStock = mysql_real_escape_string($_GET["ProductInStock"],$con);
// Check if this is the right path - who knows?
include "./imageHander/imagePointerDownloader.php";
//adds to table
$sql ="INSERT INTO Product ( ProductName, ProductCategory, SuitableAge, ProductDiscription, ProductPrice, ProductAvailable)
VALUES ('$ProductName','$ProductCategory','$ProductAge','$ProductDis','$ProductPrice','$ProductInStock')";
mysql_query($sql,$con);
// gets the product id
$query="SELECT * FROM Product";
$result=mysql_query($query);
$ItemID=mysql_numrows($result);
// adds picture caption
$PictureCaption = "this is a picture";
//adds a image to the table related to the product
$sql="INSERT INTO ProductPictures (ItemID, PictureCaption, ImagePointer)
VALUES ('$ItemID', '$PictureCaption', '$newFile')";
//echo $sql;
// ---
// You were missing a semicolon here.
// ---
mysql_query($sql,$con);
// ---
// You were missing a semicolon also here.
// ---
mysql_close($con);
?>
test.js 的网址错误 addProductTodatabase.php 。它应该是:
xmlhttp.open("GET","./mysql/addProductTodatabase.php" + queryString,true);
路径是相对于html / php文件脚本包含在内。
再加上j0k所说的是对的。 应该 使用MySQLi。 MySQL已被弃用。