Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This question already has an answer here:

I'm trying to run the following code with the correct connect information and for some reason it isn't working. My php skills are novice and I'm learning via w3schools, but I can't figure out what's wrong and the following message is returned:

Parse error: syntax error, unexpected end of file in /home4/user/public_html/Scripts/demo.php on line 22

As far as I'm concerned everything on the server side is fine. All in all with this code I'm trying to insert info into a database table and then send a user to a different page if it's entered correctly.

<?php

// Create connection
$con=mysqli_connect("host","username","password","db");

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();

// escape variables for security
 $name = mysqli_real_escape_string($con, $_POST['name']);

 $sql="INSERT INTO testing (Name)
 VALUES ('$name')";

 if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}

 echo "1 record added";

 mysqli_close($con);
?>
share|improve this question

marked as duplicate by Hanky Panky php Aug 25 '14 at 8:52

This question was marked as an exact duplicate of an existing question.

    
w3schools is full of buggy code and bad practice examples. You should learn from the docs and examples from PHP.net. – DanFromGermany Aug 25 '14 at 8:52
    
Glad to hear, thank you for the advice! – ArtyyDesigns Aug 25 '14 at 9:47
up vote 0 down vote accepted

You have not clased the mysql_connect errorno() brackets. Use the code below

<?php

// Create connection
$con=mysqli_connect("host","username","password","db");

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
 $name = mysqli_real_escape_string($con, $_POST['name']);

 $sql="INSERT INTO testing (Name)
 VALUES ('$name')";

 if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}

 echo "1 record added";

 mysqli_close($con);
?>

Hope this helps you

share|improve this answer
    
Thank you very much! Sorry it took a bit to get back to you, was reading. – ArtyyDesigns Aug 25 '14 at 9:40
    
No problem please choose this answer as correct – Hudixt Aug 25 '14 at 10:23

you have not close your code

<?php

// Create connection
$con=mysqli_connect("host","username","password","db");

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die; // append this
 } //apend this 
// escape variables for security
 $name = mysqli_real_escape_string($con, $_POST['name']);

 $sql="INSERT INTO testing (Name)
 VALUES ('$name')";

 if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}

 echo "1 record added";

 mysqli_close($con);


?>
share|improve this answer
    
Thank you for the answer and your time! – ArtyyDesigns Aug 25 '14 at 9:47

your if (mysqli_connect_errno()) { is not closed

<?php

// Create connection
$con=mysqli_connect("host","username","password","db");

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
 $name = mysqli_real_escape_string($con, $_POST['name']);

 $sql="INSERT INTO testing (Name)
 VALUES ('$name')";

 if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}

 echo "1 record added";

 mysqli_close($con);
?>
share|improve this answer
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();

forgot to close

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.