UPDATE表语句中的PHP语法错误

时间:2016-01-16 10:04:56

标签: php mysql database syntax-error

我正在尝试使用来自sourcelocationtable(LocationID),imagetable(ImageID),itemtypetable(ItemTypeID)和donatortable(DonatorID)的外键值更新名为mineraltable的表(其主键名为ItemID)。

我希望用户能够从下拉选择框中选择位置,图像,项目类型和捐赠者值,将值存储在变量中,然后使用显示在值中的值的外键编号更新矿化表。下拉选择框。

后4个表与矿物表的关系是1-Many因此我不能使用连接表来保存它们必须放在矿物表中的外键。

尝试运行以下sql代码后

UPDATE mineraltable SET LocationID='160',ItemTypeID='1',ImageID='6',    DonatorID='4' WHERE ItemID='372' 

这是转换为PHP格式,我的php变量替换为数值。

$sql = "UPDATE mineraltable\n"
    . "SET LocationID=\'$LocationID\', ItemTypeID=\'$ItemTypeID\', ImageID=\'$ImageID\', DonatorID=\'$DonatorID\'\n"
    . "WHERE ItemID=\'ItemID\'" 

我发现写入我的本地服务器xampp的带有数值的sql代码成功运行并更新了矿物表中的外键值,但是当我在我的Web浏览器中运行此代码的php版本时,我得到错误:< / p>

  

“您的SQL语法有错误;请查看手册   对应于您的MariaDB服务器版本,以获得正确的语法   '\'166 \'附近,ItemTypeID = \'6 \',ImageID = \'11 \',DonatorID = \'4 \'WHERE   ItemID = \'371 \''在第2行“

我已经在StackOverFlow上查看了另一种更新外键的方法我发现你可以暂时删除外键来执行代码,然后重新应用外键。但不建议这样做。

我多次查看代码,看不到任何错误。有人可以告诉我,因为我是新来的PHP编码,其中导致语法错误?任何建设性的答案都非常赞赏。

我已按照How to update foreign key value in mysql database中的答案获取UPDATE语句代码。但也看了看 [1]:Syntax error in update statement解决问题,但后一个链接中的示例与我的不相似。

这是整个输入表单的php代码。     

   $debugMode = true;

   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = '';
   $dbname = 'rockandmineraldb';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass,$dbname);

   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }

   echo 'Connected successfully';


   $sql = 'SELECT LocationID,Site,Region,Country,Continent FROM sourcelocationtable';
   mysql_select_db('rockandmineraldb');
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }

$SiteOptionData="";
   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
         $LocationID = $row['LocationID']; 
         $Site = $row['Site'];
         $Region = $row['Region'];
         $Country = $row['Country'];
         $Continent = $row['Continent'];

       $SiteOptionData .= "<option value-\"$LocationID\">$Site $Region $Country $Continent</option>";

}




   $sql = 'SELECT DonatorID,DonatorFN,DonatorLN FROM donatortable';
   mysql_select_db('rockandmineraldb');
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }

$DonatorOptionData="";
   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
         $DonatorID = $row['DonatorID']; 
         $DonatorFN = $row['DonatorFN'];
         $DonatorLN = $row['DonatorLN'];


       $DonatorOptionData .= "<option value-\"$DonatorID\">$DonatorFN $DonatorLN</option>";

}


   $sql = 'SELECT ItemTypeID,ItemType FROM itemtypetable';
   mysql_select_db('rockandmineraldb');
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }

$ItemTypeOptionData="";
   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
         $ItemTypeID = $row['ItemTypeID']; 
         $ItemType = $row['ItemType'];



       $ItemTypeOptionData .= "<option value-\"$ItemTypeID\">$ItemType</option>";

}

   $sql = 'SELECT ImageID,Image FROM imagetable';
   mysql_select_db('rockandmineraldb');
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }

$ImageOptionData="";
   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
         $ImageID = $row['ImageID']; 
         $Image = $row['Image'];

       $ImageOptionData .= "<option value-\"$ImageID\">$Image</option>";

}

 $sql = 'SELECT ItemID,TrayBoxNo,ItemInBox,Name FROM mineraltable';
   mysql_select_db('rockandmineraldb');
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }

