我无法使用php连接到mysql。我有一个名为联系人的数据库,我正在尝试连接到我想要添加的表是'tblusers'。我在“my12130”标识的“联系人”上创建了一个名为“Web_User”的用户。当用户填写所有字段并且密码匹配并且他们点击提交时,该页面将被重定向到memberContect页面。每次我使用以下连接方法时,当我填写所有写入并点击提交时,会显示来自我的连接代码的“表连接错误”消息。 (不显示PHP错误,即橙色/白色框中没有错误)。如果我将主机名更改为localhost也没有什么区别,因为我已尝试过两者。我正在使用wamp服务器来运行我的PHP代码。我在连接代码正上方有一条评论,这是我遇到问题的地方。
mysql数据库:
的MySQL>显示数据库;
+--------------------+
| Database |
+--------------------+
| information_schema |
| contacts |
| mysql |
| performance_schema |
| test |
+--------------------+
联系人中的表格列表: MySQL的>使用联系人; 数据库已更改 MySQL的> show tables;
+--------------------+
| Tables_in_contacts |
+--------------------+
| tblusers |
+--------------------+
1行(0.00秒)
PHP:
<?php
if ($_POST['submit']!=""){
if ($_POST['username']=="" || $_POST['password1']=="" || $_POST['password2']=="" || $_POST['firstname']=="" || $_POST['lastname']=="" || $_POST['email']=="" || $_POST['address']=="" || $_POST['city']=="" || $_POST['state']=="" || $_POST['zipcode']=="" || $_POST['phone']==""){
$error=1;
}
else if ($_POST['password1']!=$_POST['password2']){
$error=2;
}
else{
$hostname="localhost";
$database="contacts";
$mysql_login="Web_User";
$mysql_password="my12130";
if (!($db = mysqli_connect($hostname, $mysql_login, $mysql_password))){
echo "error on connect";
exit;
}
else{
if(!(@mysqli_select_db($database, $db))){
echo mysql_error();
echo "<br>error on table connection";
exit;
}
else{
$SQL = "Insert into tblUsers (username,password,firstname,lastname, email, address, city, state, zip, phone, signupDate) values('".$_POST['username']."',PASSWORD('".$_POST['password1']."'),'".$_POST['firstname']."'.'".$POST['lastname']."'.'".$POST['email']."'.'".$POST['address']."'.'".$POST['city']."'.'".$POST['state']."'.'".$POST['zipcode']."'.'".$POST['phone']."',NOW())";
mysql_query($SQL);
if(is_numeric(mysql_insert_id())){
header("Location:memberContect.php?name=".$_POST['username']);
}
else{
echo "Sorry, there was an error with your signup. Please contact the administrater";
}
mysql_close($db);
}
}
}
}
?>
答案 0 :(得分:0)
您可以将directyl连接到您想要的数据库,如下所示:
mysqli_connect($ hostname,$ mysql_login,$ mysql_password,$ database);
我建议您使用 isset()功能代替!='' 您也可以使用 addslashes()和 ms5()函数进行插入 你的代码看起来像这样:
else if ($_POST['password1']!=$_POST['password2']){
$error=2;
}
else{
$hostname="localhost";
$database="contacts";
$mysql_login="Web_User";
$mysql_password="my12130";
$conn = mysqli_connect($hostname, $mysql_login, $mysql_password, $database);
if (!$conn){
echo "error on connect";
exit;
}
else{
$username = addslashes($_POST['username']);
$password = md5($_POST['password1']);
$firstname = addslashes($_POST['firstname']);
$lastname = addslashes($POST['lastname']);
$email = addslashes($POST['email']);
$address = addslashes($POST['address']);
$lastname = addslashes($POST['city']);
$state = addslashes($POST['state']); /* or $state = addslashes($POST['state']); */
$zipcode = intval($POST['zipcode']); /* or $zipcode = addslashes($POST['zipcode']); */
$phone = intval($POST['phone']);
$signupDate = date("Y-m-d h:i:sa");
$SQL = "INSERT INTO tblUsers (username, password, firstname, lastname, email, address, city, state, zip, phone, signupDate)
VALUES('$username', '$password', '$firstname', '$lastname', '$email', '$address', '$city', '$state', '$zip', '$phone', '$signupDate')";
mysql_query($SQL);
if(is_numeric(mysql_insert_id())){
header("Location:memberContect.php?name=".$_POST['username']);
}
else{
echo "Sorry, there was an error with your signup. Please contact the administrater";
}
mysql_close($db);
}
}
}
}
您可以使用extract()函数来避免写入$ _POST:
if($ _SERVER [“REQUEST_METHOD”] ==“POST”): 提取物($ _ POST); 然后$ _POST ['地址']将成为$地址