2016-07-30 86 views
1

嘗試插入到mySQL數據庫時,我沒有收到任何錯誤,但在phpmyadmin查詢中沒有顯示任何錯誤。這是我試圖插入的php文件的代碼。我知道這不是最好的辦法,但這是我迄今爲止所做的。用PHP插入mySQL,但沒有在phpmyadmin中顯示

<?php 

require 'steamauth/steamauth.php'; 
require 'php/main.php'; 
require 'php/db.php'; 

$title = $_POST["title"]; 
$desc = $_POST["desc"]; 
$date = $_POST["date"]; 
$skills = $_POST["skills"]; 
$budget = $_POST["budget"]; 
$tc = $_POST["tc"]; 
$steamid = $steamprofile['steamid']; 
$steamname = $steamprofile['personaname']; 
$avatar = $steamprofile['avatarmedium']; 
$dateposted = date("m-d-y"); 


// insert into db 

if(1==1){ 

    $insert = $link->query("INSERT INTO livepost (title, desc, date, skills, budget, tc, steamid, steamname, avatar, dateposted) 
    VALUES ('$title', '$desc', '$date' , '$skills' , '$budget', '$tc', '$steamid', '$steamname', '$avatar', '$dateposted')"); 

    header('Location: index'); 

} else { 

    header('Location: index?insertdidntwork'); 

} 
+0

你怎麼知道有沒有錯誤,是顯示錯誤上?你的代碼在最後輸出了什麼?它重定向,但是這是什麼頁面,就像重定向一樣。你怎麼能重定向到'索引'也許'index.php'或者你是否正在重寫網址。 – ArtisticPhoenix

+0

我有error_reporting打開只是爲了確保我可以看到一個錯誤,如果發生,頁面是空的時候去它,是的,我正在重寫網址。 –

+0

@StenCooley查看您的網絡服務器的錯誤日誌文件。也許你會發現一些輸出 – Jens

回答

0

也許你可以試試這個 ,你應該總是使用$ mysqli->錯誤()函數來知道你有什麼類型的錯誤

<?php 
$title = $_POST["title"]; 
$desc = $_POST["desc"]; 
$date = $_POST["date"]; 
$skills = $_POST["skills"]; 
$budget = $_POST["budget"]; 
$tc = $_POST["tc"]; 
$steamid = $steamprofile['steamid']; 
$steamname = $steamprofile['personaname']; 
$avatar = $steamprofile['avatarmedium']; 
$dateposted = date("m-d-y"); 
// insert into db 
if (1 == 1) { 
    $insert = $link ->query("INSERT INTO livepost (`title`,`desc`,`date`,`skills`,`budget`,`tc`,`steamid`,`steamname`,`avatar`,`dateposted`) 

    VALUES ('".$title."', '".$desc."', '".$date."' , '".$skills."' , '".$budget."', '".$tc."', '".$steamid."', '".$steamname."', '".$avatar."', '".$dateposted."')") or die($mysqli -> error . __LINE__); 
    header('Location: index'); 
} else { 
    header('Location: index?insertdidntwork'); 
} 
?> 
+0

謝謝!這解決了它並將數據插入到數據庫中。 –

+0

@StenCooley您好! – mohammed

3

date和desc在mysql中是keywords。你必須逃離列名:

$insert = $link->query("INSERT INTO livepost (title, `desc`, `date`, skills, budget, tc, steamid, steamname, avatar, dateposted) 
    VALUES ('$title', '$desc', '$date' , '$skills' , '$budget', '$tc', '$steamid', '$steamname', '$avatar', '$dateposted')"); 

提示1:瞭解prepred statemts7

提示2:檢查執行SQL語句後錯誤。提示3:您的if else語句不檢查插入是否有效。

+0

這仍然不起作用。 –

+0

@StenCooley你有任何錯誤信息? – Jens

+0

日期是一個關鍵字... desc是保留字。仍然+1 ...大聲笑 – ArtisticPhoenix

0

@Jens已經指出關於keywords

使用準備陳述。

<?php 
if(1==1){ 

    $insert = $link->prepare("INSERT INTO livepost (title, `desc`, `date`, skills, budget, tc, steamid, steamname, avatar, dateposted) VALUES (?,?,?,?,?,?,?,?,?,?)"); 
    $insert->bind_param("sss",$title,$desc,$date,$skills,$budget,$tc,$steamid,$steamname,$avatar,$dateposted); 
    $insert->execute(); 

    header('Location: index'); 

} else { 

    header('Location: index?insertdidntwork'); 

} 

可能是我認爲問題在這一行。 $dateposted = date("m-d-y");

$dateposted = date("Y-m-d");日期應該是Y-m-d格式。

請嘗試一次這樣。並且,請回復。

+0

它吐出這個錯誤「致命錯誤:未捕獲錯誤:調用成員函數bind_param H:\ Websites \ Xampp \ htdocs \ Freelanceforskins \ post.php:27 Stack trace:#0 {main}拋出H:\ Websites \ Xampp \ htdocs \ Freelanceforskins \ post.php第27行「 –

相關問題