Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法连接到数据库?_Php_Mysql_Database_Forms - Fatal编程技术网

Php 无法连接到数据库?

Php 无法连接到数据库?,php,mysql,database,forms,Php,Mysql,Database,Forms,下面是我的php代码。出于某种原因,当我希望它连接到数据库时,它会显示“未选择任何数据库”,但当我进行连接测试时,它会显示“数据库连接成功!”,因此下面的代码中有一个错误。而且我认为它可能会干扰我的形式,所以我必须有一个打字错误;当我删除PHP时,表单正常运行,但我希望它连接到数据库 <?php // let's initialize vars to be printed to page in the HTML section so our script does not return e

下面是我的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>

名字:

MySQL服务器可以处理许多数据库。至少您将拥有
mysql
数据库本身,它保存特定于服务器的数据,如用户帐户和您自己的应用程序数据库

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

?>
因此,对于任何与数据库相关的查询,仅仅连接到服务器是不够的。您必须选择一个数据库

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

?>
在您的情况下,在不修改其余代码和MySQL API的情况下,您需要在提交查询之前使用此命令

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

不建议使用PHP的MySQL扩展,因为这将在未来的PHP版本中删除。有关备选方案,请参见上面的链接
  • 使用mysqli或PDO
  • 查看您是否在选择数据库时未选择任何数据库
    $sql=mysql\u select\u db('your database name',$connection)

  • stristr
    获得与
    eregi\u replace
    完全相同的结果(至少在不使用正则表达式时):

  • 编辑:


    eregi_replace()已被弃用…也停止使用mysql_insert_id…这会引发“无法连接到数据库?”。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果选择PDO,。问题可能出在connect_to_mysql.php中-我看不到测试连接,测试是否指定connect和connect_to_mysql上的哪个数据库缺少要连接的数据库?能否发布connect_to_mysql.php脚本。因为如果您遇到错误,如未选择任何数据库,则该错误必须存在于您的连接脚本中。这应该进入到您的connect\u to\u mysql.php中,您可以自己尝试,这并不难:)