简体   繁体   English

无法连接到数据库?

[英]Unable to connect to the database?

This is my php code below. 这是我下面的php代码。 For some reason, when I want it to connect to the database, it says "No database selected" but when I do a connection test it says "Success in database connection!" 出于某种原因,当我希望它连接到数据库时,它会显示“没有选择数据库”,但是当我进行连接测试时,它会显示“数据库连接成功!” So there's an error in the code below. 因此,下面的代码中存在错误。 Also I think it may be interfering with my form so I must have a typo; 另外我认为它可能会干扰我的形式,所以我必须输错字; when I remove the PHP the forms runs normally, but I want it to connect to the database. 当我删除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);    

?>

This is my form coding below 这是我在下面的表格编码

    <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 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");         
?>

MySQL server can handle many databases. MySQL服务器可以处理许多数据库。 At a minimum you'll have the mysql database itself, which holds server specific data like user accounts and your own application database. 至少你将拥有mysql数据库本身,它拥有服务器特定的数据,如用户帐户和你自己的应用程序数据库。

So simply connecting to the server is not enough for any query related to a database. 因此,仅仅连接到服务器对于与数据库相关的任何查询都是不够的。 You have to select a database. 您必须选择一个数据库。

in your case, without modifying the rest of the code and MySQL API you need to utilize this command before you can submit a query 在您的情况下,在不修改其余代码和MySQL API之前,您需要先使用此命令,然后才能提交查询

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

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

Using PHP's MySQL extension is not recommended, since this is going to be removed in future PHP versions. 建议不要使用PHP的MySQL扩展,因为将来的PHP版本将删除它。 See the link above for alternatives 请参阅上面的链接了解替代方案

  • use mysqli or PDO 使用mysqli或PDO
  • look if you are selecting databse while you are getting no database selected $sql = mysql_select_db('your database name', $connection) 看看你是否在没有选择数据库时选择数据库$sql = mysql_select_db('your database name', $connection)

  • stristr achieves exactly the same result as eregi_replace (at least when you don't use regular expressions): stristr达到完全相同的结果为eregi_replace (至少,当你不使用正则表达式):

EDIT: 编辑:

         $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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM