2012-12-25 42 views
0

這是我的php代碼如下。出於某種原因,當我希望它連接到數據庫時,它會顯示「沒有選擇數據庫」,但是當我進行連接測試時,它會顯示「數據庫連接成功!」所以在下面的代碼中有一個錯誤。另外我認爲這可能會干擾我的表格,所以我必須有一個錯字;當我刪除PHP的表單正常運行,但我希望它連接到數據庫。無法連接到數據庫?

<?php 
// let's initialize vars to be printed to page in the HTML section so our script does not return errors 
// they must be initialized in some server environments, not shown in video 
$errorMsg = ""; 
$First_Name = ""; 
$Last_Name = ""; 
$Email = ""; 
$Password = ""; 


// This code runs only if the form submit button is pressed 
if (isset ($_POST['firstname'])){ 

    /* Example of cleaning variables in a loop 
    $vars = ""; 
    foreach ($_POST as $key => $value) { 
     $value = stripslashes($value); 
     $vars .= "$key = $value<br />"; 
    } 
    print "$vars"; 
    exit(); 
    */ 
    $First_Name = $_POST['firstname']; 
    $Last_Name = $_POST['lastname']; 
    $Email = $_POST['email']; 
    $Password = $_POST['password']; 

    // Connect to database 
    include "connect_to_mysql.php"; 
    $emailCHecker = mysql_real_escape_string($Email); 
    $emailCHecker = eregi_replace("`", "", $EmailCHecker); 
    // Database duplicate e-mail check setup for use below in the error handling if else conditionals 
    $sql_email_check = mysql_query("SELECT email FROM members WHERE email='$emailCHecker'"); 
    $email_check = mysql_num_rows($sql_email_check); 

} 

    $sql = mysql_query("INSERT INTO members (firstname, lastname, email, password, sign_up_date) 
    VALUES('$First_Name','$Last_Name','$Email','$Password', now())") 
    or die (mysql_error()); 

    $id = mysql_insert_id(); 

    // Create directory(folder) to hold each user's files(pics, MP3s, etc.)   
    mkdir("members/$id", 0755);  

?> 

這是我下面

<form action="index.php" method="post" enctype="multipart/form-data"> 
    <tr> 
    <td width="23%" class="right">First Name:</td> 
    <td width="77%" class="left left_nowrap"><input type="text" class="left left_nowrap tb10" id="First_Name" value="<?php print "$First_Name"; ?>"/></td> 
    </tr> 
    <tr> 
    <td class="right">Last Name:</td> 
    <td class="left left_nowrap"><input type="text" class="left left_nowrap tb10" id="Last_Name" value="<?php print "$Last_Name"; ?>" /> 
    </td> 
    </tr> 
    <tr> 
    <td class="right">Email:</td> 
    <td class="left left_nowrap"><input type="text" class="left left_nowrap tb10" id="Email" value="<?php print "$Email";?>" /></td> 
    </tr> 
    <tr> 
    <td class="right">Password:</td> 
    <td class="left left_nowrap"><input type="password" class="left left_nowrap tb10" id="Password"/></td> 
    </tr> 
    <tr> 
    <td class="right">Confirm Password:</td> 
    <td class="left left_nowrap"><input type="password" class="left left_nowrap tb10" id="Confirm_Password"/></td> 
    </tr> 
    <tr> 
    <td class="right">Gender:</td> 
    <td class="left left_nowrap"><span class="right"> 
     <select name="Gender" class="large tb10" id="Gender" value="<?php print "$gender"; ?>"> 
     <option value="Please Select...">Please Select...</option> 
     <option value="Male">Male</option> 
     <option value="Female">Female</option> 
     </select> 
    </span></td> 
    </tr> 
    <tr> 
    <td class="right">&nbsp;</td> 
    <td class="left"><input type="submit" class="submit tb10" value="Sign-UP" /></td> 
    </tr>  
    </form> 

connect_to_mysql.php

<?php 

/* 
1: "die()" will exit the script and show an error statement if something goes wrong with the "connect" or "select" functions. 
2: A "mysql_connect()" error usually means your username/password are wrong 
3: A "mysql_select_db()" error usually means the database does not exist. 
*/ 
// Place db host name. Sometimes "localhost" but 
// sometimes looks like this: >>  ???mysql??.someserver.net 
$db_host = "localhost"; 
// Place the username for the MySQL database here 
$db_username = "Shayaa"; 
// Place the password for the MySQL database here 
$db_pass = "nestle324"; 
// Place the name for the MySQL database here 
$db_name = "social_media"; 

// Run the actual connection here 
$con = mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql"); 
mysql_select_db('social_media', $con) or die ("no database");   
?> 
+2

eregi_replace()不推薦使用http://php.net/manual/en/function.eregi-replace.php ....使用mysql_insert_id停止http://php.net/manual/en/ function.mysql-insert-id.php – swapnesh

+0

...這是拋出「無法連接到數據庫?」 – OpenSorceress

+3

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – ThiefMaster

回答

0
  • 使用庫MySQLi或PDO的鏈接
  • 看,如果你選擇DATABSE而你沒有得到任何選擇的數據庫$sql = mysql_select_db('your database name', $connection)

  • stristr達到完全相同的結果爲eregi_replace(至少當你不使用正則表達式):

編輯:

  $sql= "INSERT INTO members (firstname, lastname, email, password,bio_body, sign_up_date , account_type) 
VALUES( '".$_SESSION['firstname']."','".$_SESSION['lastname']."','".$_SESSION['email']."','".$_SESSION['password']."','NULL' ,NOW() , 'a') " ; 

if (!mysql_query($sql)) 
{ 
die('Error: ' . mysql_error()); 
} 
echo "1 record added"; 
+0

現在檢查我的編輯。 –

+0

非常感謝sooo goodmod –

2

MySQL服務器形式的編碼可以處理許多數據庫。至少你會擁有mysql數據庫本身,其中包含服務器特定的數據,如用戶帳戶和您自己的應用程序數據庫。

因此,僅僅連接到服務器對於任何與數據庫相關的查詢來說都是不夠的。你必須選擇一個數據庫。

你的情況,而無需修改就需要利用這個命令之前,你可以提交一個查詢

mysql_select_db('your database name', $dbConnection); 

http://php.net/manual/en/function.mysql-select-db.php

不建議使用PHP的MySQL擴展的代碼和MySQL API的其餘部分,因爲這將在未來的PHP版本中被刪除。見上面的替代品

+0

你可以編輯我的編碼一個http://beta.primarypad.com/p/lCvHpCMgfL –

+1

這應該進入你的connect_to_mysql.php,自己嘗試,這並不困難:) –

+1

+1鼓勵OP自己試試':)' – halfer