5

I am a beginner and also a diploma student... please help me solve this error... I tried many online solution but it cant help ... I'm new to php and mysql...

<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="slr"; // Database name 
$tbl_name="software"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form 
$soft_name=$_POST['soft_name'];
$installed_date=$_POST['installed_date'];
$expiry_date=$_POST['expiry_date'];
$product_key=$_POST['product_key'];
// Insert data into mysql 
$sql="INSERT INTO $software(soft_name, installed_date, expiry_date, product_key)VALUES('$soft_name', '$installed_date', '$expiry_date', '$product_key')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful". 
if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='CreateData.php'>Back to main page</a>";
} else {
    echo "ERROR";
}
// close connection 
mysql_close();
?>
Muhammad Arif
  • 1,014
  • 3
  • 22
  • 56
Steven Surain
  • 51
  • 3
  • 3
  • 5

2 Answers2

3

You should use mysqli_connect instead of mysql_connect which is deprecated since PHP 5.5.0 :

    $link = mysqli_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysqli_select_db($link, $db_name)or die("cannot select DB");
Delphine
  • 861
  • 8
  • 21
  • Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\SLR\CreateData2.php on line 21 ERROR Fatal error: Uncaught Error: Call to undefined function mysql_close() in C:\xampp\htdocs\SLR\CreateData2.php:37 Stack trace: #0 {main} thrown in C:\xampp\htdocs\SLR\CreateData2.php on line 37 – Steven Surain Apr 07 '16 at 08:18
  • Same as others.. Use **mysqli_close($link);** Take a look at this example in offical php documentation : http://php.net/manual/fr/mysqli.select-db.php ("Procedural style" example) – Delphine Apr 07 '16 at 08:21
  • now it shows: Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\SLR\CreateData2.php on line 21 ERROR Warning: mysqli_close() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\SLR\CreateData2.php on line 37 – Steven Surain Apr 07 '16 at 08:24
  • Got to official php doc, it is really well written : http://php.net/manual/en/mysqli.query.php. You have to pass `$link` param too, so : `$result=mysqli_query($link, $sql);` where `$link` is the same variable as my answer (your connection to DB) – Delphine Apr 07 '16 at 08:27
  • Nice ! Please check my answer as the right one, thanks. And read PHP official doc, there are helpful examples – Delphine Apr 07 '16 at 08:32
0

Try This:

Old way:

<?php
    $link = mysql_connect('localhost', 'user', 'pass');
    mysql_select_db('testdb', $link);
    mysql_set_charset('UTF-8', $link);
?>

New way: all you gotta do is create a new PDO object. PDO's constructor takes at most 4 parameters, DSN, username, password, and an array of driver options.

A DSN is basically a string of options that tell PDO which driver to use, and the connection details... You can look up all the options here PDO MYSQL DSN

<?php
    $db=new PDO('mysql:host=localhost;dbname=slr;charset=utf8mb4', 'root', '') or die("Could connect to Database");
?>

According to Here.

EniGma
  • 2,444
  • 4
  • 22
  • 33