$ItemOptionData="";
   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
         $ItemID = $row['ItemID']; 
         $TrayBoxNo = $row['TrayBoxNo'];
         $ItemInBox = $row['ItemInBox'];
         $Name = $row['Name'];



       $ItemOptionData .= "<option value-\"$ItemID\">$TrayBoxNo,$ItemInBox,$Name</option>";

}
   mysql_free_result($retval);
   echo "Fetched data successfully\n";

if(isset($_POST['Item'])){ $ItemID== $_POST['Item']; } 
if(isset($_POST['Location'])){ $LocationID = $_POST['Location']; } 
if(isset($_POST['ItemType'])){ $ItemTypeID = $_POST['ItemType']; } 
if(isset($_POST['Image'])){ $ImageID = $_POST['Image']; } 
if(isset($_POST['Donator'])){ $DonatorID = $_POST['Donator']; } 


if(isset)

$sql = "UPDATE mineraltable\n"
    . "SET LocationID=\'$LocationID\', ItemTypeID=\'$ItemTypeID\', ImageID=\'$ImageID\', DonatorID=\'$DonatorID\'\n"
    . "WHERE ItemID=\'$ItemID\'";


            mysql_select_db('rockandmineraldb');
            $retval = mysql_query( $sql, $conn );

            if(! $retval ) {
               die('Could not update data: ' . mysql_error());
            }
            echo "Updated data successfully\n";

            mysql_close($conn);


?>



  <form method = "post" action = "<?php $_PHP_SELF ?>">
<table>
 <tr>
                    <td>Select Site</td>
                    <td>
                        <select size="10" name="Location" multiple="multiple" id="Location">


                            <?php echo $SiteOptionData; ?>

                        </select>
                    </td>
                </tr>

    <br>


                <tr>
    <td>Select Donator</td>
                    <td>
                        <select size="10" name="Donator" multiple="multiple" id="Donator">


                            <?php echo $DonatorOptionData; ?>

                        </select>
                    </td>
                </tr>

<br>

                <tr>
    <td>Select ItemType</td>
                    <td>
                        <select size="10" name="ItemType" multiple="multiple" id="ItemType">


                            <?php echo $ItemTypeOptionData; ?>

                        </select>
                    </td>
                </tr>

<br>

                <tr>
    <td>Select Image</td>
                    <td>


                        <select size="10" name="Image" multiple="multiple" id="Image">


                        <?php echo $ImageOptionData; ?> 

                        </select>
                    </td>
                </tr>

                <tr>

    <td>Select Item</td>
                    <td>
                        <select size="10" name="Item" multiple="multiple" id="Item">


                            <?php echo $ItemOptionData; ?>

                        </select>
                    </td>
                </tr>


</table>

  <input name="update" type="submit" id="update" value="update">

</form>

3 个答案:

答案 0 :(得分:1)

试试这个:

<textarea id="editor"></textarea>
<br/>
<a id="download" href="" download="file.csv">Download</a>

你使用双引号,所以你不应该单引号。

答案 1 :(得分:0)

在您的查询中

而不是

Image image("Image_Path");   

int width = image.size().width();
int height = image.size().height();

IplImage* mat = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
image.write(0, 0, width, height, "BGR", Magick::CharPixel, (char*)mat->imageData);

cvShowImage("image", mat);

使用

"UPDATE mineraltable\n"
    . "SET LocationID=\'$LocationID\', ItemTypeID=\'$ItemTypeID\', ImageID=\'$ImageID\', DonatorID=\'$DonatorID\'\n"
    . "WHERE ItemID=\'$ItemID\'"

答案 2 :(得分:0)

 $sql = "UPDATE mineraltable
        SET LocationID='$LocationID', ItemTypeID='$ItemTypeID', ImageID='$ImageID', 
     DonatorID='$DonatorID' WHERE ItemID='ItemID' ";

请从查询中删除\nbackslash。这不是必需的。