WinInet類比HTTP的POST請求出錯

來源:互聯網
上載者:User
在VS2012裡面設定斷點跟蹤執行,發現請求該php檔案擷取的不是正確的返回字串,而是如下出錯資訊:

Invalid query:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Whole query:
select id,name from index_activities where top=0

這段資訊是我在PHP的
$result = mysql_query($query);

語句行後面,“if”請求不成功才輸出的,顯然是mysql請求沒成功,且可以確定,調用該PHP檔案是成功的。

但是當我在網頁中用同樣的參數調用同一個PHP檔案時(GET方式),傳回值則是正確的。

我php檔案中擷取傳參是用的
$top = $_REQUEST['top'];

所以POST和GET參數應該都可以同樣擷取的。

不知道可能是哪裡出了問題?用WinInet傳參會造成這個請求字串有什麼異常嗎?


回複討論(解決方案)

資料庫連接正確嗎?

資料庫連接正確嗎?


串連正確啊,不正確就返回unable to connect to mysql了,就不會執行到下面的請求,也不會出現MySQL的請求出錯資訊了。

另有一個同樣的類比HTTP請求,也有參數,返回就是正確的:

void CDllValidateDlg::ValidateAPerson(char* Name, char* Code){CString post_data;post_data.Format("userid='%s'&name='%s'",Code,Name); //請求的附加參數CString result; //返回的結果CString post_page = "test_id_validater/validateid.php"; //請求的phpPostHttpPage(result,post_page,post_data);AfxMessageBox(result);}

而我現在調試不明白的這個HTTP請求,到底哪裡不一樣,我把原本由變數決定的參數都寫死了,仍然返回說mysql請求不正確:
void CDllValidateDlg::getActs(HTREEITEM root){CString post_data="top=0";//char top[10];//itoa(ActivitiesTree.GetItemData(root),top,10);//post_data.Format("top=%s",top);CString result;CString post_page = "test_id_validater/GetActivities.php";//AfxMessageBox("post_page:"+post_page+", "+"post_data:"+post_data);PostHttpPage(result, post_page, post_data);AfxMessageBox(result);    ……    ……

感覺好像是PHP端的問題,像下面這麼寫就返回正確了:



原來我寫的代碼是:

Invalid query:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Whole query:
select id,name from index_activities where top=0
這個資訊分明是資料庫報的錯!

如果 $query = "select id,name from index_activities where top=0"; 可以
而 $query = "select id,name from index_activities where top=$top"; 不可以
這就表示 $top 無值或不是數字

$top = $_REQUEST['top']; 改為 $top = intval($_REQUEST['top']); 試試

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.