-2
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "test";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$value1=$_POST['txtname'];
$value2=$_POST['cellnumber'];
$value3=$_POST['dist'];
$value4=$_POST['specialization'];
$value5=$_POST['membername'];
$value6=$_POST['date'];
$sql = "INSERT INTO students (StudentName, CellNumber, District, Specialization, PromotionMember, Date)
VALUES ('$value1', '$value2', '$value3', '$value4', '$value5', '$value6')";
if (!mysqli_query($sql)) { 
die ('Error: ' . mysql_error());
}
else
{ 
echo ("معلومات ارایه شده شما ثبت شد");
header("Location: register.html");
}
mysqli_close();
?>

connection is successfull but data insertion is getting error on line 23 which is (if (!mysqli_query($sql)) { ) enter image description here

Ghufran Ataie
  • 160
  • 2
  • 11

3 Answers3

1

Your query doesn't run because you are using the MySQLi function wich need 2 parameters to execute your query.

So instead of

mysqli_query($sql)

you have to do:

mysqli_query($conn, $sql)

Your code also looks vulnerable to SQL Injections, so you want to know how to escape the strings in MySQLi. I recommend you to use prepared statements. I hope this will help!

node_modules
  • 4,790
  • 6
  • 21
  • 37
0

Try this code...

$sql = "INSERT INTO students (StudentName,CellNumber, District, Specialization, PromotionMember,      Date)VALUES ('$value1', '$value2', '$value3', '$value4', '$value5', '$value6')";
$q=mysqli_query($conn,$sql);
if (!$q) { 
die ('Error: ' . mysqli_error($conn));
}else{header("Location: register.html");}
bharat savani
  • 339
  • 5
  • 18
0

Try This code..

$sql = "INSERT INTO students (StudentName, CellNumber, District, Specialization, PromotionMember, Date)
VALUES ('$value1', '$value2', '$value3', '$value4', '$value5', '$value6')";
$qw=mysqli_query($conn,$sql);

if($qw)

{

header("Location: register.html");

}
Ragul N
  • 9
  • 2
  • 1
    Why should the OP "try this code"? A **good answer** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – B001ᛦ Jul 13 '17 at 14:27