我收到错误:
Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in C:\Apache\htdocs\movie\actor_form\demo.php:9 Stack trace: #0 {main} thrown in C:\Apache\htdocs\movie\actor_form\demo.php on line 9
当我尝试通过php表单将数据输入我的数据库时。这是我的两个脚本:
demo.php文件:
<?php
define('DB_Name', 'movies');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_HOST', 'localhost');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysqlI_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$actfn = $_POST['Actor First Name'];
$actln = $_Post['Actor Last Name'];
$actag = $_POST['Actor Age'];
$actgn = $_POST['Actor Gender'];
$actht = $_POST['Actor Height'];
$sql = "INSERT INTO actor_demographics (Actor_First_Name, Actor_Last_Name, Actor_Age, Actor_Gender, Actor_Height) VALUES ('$actfn', '$actln', '$actag', '$actgn', '$actht')";
if (!mysql_query($sql)) {
die('ERROR: ' . mysql_error());
}
mysql_close();
?>
demo-form.php文件:
<form action="demo.php" method="post" />
<p>Actor First Name: <input type="text" name="Actor First Name" /><p>
<p>Actor Last Name: <input type="text" name="Actor Last Name" /><p>
<p>Actor Age: <input type="text" name="Actor Age" /><p>
<p>Actor Gender: <input type="text" name="Actor Gender" /><p>
<p>Actor Height: <input type="text" name="Actor Height" /><p>
<input type="submit" value="Submit" />
</form>
错误引用的第9行是:
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
在此问题上还有其他帖子表明我需要删除;在命令
;extension=php_mysqli.dll
在我的php.ini文件中找到
我已尝试删除此分号并将其重新添加,但仍然收到错误。
非常感谢任何帮助。
谢谢
Php.ini编辑文件:
; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir ="C:/php/ext"
;extension=php_exif.dll ; Must be after mbstring as it depends on it
extension=php_mysqli.dll
;extension=php_oci8_12c.dll ; Use with Oracle Database 12c Instant Client
答案 0 :(得分:-1)
试试这个
如果您正在使用本地服务器,则无需输入密码,只需将其设为“空白”即可。
始终使用SQL注入来提交数据库中的数据 例如
$actfn=$link ->real_escape_string(trim($_POST['Actor_First_Name']));
如果您使用mysqli_query() expects at least 2 parameters, 1
mysqli_query($sql)
你必须这样使用。
if (!mysqli_query($link,$sql))
{
echo("Error description: " . mysqli_error($link));
}
mysqli_close($link);
在此处查看完整代码
define('DB_NAME', 'movies');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
if (!$link) {
die("Database connection failed: " . mysqli_error());
}
// 2. Select a database to use
$db_select = mysqli_select_db($link, DB_NAME);
if (!$db_select) {
die("Database selection failed: " . mysqli_error());
}
$actfn=$_POST['Actor_First_Name'];
$actln=$_POST['Actor_Last_Name'];
$actag=$_POST['Actor_Age'];
$actgn=$_POST['Actor_Gender'];
$actht=$_POST['Actor_Height'];
$sql = "INSERT INTO actor_demographics (Actor_First_Name, Actor_Last_Name, Actor_Age, Actor_Gender, Actor_Height) VALUES ('$actfn', '$actln', '$actag', '$actgn', '$actht')";
if (!mysqli_query($link,$sql))
{
echo("Error description: " . mysqli_error($link));
}
mysqli_close($link);
?>
演示from.php
我刚刚在name
标记
<form action="demo.php" method="post" />
<p>Actor First Name: <input type="text" name="Actor_First_Name" /><p>
<p>Actor Last Name: <input type="text" name="Actor_Last_Name" /><p>
<p>Actor Age: <input type="text" name="Actor_Age" /><p>
<p>Actor Gender: <input type="text" name="Actor_Gender" /><p>
<p>Actor Height: <input type="text" name="Actor_Height" /><p>
<input type="submit" value="Submit" />
</form>