0

I am using PHP 7 and am trying to find a solution on how my code should be because of 3 functions I am using that have been depricated in current version of php 7.

The three functions are:

mysql_close()
mysql_query()
mysql_select_db()

Here is my code:

 <?php  
 //mysql database connectivity  
 //inserting form data into the database  
 if(@$valid==true)  
{  
 $con= mysqli_connect("localhost","root","root"); 
 mysql_select_db("usersignup", $con);  
 @$a=$_POST['fname'];  
 @$b=$_POST['lname'];  
 @$c=$_POST['email'];  
 @$d=$_POST['cell'];  
 @$e=$_POST['add'];  
 @$f=$_POST['age'];  
 @$g=$_POST['gen'];  
 @$i=$_POST['username'];  
 @$j=$_POST['pass'];  
 @$h=$_POST['mtype'];  
 mysql_query("insert into user (FirstName,LastName,Email,CPhone,Address,Age,Gender,Username,Password,MType) values('$a','$b','$c','$d','$e','$f','$g','$i','$j','$h')");  
 header("Location:form1.php");  
 mysql_close($con);  
}  

?>

I have looked at some solutions offered that suggest the MySQLi or PDO_MySQL extension should be used, The problem is I havent found a practical way to impliment that.

John
  • 19
  • 9

4 Answers4

0

Please use mysqli with prepared statements, its more secure.

 $a=$_POST['fname'];  
 $b=$_POST['lname'];  
 $c=$_POST['email'];  
 $d=$_POST['cell'];  
 $e=$_POST['add'];  
 $f=$_POST['age'];  
 $g=$_POST['gen'];  
 $i=$_POST['username'];  
 $j=$_POST['pass'];  
 $h=$_POST['mtype']; 

$query= $mysqli->prepare("insert into user (FirstName,LastName,Email,CPhone,Address,Age,Gender,Username,Password,MType) values(?,?,?,?,?,?,?,?,?)");

$query->bind_param('ssssssssss',$a,$b,$c,$d,$e,$f,$g,$i,$j,$h);

if (!$query->execute())
{
    $flag = false;
}
Fakhruddin Ujjainwala
  • 2,493
  • 17
  • 26
0

PHP7 no longer supports mysql_* functions. You need to chose either PDO or mysqli APIs instead. You can use the following mysqli functions to replace the deprecated functions.

mysqli_select_db

mysqli_query

mysqli_close

Eg:

$link = mysqli_connect("localhost", "my_user", "my_password", "test");
mysqli_select_db($link, "world");
// SQL Logic goes here
mysqli_close($link);
Nisar P
  • 316
  • 2
  • 9
0

Just replace the deprecated function with the newest one.

mysql_close()      => mysqli_close()
mysql_query()      => mysqli_query()
mysql_select_db()  => mysqli_select_db()

so your code will be:

<?php  
//mysql database connectivity  
//inserting form data into the database  
if(@$valid==true){  
   $con= mysqli_connect("localhost","root","root"); 
   mysqli_select_db("usersignup", $con);  
   @$a=$_POST['fname'];  
   @$b=$_POST['lname'];  
   @$c=$_POST['email'];  
   @$d=$_POST['cell'];  
   @$e=$_POST['add'];  
   @$f=$_POST['age'];  
   @$g=$_POST['gen'];  
   @$i=$_POST['username'];  
   @$j=$_POST['pass'];  
   @$h=$_POST['mtype'];  
   mysqli_query("insert into user (FirstName,LastName,Email,CPhone,Address,Age,Gender,Username,Password,MType) values('$a','$b','$c','$d','$e','$f','$g','$i','$j','$h')");  
   header("Location:form1.php");  
   mysqli_close($con);  
}
?>
lollo64
  • 81
  • 1
  • 1
  • 6
-1

The same but with mysqli_ with only one difference that you have to pass db name in mysqli_connect( ... db name as last param

// mysqli connect
$con = mysqli_connect("localhost","root","root", "usersignup"); 

// mysqli query
mysqli_query("
  insert into user 
  (FirstName,LastName,Email,CPhone,Address,Age,Gender,Username,Password,MType)
  values('$a','$b','$c','$d','$e','$f','$g','$i','$j','$h')
");  

// connection close
mysqli_close($con);
Armen
  • 4,064
  • 2
  • 23
  • 40
  • what you mean @Anant ? – Armen Feb 24 '16 at 11:24
  • We are here not solving logickal issue of the code (like validation or mysql injection) @Anant , we are solving issue with mysql and mysqli usage issue – Armen Feb 24 '16 at 11:28
  • There is validation its just that I didnt post all the code here. – John Feb 24 '16 at 11:28
  • Actually John and Anant mentioning good point to improve your code to use %s %i in `mysqli_query(` or `bind_param` in pdo you can read more about it here: http://www.w3schools.com/php/php_mysql_prepared_statements.asp OR http://markonphp.com/mysqli-select-prepared-statements/ – Armen Feb 24 '16 at 11:32
  • atleast you can point those issues, and if you are doing same thing then what's the purpose to give the code. – Alive to die - Anant Feb 24 '16 at 11:45
  • @Anant i'm answering on exact question which is asked, i'm not going so deep if you want to point `Good` then post your own answer – Armen Feb 24 '16 at 11:47
  • I have done that alreaDY – Alive to die - Anant Feb 24 '16 at 11:50
  • With posting answer in question directly with text `Please try this` :D, instead writing normal answer ... – Armen Feb 24 '16 at 11:51