How i can fix the error in following code ? | SoloLearn: Learn to code for FREE!

0

How i can fix the error in following code ?

it gives error while connecting to mysql data base complete code is belown in cmnts . thnx

12/30/2017 6:54:13 PM

hassan

2 Answers

New Answer

+2

you are using the $con variable outside the else block.. so cut the connection statement and paste it in else block

0

<html> <body> <form action="" method=post> id <input type=text name=id> username <input type=text name=username> <input type=submit> </form> </body> </html> <?Php if($_SERVER["REQUEST_METHOD"]=="POST") { if(!isset($_POST['id']) || !isset($_POST['username'])) { echo "plz fill out all fields "; } else { $id=mysqli_real_escape_string($con , $_POST['id']); $username=mysqli_real_escape_string($con , $_POST['username']); } $con=mysqli_connect("localhost" , "root" , "" , "hassan"); if(mysqli_connect_errno()) { echo "could not connect to mysql " . mysqli_connect_error(); } else { if($con->query("INSERT INTO student (id , username) VALUES ('$id' , '$username')")) echo "your record is added succesfully"; else echo mysqli_error($con); } $con->close(); } ?>