gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:43:27 27 4
gpt4 key购买 nike

<?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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com