簡體   English   中英

將數據放入數據庫時​​出錯

[英]Error putting data into database

我有以下php代碼:

foreach($html->find('dl[class=movie-info]') as $info) {

   for($i = 0; $i <= 20; $i++) {

    $contenido = $info->find('dt',$i)->plaintext;

    if($contenido == 'A&ntilde;o'){
        $year = utf8_encode($info->find('dd',$i)->plaintext);}}}

(該代碼具有更多的if函數)

還有一個mysql表,我在其中放置了變量的內容。

問題在於$ year內容,我需要將其填充為smallint(5)未簽名。

當我使用

$con = mysqli_connect("localhost","root","tdguchiha","phpbb3");
mysqli_query($con,"INSERT INTO pablo (forum_id, calidad, titulo, caratula, sinopsis, pais, director, reparto, genero) VALUES ('$forum_id', '$calidad', '$titulo', '$img', '$sinopsis', '$pais', '$director', '$reparto', '$genero')");
mysqli_close($con);

所有內容均已插入,但是當我嘗試將$year插入類型為smallint(5) unsigned año時,什么也沒發生,所以沒有創建行...

如何將$ year轉換為數字(必須是數字)以在該列中填寫它? 還是我需要更改列類型?

PD:我現在正在學習“玩” MySQL

謝謝

也許嘗試將其強制轉換為整數,而不是使用utf8_encode

$year = intval($info->find('dd',$i)->plaintext);

或者,除此之外:

$year = intval(utf8_encode($info->find('dd',$i)->plaintext));

能給我們一個例子的數據嗎?

編輯:Damien是正確的,intval()不應有所作為。

嘗試回顯內容,並確保它是字符串中的實際數字以進行一些調試:

if($contenido == 'A&ntilde;o'){
    $year = utf8_encode($info->find('dd',$i)->plaintext);
    echo $year.'<br />';}}}

進行查詢時,在mysql和數字之間沒有區別。 確保在所有列的列表中包括列名:

mysqli_query($con,"INSERT INTO pablo (forum_id, calidad, titulo, caratula, sinopsis, pais, director, reparto, genero, year) VALUES ('$forum_id', '$calidad', '$titulo', '$img', '$sinopsis', '$pais', '$director', '$reparto', '$genero', '$year')");
                                                                                                                       ^notice year the column                                                                                             ^now the actual value

只需用mysql列的名稱替換列部分中的year。

在將問題發布到StackOverflow之前,請確定所有錯誤代碼/錯誤消息,並將其與您的問題一起發布

/* check connection */
if ( mysqli_connect_errno() ) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

和:

if ( ! mysqli_query( $link, "SELECT ..." )) {
    printf("Errorcode: %d\n", mysqli_errno($link));
}

有關PHP文檔站點的更多信息。

我認為您的問題出在您要插入的值類型和數據上:

A smallint is between -32768 and 32767 signed, or 0 and 65535 unsigned.

The (5) represents the display width of the field - if you will try to put 90000 you will be rejected.

在您的情況下,這應該不是問題(我認為是年值),但請向我們顯示您要插入的數據。

我猜您正在使用simple_html_dom解析某些內容-在將所有數據插入數據庫之前,請確保所有檢索到的數據均符合您的期望。

另外:嘗試將類型更改為int(5),然后告訴我們。

查看您使用的查詢(帶有年份)和數據樣本將更加容易。

對於錯誤記錄和顯示,請使用:

 $execute = mysqli_query($con,"INSERT INTO pablo ( ....your query..... ")
       or
          printf("Errormessage: %s\n", mysqli_error($con)); 

玩得開心

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM