【问题标题】:Insert query failed MySQL error-works in phpmyadmin-help插入查询失败 MySQL 错误-在 phpmyadmin-help 中有效
【发布时间】:2011-08-08 16:09:19
【问题描述】:

我从下面的 sn-p 收到“错误,插入查询失败”。然而,当进入 phpmyadmin 时,这条线完美运行INSERT INTO ****** (time, ad1) VALUES ('$FileName','www.hats.com')

所以,问题不在于数据库参数,我仔细检查了代码中的连接设置。相关循环的 $countforme 不超过 6 或 15。任何cmets或建议都非常欢迎。

 <html>
<body>

<?php

error_reporting(E_ALL);
set_time_limit (0);

$urls=explode("\n", $_POST['url']);
//$proxies=explode("\n", $_POST['proxy']);

$target=$_POST['target'];

$allurls=count($urls);
//$allproxies=count($proxies);

//use the new tool box
require "ToolBoxA4.php";
echo "before loops all ok";

for ( $counter = 0; $counter < $allurls; $counter++) {
//for ( $count = 0; $count <= $allproxies; $count++) {


 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL,$urls[$counter]);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
 curl_setopt ($ch, CURLOPT_HEADER, 1); 
 curl_exec ($ch); 
 $curl_scraped_page=curl_exec($ch); 

echo "after curl ok";

//call the new function parseA1
$arrOut = parseA1 ($curl_scraped_page);


$curl_scraped_page=strtolower($curl_scraped_page);
$haystack=$curl_scraped_page;
if (strlen(strstr($haystack,$target))>0) {



$FileName = abs(rand(0,100000));
$FileHandle = fopen($FileName, 'w') or die("can't open file");
fwrite($FileHandle, $curl_scraped_page);

echo "after fwrite ok";



$hostname="********";
$username="****";
$password="*****";
$dbname="****";
$usertable="******";

$con=mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname ,$con);

$right = explode(",", $arrOut[0]);
$top = explode(",", $arrOut[1]);
echo count($top);
for ( $countforme = 0; $countforme < 6; $countforme++) {
//$topnow=$top[$countforme];
//echo '$topnow';

$query = "INSERT INTO happyturtle (time, ad1) VALUES ('$FileName','www.hats.com')";
mysql_query($query) or die('Error, insert query failed');

echo "after mysql ok";

}

for ( $countforme = 0; $countforme <= 15; $countforme++) {

//$rightnow = $right[$countforme];


$query = "INSERT INTO happyturtle (time, ad1) VALUES ('$FileName','$right[$countforme]')";
mysql_query($query) or die('Error, insert query failed');

}

mysql_close($con);


echo '$FileNameSQL';


fclose($FileHandle);
}
curl_close($ch);

}



?>

</body>
</html>

作为参考,下面是toolboxa4.php

<?php

function strTrim ($strIn, $cutA, $cutB){
    //keeps what is between $cutA and $cutB
    $pieces = explode($cutA, $strIn, 2);
    $str1 = $pieces[1];  //keep everything after cutA 

    $pieces = explode($cutB, $str1, 2);
    $strOut = $pieces[0];  //keep everything before cutB            
    return $strOut;
}

function arrWords ($strIn, $theStart, $theEnd){
    //returns what is between $theStart and $theEnd
    $cutA = $theStart;
    $pieces = explode($cutA, $strIn);
    $pieces[0] = "";  //discard the first piece

    $cutB = $theEnd;
    foreach ($pieces as $key => $value) {
        $arrB = explode($cutB, $value, 2);
        $arrOut[$key] = $arrB[0];  //keep everything before cutB        
    }

    return $arrOut;
}

function arrElems ($strIn, $tag){
    //returns what is between <$tag> and </$tag>
    $cutA = "<$tag>";
    $pieces = explode($cutA, $strIn);
    $pieces[0] = "";  //discard the first piece

    $cutB = "</$tag>";
    foreach ($pieces as $key => $value) {
        $arrB = explode($cutB, $value, 2);
        $arrOut[$key] = $arrB[0];  //keep everything before cutB        
    }

    return $arrOut;
}

function arrElemAB ($strIn, $tagA, $tagB){
    //returns what is between <$tagA><$tagB> and </$tagB></$tagA>
    $cutA = "<$tagA><$tagB>";
    $pieces = explode($cutA, $strIn);
    $pieces[0] = "";  //discard the first piece

    $cutB = "</$tagB></$tagA>";
    foreach ($pieces as $key => $value) {
        $arrB = explode($cutB, $value, 2);
        $arrOut[$key] = $arrB[0];  //keep everything before cutB        
    }

    return $arrOut;
}

function DropTag ($strIn, $tag){
    //drops <$tag> and </$tag>.  $strIn can be a string or an array of strings.  $strOut is same type as $strIn.
    $strA = str_replace("<$tag>", "", $strIn);
    $strOut = str_replace("</$tag>", "", $strA);
    return $strOut;
}

