我是 mysql 和 php 编码的新手。我遇到了同样的问题,访问 mysql 的基本方法效果很好。这是我的代码
<?php
$server = "localhost";
$user = "cscs";
$password = "C0va!CaRe";
$database = "cscs";
$link = mysqli_connect($server, $user, $password, $database);
if(!$link){
echo "Error connecting database, ".mysql_error();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP MySQL Test 1</title>
</head>
<body>
<h2 style="text-align: center;">Database Test 1 - Creating a table manually and updating through form</h2>
<form name="register" id="register" action="" method="post">
<table width="60%" cellspacing='1' cellpadding="10" border="1">
<tr>
<td width="45%" valign="middle" align="left">Full Name</td>
<td width="55%" valign="middle" align="left"><input type="text" name="full-name" id="full-name"></td>
</tr>
<tr>
<td width="45%" valign="middle" align="left">Email</td>
<td width="55%" valign="middle" align="left"><input type="text" name="email" id="email"></td>
</tr>
<tr>
<td width="45%" valign="middle" align="left">Phone</td>
<td width="55%" valign="middle" align="left"><input type="text" name="phone" id="phone"></td>
</tr>
<tr>
<td width="45%" valign="middle" align="left">City</td>
<td width="55%" valign="middle" align="left"><input type="text" name="city" id="city"></td>
</tr>
<tr>
<td width="45%" valign="middle" align="left">Country</td>
<td width="55%" valign="middle" align="left"><input type="text" name="country" id="country"></td>
</tr>
<tr>
<td width="45%" valign="middle" align="left">User Name</td>
<td width="55%" valign="middle" align="left"><input type="text" name="username" id="username"></td>
</tr>
<tr>
<td width="45%" valign="middle" align="left">Password</td>
<td width="55%" valign="middle" align="left"><input type="text" name="pass" id="pass"></td>
</tr>
<tr>
<td width="45%" valign="middle" align="left">Confirm Password</td>
<td width="55%" valign="middle" align="left"><input type="text" name="cpass" id="cpass"></td>
</tr>
<tr>
<td valign="middle" align="left" colspan="2"><input type="submit" name="submit" value="Register"></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit'])){
$fullname = $_POST['full-name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$country = $_POST['country'];
$username = $_POST['username'];
$pass = $_POST['pass'];
mysqli_select_db($link,'cscs');
$sql1 = "INSERT INTO userinfo (fullname, email, phone, city, country, username, password) VALUES ('{$fullname}', '{$email}', '{$phone}', '{$city}', '{$country}', '{$username}', '{$pass}')";
if(mysqli_query($link, $sql1)) {
echo "Data inserted to database";
}
else {
echo "Error updating database, ".mysql_error();
}
}
?>
</body>
</html>