php - 需要插入表 "doctors"(id 是自动增量)。不知道查询出了什么问题。我收到错误 -"webpage not available "。帮助解决它

标签 php html mysql

<?php

$host = 'localhost';
$username = 'uai';
$password = '';
$database = 'Pharma';
$table = 'doctors';
mysql_connect( $host, $username, $password )
or  die(" could not connect to database..." .mysql_error ());
mysql_select_db($database)
or die ("Could not connect to database..." .mysql_error ());
$firstname =$_POST['firstname'];
$lastname =$_POST['lastname'];
$speciality =$_POST['speciality'];
$hospital=$_POST['hospital'];
$city=$_POST['city'];
$state=$_POST['state'];
$country=$_POST['country'];
$sql = "INSERT INTO $table (firstname,lastname,speciality,hospital,city,state,country)VALUES ('$firstname','$lastname','$speciality','$hospital','$city','$state','$country')";
$result =mysql_query($sql);
if ($result) {
echo "New record created successfully";
}
else {
echo "Insertion Error: ";
}
?>
<?php
mysql_close();
?>

<form action="doctors.php" method = "post">

<table align="center">
<tr>
<td>First Name:</td>
<td><input type="text" name="firstname" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lastname" /></td>
</tr>
<tr>
<td>Speciality:</td>
<td><input type="text" name="speciality" /></td>
</tr>
<tr>
<td>Hospital:</td>
<td><input type="text" name="hospital" /></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city" /></td>
</tr>
<tr>
<td>State:</td>
<td><input type="text" name="state" /></td>
<tr>
<td>Country:</td>
<td><input type="text" name="country" /></td>
</tr>
</table>
<table align="center">
<tr>
<td><center> <input type="submit" name="submit" >
<input type="reset" name="clear">
</td>
</form>

最佳答案

$country=$_POST[country'] 中缺少 '

将此 $country=$_POST[country']; 更改为 $country=$_POST['country'];

$country=$_POST['country'];
$sql = "INSERT INTO $table (firstname,lastname,speciality,hospital,city,state,country)VALUES ('$firstname','$lastname','$speciality','$hospital','$city','$state','$country')";
$result =mysql_query($sql);
if ($result) {

更新的代码:

改变这个

mysql_connect( $host, $username, $password ) or  die(" could not connect to database..." .mysql_error ());
mysql_select_db($database) or die ("Could not connect to database..." .mysql_error ());

$con = mysql_connect( $host, $username, $password ) or  die(" could not connect to database..." .mysql_error ());
$db = mysql_select_db($database,$con) or die ("Could not connect to database..." .mysql_error ());

语法

mysql_select_db(connection,dbname);

connection =>   Required. Specifies the MySQL connection to use
dbname =>       Required. Specifies the default database to be used

因此,连接和数据库名称是必填字段。将连接值传递给 mysql_select_db。

[注意:mysql 函数 已被折旧。使用PDOmysqli.*函数]

<?php

$host = 'localhost';
$username = 'uai';
$password = '';
$database = 'Pharma';
$table = 'doctors';

$con = mysql_connect( $host, $username, $password ) or  die(" could not connect to database..." .mysql_error ());
$db = mysql_select_db($database,$con) or die ("Could not connect to database..." .mysql_error ());

$firstname =$_POST['firstname'];
$lastname =$_POST['lastname'];
$speciality =$_POST['speciality'];
$hospital=$_POST['hospital'];
$city=$_POST['city'];
$state=$_POST['state'];
$country=$_POST['country'];
$sql = "INSERT INTO $table (firstname,lastname,speciality,hospital,city,state,country)VALUES ('$firstname','$lastname','$speciality','$hospital','$city','$state','$country')";
$result =mysql_query($sql);
if ($result) {
echo "New record created successfully";
}
else {
echo "Insertion Error: ";
}
?>
<?php
mysql_close();
?>
<小时/>

使用 mysqli.* 函数

MySQL extension officially deprecated since PHP 5.5.0 in late 2012 and it will be removed in the future. The alternatives since PHP 5 and later are MySQLi ("i" stands from "improved") and PDO (PHP Data Objects).

<?php
$host = 'localhost';
$username = 'uai';
$password = '';
$database = 'Pharma';
$table = 'doctors';

$con = mysqli_connect($host, $username, $password,$database) or die(" could not connect to database..." .mysqli_connect_error());

$firstname =$_POST['firstname'];
$lastname =$_POST['lastname'];
$speciality =$_POST['speciality'];
$hospital=$_POST['hospital'];
$city=$_POST['city'];
$state=$_POST['state'];
$country=$_POST['country'];

$result = mysqli_query($con,"INSERT INTO $table (firstname,lastname,speciality,hospital,city,state,country)VALUES ('$firstname','$lastname','$speciality','$hospital','$city','$state','$country')");

if ($result) {
    echo "New record created successfully";
}
else {
    echo "Insertion Error: ";
}

mysqli_close($con);
?>

关于php - 需要插入表 "doctors"(id 是自动增量)。不知道查询出了什么问题。我收到错误 -"webpage not available "。帮助解决它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34286429/

相关文章:

PHP Mysql Insert Into 不工作,没有数据发布到数据库时没有错误

php - 测试未链接到任何路由的 Controller 方法

html - 对齐 self 拉伸(stretch)不起作用?

html - 如何重写网络服务器子目录上的绝对 Assets 路径

MySql 多选和分组结果。如何正确地做到这一点?

mysql - 如何进行大量选定的更新

php - 未定义 xmlDocumentElement

php - 无法添加或更新子行: a foreign key constraint fails

javascript - 具有特定视口(viewport)宽度的超出视口(viewport)高度的额外空间

mysql - 数据库 : How to store a date unknown vs date that hasn't occurred yet