function arrCompress ($arrIn){
    //deletes empty array entries
    if(!empty($arrIn)){
        foreach ($arrIn as $key => $value) {
            if ($value == "") {
                unset($arrIn[$key]);
            }       
        }
        $arrIn = array_values($arrIn);  
    }   
    return $arrIn;  
}

function arrDeDup ($arrIn){
    //changes duplicate array entries to empty string
    if(!empty($arrIn)){
        foreach ($arrIn as $key1 => $value1) {
            foreach ($arrIn as $key2 => $value2) {
                if ($key1 != $key2 and $value1 == $value2) {
                    $arrIn[$key2] = "";
                }   
            }
        }
    }   
    return $arrIn;  
}

function arrToString ($arrIn){
    //turns an array into a string
    $strOut = "";
    if(!empty($arrIn)){
        foreach ($arrIn as $value) {
            $strOut .= "$value<br>";                
        }
    }
    return $strOut; 
}

function arrContains ($arrIn, $strIn){
    //deletes array entries that do not contain $strIn.
    foreach ($arrIn as &$value) {
        if (stripos($value, $strIn) === false) {
            $value = "";
        }       
    }
    return $arrIn;
}

function arrNotContain ($arrIn, $strIn){
    //deletes array entries that contain $strIn.
    foreach ($arrIn as &$value) {
        if (stripos($value, $strIn) !== false) {
            $value = "";
        }       
    }
    return $arrIn;
}

function parseStrA1 ($strToParse){  
    //drop some tags
    $strA = DropTag ($strToParse, "b");

    //split the file into Top and RHS
    $strTOP = strTrim ($strA, "<body", "<div id=rhs_block");
    $strRHS = strTrim ($strA, "<div id=rhs_block", ">See your ad here");

    //get the elements using tags
    $arrTOP = arrElemAB ($strTOP, "span", "cite");
    $arrRHS = arrElems ($strRHS, "cite");  

    //remove empty elements
    $arrTOP = arrCompress ($arrTOP);
    $arrRHS = arrCompress ($arrRHS);

    //output results
    $strA = "TOP<br>" 
        . arrToString ($arrTOP) 
        . "<br><br>RHS<br>"
        . arrToString ($arrRHS);
    return $strA;
}

function parseA1 ($strToParse){ 
    //drop some tags
    $strA = DropTag ($strToParse, "b");

    //split the file into Top and RHS
    $strTOP = strTrim ($strA, "<body", "<div id=rhs_block");
    $strNAT = strTrim ($strA, "<body", "<div id=rhs_block");
    $strRHS = strTrim ($strA, "<div id=rhs_block", ">See your ad here");

    //get the elements using tags
    $arrTOP = arrElemAB ($strTOP, "span", "cite");
    $arrRHS = arrElems ($strRHS, "cite");

    //get the links
    $arrNAT = arrWords ($strNAT, '<a href="http://', '" ');
    $arrNAT = arrNotContain ($arrNAT, "oogle");

    //remove empty and duplicate elements
    $arrTOP = arrCompress ($arrTOP);
    $arrRHS = arrCompress ($arrRHS); 
    $arrNAT = arrDeDup ($arrNAT);
    $arrNAT = arrCompress ($arrNAT);

    //combine arrays into strings, comma separated values
    $arrOut[0] = implode(",", $arrRHS);
    $arrOut[1] = implode(",", $arrTOP);
    $arrOut[2] = implode(",", $arrNAT); 

    return $arrOut; 
}

?>      

此代码之前已发布,但由于旧问题已解决,这是一个新错误,它是新的。

【问题讨论】:

  • 使用mysql_error()输出实际的mySQL错误信息
  • 您应该执行 INSERT INTO ****** (time, ad1) VALUES ($FileName,'www.hats.com') 而不是 INSERT INTO ****** (time, ad1) VALUES ('$FileName','www.hats.com') 如果您想在变量文件名中插入值而不是文字 $filename。这不是答案,但我只是注意到了。
  • 您可以尝试返回 mysql 错误而不是静态文本:php.net/manual/de/function.mysql-error.php
  • @Brahadeesh:查询位于双引号字符串中。 sql 中的字符串需要在内部使用单引号。
  • mysql_error() 的错误是 Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/53/7382753/html /Summer/wootsummer.php 在第 72 行 @Pekka

标签: php mysql curl insert


【解决方案1】:
INSERT INTO ****** (time, ad1) VALUES ('$FileName','www.hats.com')

应该是这样的

$sql = "INSERT INTO ****** (time, ad1) VALUES ('".$FileName."','www.hats.com')";

注意引号

还有一个名为 time 的字段,我希望它只能排除整数或时间值,但这可能是我的解释,你应该阅读 this article 来命名字段。

【讨论】:

  • 感谢您的建议 - 已修复!但不幸的是,这并没有导致错误。 @DonSeba
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-21
  • 2014-11-01
  • 2019-11-03
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多