0

I am getting the error:

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

When I try to enter data into my database via a php form. Here are my two scripts:

demo.php file:

<?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 file:

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

Line 9 that the error is referencing is:

$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

There have been other posting on this matter that state I need to remove the ; on the command

;extension=php_mysqli.dll

found in my php.ini file

I have tried removing this semicolon and have added it back in and am still receiving the error.

Any help would be greatly appreciated.

Thank you

Php.ini edited file:

; 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
Ububtunoob
  • 103
  • 8
  • 1
    The semicolon is indicates that that line is commented out in the config, it should definitely be gone. Remove it and restart your web server and you should be good – Orangepill Nov 21 '17 at 04:20
  • dump it like echo phpinfo(); It will give useful info – Shubhranshu Nov 21 '17 at 04:21
  • Did you restart the server, no pun intended :), serving PHP? – Rubén S. García Nov 21 '17 at 04:22
  • @sigfried have the semicolon removed and have restarted my Apache server twice now, with no luck. Does everything else seem good with my code. I am a bit confused as to why this is happeing – Ububtunoob Nov 21 '17 at 04:57
  • @Ububtunoob, check this post, it may help you https://stackoverflow.com/questions/7250356/how-to-install-mysqli – Rubén S. García Nov 21 '17 at 05:13
  • @sigfried. I thank you for your assistance but I am still at a total loss. I have un-commented both the extension_dir = "C:/php/ext" and the extension=php_mysqli.dll lines in my php.ini file per the link you provided me. I have restarted the Apache server several times and have restarted my computer. I am not sure what else there is to be done, but I appreciate all the help you have given. – Ububtunoob Nov 21 '17 at 06:07
  • @Ububtunoob no problem, glad to help you or at least try :), by the way, I can see you're on Windows, are you using XAMPP or lampstack?, if that's the case I'll encourage you to check their pages and try to find out what can be causing the issue. I'll read you later. Cheers – Rubén S. García Nov 21 '17 at 06:33

1 Answers1

-1

Try this

If you are working on the local server then no need to require the password just make it `blank'.

Always use the SQL injection for submitting the data in the database For example

$actfn=$link ->real_escape_string(trim($_POST['Actor_First_Name']));

You will get the error mysqli_query() expects at least 2 parameters, 1 if you used mysqli_query($sql)

You have to use like this.

if (!mysqli_query($link,$sql))
  {
  echo("Error description: " . mysqli_error($link));
  }

mysqli_close($link);

Checkout full code here

  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);
?>

demo-from.php I just added the underscore in the name tag

<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>
Naren Verma
  • 2,205
  • 5
  • 38
  • 95