Page 1 of 1

Unable to Connect to Localhost

PostPosted: 07. April 2017 01:35
by NinePoundHammer
Hello,

I apologize if this has already been discussed, however I could not find a thread that was the same problem as mine. I recently installed XAMPP with the intention of connecting PHP to a mySQL database to create some HTML form entry to enter data into tables. I am able to connect through the control panel to both Apache and mySQL. I created a file to test my connect to Localhost, however it is not doing anything, not even producing an error. I created this simple PHP connect file to try and test my connection:

Code: Select all
<? php

$dbhost = 'localhost';
$username = 'root';
$password = '';

mysql_connect ("$dbhost" , "$username" , "$password");

echo"Successful connection";

?>


When I navigate to localhost/connect.php (file name is connect.php and stored in the htdocs folder in my XAMPP install) I simply get a blank page, it displays nothing. It does not return my echo statement nor an error. If I navigate to just localhost in my browser the XAMPP page will display. I am at a loss, the HTML form entry I created also does not work and does not successfully insert data into my database.

Any help would be greatly appreciated.

Thanks.

Re: Unable to Connect to Localhost

PostPosted: 07. April 2017 07:45
by Altrea
Hi,

Use the correct php start tag, there is no whitespace in it.

Best wishes,
Altrea

Re: Unable to Connect to Localhost

PostPosted: 08. April 2017 16:23
by NinePoundHammer
Wow, that was certainly an oversight, thanks! That solved that problem. It's been about 10 years since I last used PHP, so I feel like I'm learning it over again. If you might be able to provide a little more guidance, my end goal is to have a form enter fields into a specific table in mySQL DB I have already created. I can confirm all of the field names are correct, but the HTML form will not enter the data for some reason. This is the PHP code I am using:

Code: Select all
<?php

$dbhost = 'localhost';
$username = 'root';
$password = '';
$db = 'mis360db';

mysql_connect ("$dbhost" , "$username" , "$password", "$db");

echo"Successfull connection";

$value=$_POST['CustomerFName'];
$value2=$_POST['CustomerLName'];
$value3=$_POST['CustomerAddress'];
$value4=$_POST['CustomerPhone'];
$value5=$_POST['CustomerEmail'];

$sql="INSERT INTO customer (CustomerFName, CustomerLName, CustomerAddress, CustomerPhone, CustomerEmail) VALUES ('$value', '$value2', '$value3', '$value4', '$value5');

if (!mysql_query($sql)) {
   die(Error:  ' . mysql_error() );
}
?>


And this is the HTLM form that I created:

Code: Select all
<html>
<head>
   <title> Enter Customer Information</title>
</head>
<body>
<div class="header">
   <h1>Enter customer information</h1>
</div>
<form acction="connect.php" method="post" />
   <table>
   <tr>
      <td>First Name:</td>
      <td><input type="text" name="CustomerFName" /></td>
   </tr>
   <tr>
      <td>Last Name:</td>
      <td><input type="text" name="CustomerLName" /></td>
   </tr>
   <tr>
      <td>Address:</td>
      <td><input type="text" name="CustomerAddress" /></td>
   </tr>
   <tr>
      <td>Phone Number:</td>
      <td><input type="text" name="CustomerPhone" /></td>
   </tr>
   <tr>
      <td>Email:</td>
      <td><input type="text" name="CustomerEmail" /></td>
   </tr>
   <tr>
      <td><input type="submit" value="Submit" /><td>
   </tr>
</form>
</body>
</html>


When I submit data through the form it does nothing. I confirmed all the field names are correct, the file names are correct, my database name is correct. Any idea why it may not be working? When I test my PHP code in my browser I get this error pointing to the end of my code at line 23:

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\connect.php on line 23


Thanks.

Re: Unable to Connect to Localhost

PostPosted: 08. April 2017 17:48
by JJ_Tagy
Close your single and double quotes.

$sql="INSERT INTO customer...

and
die(Error: ' . mysql_error() );


I recommend using Notepad++ or some other GUI as it will help you visually see some syntax errors.

Re: Unable to Connect to Localhost

PostPosted: 08. April 2017 19:45
by NinePoundHammer
That solved many of the issues I encountered, thanks! I was so used to just using Notepad, but Notepad ++ really helped. So I am now able to enter data into my table, however it is only entering null values and not the data that I enter into each text box from my HTML file. When I look at my connect.php file again I receive this:

Notice: Undefined index: CustomerFName in C:\xampp\htdocs\connect.php on line 20

Notice: Undefined index: CustomerLName in C:\xampp\htdocs\connect.php on line 21

Notice: Undefined index: CustomerAddress in C:\xampp\htdocs\connect.php on line 22

Notice: Undefined index: CustomerPhone in C:\xampp\htdocs\connect.php on line 23

Notice: Undefined index: CustomerEmail in C:\xampp\htdocs\connect.php on line 24


I followed a tutorial for this and I've raked my brain, Googled around, but can't seem to figure it out. This is the updated PHP file I am using with a few minor tweaks to fix syntax errors:

Code: Select all
<?php

define('DB_NAME', 'mis630db');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');

$link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
      die('Could not connect:  ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
      die('Can\'t use ' . DB_NAME . ':  ' . mysql_error());
}

$value=$_POST['CustomerFName'];
$value2=$_POST['CustomerLName'];
$value3=$_POST['CustomerAddress'];
$value4=$_POST['CustomerPhone'];
$value5=$_POST['CustomerEmail'];

$sql="INSERT INTO customer (CustomerFName, CustomerLName, CustomerAddress, CustomerPhone, CustomerEmail) VALUES ('$value', '$value2', '$value3', '$value4', '$value5')";

if (!mysql_query($sql)) {
   die('Error:  ' . mysql_error() );
}

mysql_close();
?>

Re: Unable to Connect to Localhost

PostPosted: 08. April 2017 23:40
by JJ_Tagy
I also see you have spelled 'action' as 'acction' in your HTML.

Re: Unable to Connect to Localhost

PostPosted: 10. April 2017 20:00
by NinePoundHammer
Yup, that was it. Thanks for all the help!