2013-06-20 128 views
-3

我一直在開發客戶端Facebook頁面的推薦應用程序,她可以編輯自己! 我在將它插入數據庫時​​遇到問題!我已經寫了它的MySQLi福斯特,但現在已經回到基礎,我在這裏使用這個代碼...MySQL沒有選擇數據庫

<?php 
     $testtitle = preg_replace('#[^a-z]#i', '', $_POST['ts_tt']); 
     $testbody = preg_replace('#[^a-z]#i', '', $_POST['ts_tb']); 
     $compowner = preg_replace('#[^a-z]#i', '', $_POST['ts_co']); 
     $ownertitle = preg_replace('#[^a-z]#i', '', $_POST['ts_ot']); 
     $compname = preg_replace('#[^a-z]#i', '', $_POST['ts_cn']); 
     $compwebsite = preg_replace('#[^a-z]#i', '', $_POST['ts_cw']); 

     include_once "../php_includes/db_conx.php"; 

     $sql = "INSERT INTO testimonials (testtitle, testbody, compowner, ownertitle, compname, compwebsite) 
       VALUES ('$testtitle', '$testbody', '$compowner', '$ownertitle', '$compname', '$compwebsite')"; 
     if (!mysql_query($sql, $connection)){ 
      die('Error: ' . mysql_error()); 
    } 
    exit(); 
?> 

這裏是簡單的形式,我使用!而且我知道它在桌子上!我只是想快速簡單地做...

<form method="post" action="testimonial_new_parse.php" onsubmit="return validate_form ();"> 
    <tr> 
    <td width="12%" align="right" bgcolor="#F5E4A9">Testimonial Full Title</td> 
    <td width="88%" bgcolor="#F5E4A9"><input name="ts_tt" type="text" size="80" maxlength="64" /></td> 
    </tr> 
    <tr> 
    <td align="right" valign="top" bgcolor="#DAEAFA">Testimonial Body</td> 
    <td bgcolor="#DAEAFA"><textarea name="ts_tb" cols="60" rows="16"></textarea></td> 
    </tr> 
    <tr> 
    <td align="right" bgcolor="#D7EECC">Company Owner</td> 
    <td bgcolor="#D7EECC"><input name="ts_co" type="text" maxlength="64" size="80" /></td> 
    </tr> 
    <tr> 
    <td align="right" bgcolor="#D7EECC">Owner Title</td> 
    <td bgcolor="#D7EECC"><input name="ts_ot" type="text" maxlength="64" size="80"/></td> 
    </tr> 
    <tr> 
    <td align="right" bgcolor="#D7EECC">Company Name</td> 
    <td bgcolor="#D7EECC"><input name="ts_cn" type="text" maxlength="64" size="80" /></td> 
    </tr> 
    <tr> 
    <td align="right" bgcolor="#D7EECC">Company Website</td> 
    <td bgcolor="#D7EECC"><input name="ts_cw" type="text" maxlength="64" size="80" /></td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    <td><input type="submit" name="ts_button" value="Create this Testimonial now" /></td> 
    </tr> 
    </form> 

問題是我不斷得到的是,Np數據庫已被選中! 任何幫助將不勝感激!

+0

告訴我們你在'../ php_includes/db_conx.php'中有什麼 – budwiser

回答

2

您缺少連接和數據庫選擇。在運行查詢之前應該執行這兩個步驟。

從PHP文件(相應調整):

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); 
if (!$link) { 
    die('Not connected : ' . mysql_error()); 
} 

// make foo the current db 
$db_selected = mysql_select_db('foo', $link); 
if (!$db_selected) { 
    die ('Can\'t use foo : ' . mysql_error()); 
} 
0

這將與你的$連接變量做,你怎麼初始化呢?

正如其他人已經提到的,你需要正確初始化它,並選擇一個數據庫,像這樣:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); 
if (!$link) { 
    die('Not connected : ' . mysql_error()); 
} 

// make foo the current db 
$db_selected = mysql_select_db('foo', $link); 
if (!$db_selected) { 
    die ('Can\'t use foo : ' . mysql_error()); 
} 

還要注意的是,MySQLi擴展優於MySQL的,所以使用,如果你能。

另外,請注意你的HTML代碼不會工作,你的「TR」和「TD」必須是在一個表中,這樣的:

<table border="1"> 
<tr> 
    <td>row 1, cell 1</td> 
    <td>row 1, cell 2</td> 
</tr> 
<tr> 
    <td>row 2, cell 1</td> 
    <td>row 2, cell 2</td> 
</tr> 
</table> 
+0

歡呼Dany是te問題很多謝謝Dany –

0

可能是連接文件是不正確的。檢查連接文件,然後重試。

+0

歡迎來到SO。雖然你的回答是一個很好的努力,但花點時間先研究一下這個問題。你看,這個問題已經過了一年多了,已經回答了,所以OP不太可能再次修改。另外,爲了將來,考慮給出一個更完整的答案,並且如果可能的話附上代碼示例。 另外我有點沒有注意到你的答案和問題之間的任何關係。 –