How i can fix the error in following code ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

30th Dec 2017, 6:54 PM
hassan
2 Answers
+ 2
you are using the $con variable outside the else block.. so cut the connection statement and paste it in else block
30th Dec 2017, 9:54 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
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(); } ?>
30th Dec 2017, 6:54 PM
hassan