这是我下面的 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"> </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");
?>