我已经写了一个PHP脚本来将数据插入MySQL,对于客户端我正在使用Android,通过使用第一个按钮我上传图像并使用第二个按钮我将图像信息存储到同一个表中(信息如: - PersonName我有大约10个领域。
但我没有将我的数据放在同一行,请参见下面的屏幕截图: -
我想将数据存储在同一行,我需要使用Update Query,但是通过Profession我是Android开发人员,所以在PHP中不强,但我想这对任何PHP开发人员来说都是一个小问题,请参阅我的PHP脚本如下:
<?php
//*** Insert Record ***//
$objConnect = mysql_connect("localhost","root","");
$objDB = mysql_select_db("myDatabase");
/*** Check Username Exists ***/
$strSQL = "SELECT * FROM image_information WHERE ImageName = '".$_FILES["filUpload"]["name"]."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if($objResult)
{
$arr['StatusID'] = "0";
$arr['Message'] = "File Name Exists";
echo json_encode($arr);
exit();
}
$strPersonName = $_POST["sPersonName"];
$strSQL = "INSERT INTO image_information";
$strSQL .="(ImageName, PersonName) VALUES ('".$_FILES["filUpload"]["name"]."','".$strPersonName."')";
$objQuery = mysql_query($strSQL);
if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],"myfile/".$_FILES["filUpload"]["name"]))
if(!$objQuery)
{
$arr['StatusID'] = "0";
$arr['Message'] = "Cannot upload Image(s)";
}
else
{
$arr['StatusID'] = "1";
$arr['Message'] = "Image(s) uploaded Successfully!";
}
mysql_close($objConnect);
echo json_encode($arr);
?>
答案 0 :(得分:1)
Abrahim,
据我所知,你发现了以下问题。
首次上传图片时,上述代码执行时查询为:
"INSERT INTO image_information (ImageName, PersonName) VALUES ('filename.jpg','')" as currently the name is not entered.
第二次输入人名并且此次执行的查询是:
"INSERT INTO image_information (ImageName, PersonName) VALUES ('','person_name')" as image is not present now.
现在为了避免这种情况,您可以执行以下操作
change you code as follows :
/*** Check Username Exists ***/
$strSQL = "SELECT * FROM image_information WHERE ImageName = '".$_FILES["filUpload"]["name"]."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
$strPersonName = $_POST["sPersonName"];
if($objResult)
{
if($strPersonName!='') {
$strSQL = "";
$strSQL = "UPDATE image_information set PersonName = '$strPersonName' where FilesId = ".$objResult->FilesId;
$objQuery = mysql_query($strSQL);
if(!$objQuery) {
$arr['StatusID'] = "0";
$arr['Message'] = "Cannot update person name";
} else {
$arr['StatusID'] = "1";
$arr['Message'] = "Updated person name successfully.";
}
} else {
$arr['StatusID'] = "0";
$arr['Message'] = "File Name Exists";
echo json_encode($arr);
}
} else {
$strSQL = "";
$strSQL = "INSERT INTO image_information (ImageName, PersonName) VALUES ('".$_FILES["filUpload"]["name"]."','".$strPersonName."')";
$objQuery = mysql_query($strSQL);
if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],"myfile/".$_FILES["filUpload"]["name"]))
if(!$objQuery)
{
$arr['StatusID'] = "0";
$arr['Message'] = "Cannot upload Image(s)";
}
else
{
$arr['StatusID'] = "1";
$arr['Message'] = "Image(s) uploaded Successfully!";
}
}
mysql_close($objConnect);
echo json_encode($arr);
?>
我没有检查过你可能需要调整它的某些部分。
希望这会对你有所帮助。
答案 1 :(得分:0)
在表名后添加一个空格
$strSQL = "INSERT INTO image_information ";
$strSQL .="(ImageName, PersonName) VALUES ('".$_FILES["filUpload"["name"]."','".$strPersonName."')";
并确保您的FilesID字段是具有自动增量模式的主键,否则您应该每次都添加它。
在尝试修复代码之前,还要检查您的查询的硬编码版本是否正常工作。测试这个:
$strSQL = "INSERT INTO image_information (ImageName, PersonName) VALUES ('A','B')";
答案 2 :(得分:0)
试试这个......
<?php
//*** Insert Record ***//
$objConnect = mysql_connect("localhost","root","");
$objDB = mysql_select_db("myDatabase");
/*** Check Username Exists ***/
$strSQL = "SELECT * FROM image_information WHERE ImageName = '".$_FILES["filUpload"]["name"]."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if($objResult)
{
$arr['StatusID'] = "0";
$arr['Message'] = "File Name Exists";
echo json_encode($arr);
exit();
}
$strPersonName = $_POST["sPersonName"];
$strSQL = "";
$strSQL = "INSERT INTO image_information (ImageName, PersonName) VALUES ('".$_FILES["filUpload"]["name"]."','".$strPersonName."')";
$objQuery = mysql_query($strSQL);
if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],"myfile/".$_FILES["filUpload"]["name"]))
if(!$objQuery)
{
$arr['StatusID'] = "0";
$arr['Message'] = "Cannot upload Image(s)";
}
else
{
$arr['StatusID'] = "1";
$arr['Message'] = "Image(s) uploaded Successfully!";
}
mysql_close($objConnect);
echo json_encode($arr);
?>
答案 3 :(得分:-1)
希望这会对你有所帮助
$strSQL = "INSERT INTO image_information (ImageName, PersonName) VALUES ('".$_FILES["filUpload"]["name"]."','".$strPersonName."')";
$objQuery = mysql_query($strSQL);