0

I am trying to create a login page so that certain users within our organization will have access to a form with sensitive information. I decided to use PHP and MySQL to do this and believe I am very close, but am having issues getting the two to connect to one another. I am using WAMP server so I have a localhost setup.

Here is my very basic html form:

<form method="post" action="addemail.php">
   <label for="firstname">First Name:</label>
   <input type="text" id="firstname" name="firstname" /> <br/>
   <label for="lastname">Last Name:</label>
   <input type="text" id="lastname" name="lastname" /> <br/>
   <label for="email">Email:</label>
   <input type="text" id="email" name="email" /> <br/>
   <input type="submit" value="submit" name="submit"/>
</form>

On my PHP form, I have this:

$dbc = mysqli_connect('localhost', 'root', 'password', 'leadmen') 
or die('Error connection to MySQL server.');

$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$email = $_POST['email'];

$query ="INSERT INTO leadmen_usernames (first_name, last_name, email)" .
"VALUES ('$first_name', '$last_name', '$email')";

mysqli_query($dbc, $query)
or die('Error querying database');

echo 'Username Added.';

mysqli_close($dbc);

I don't know too much about these technologies but believe the problem lies either within my connection info to $dbc = mysqli_connect, or maybe there's an error with mysqli vs mysql?

Not sure if this matters, but I used phpmyadmin to create the table.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user3389052
  • 27
  • 1
  • 4
  • When I submit the form, I get redirected to a white page that just shows the php content... – user3389052 Mar 06 '14 at 16:18
  • http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Aaron Mar 06 '14 at 16:18
  • Try to remove the `" . "` your code seems to check out. – Funk Forty Niner Mar 06 '14 at 16:19
  • When I remove the period the code breaks in dreamweaver.. – user3389052 Mar 06 '14 at 16:21
  • Try `$query ="INSERT INTO leadmen_usernames (first_name, last_name, email) VALUES ('$first_name', '$last_name', '$email')";` and check your column names. – Funk Forty Niner Mar 06 '14 at 16:21
  • Check your server if it runs php in another file... – Shlomo Mar 06 '14 at 16:22
  • "When I remove the period the code breaks in dreamweaver" => "INSERT INTO leadmen_usernames (first_name, last_name, email) VALUES ('$first_name', '$last_name', '$email')". You have to remove the two quotation marks and put everything on one line then, too – fsperrle Mar 06 '14 at 16:24
  • Is PHP and SQL running? Do you already have a `.php` file that you know runs? I.e.: `` (*I have to ask*) – Funk Forty Niner Mar 06 '14 at 16:24
  • Thanks again, column names match up but after putting this code in, it executes the same and replies a white page with the php code :/ – user3389052 Mar 06 '14 at 16:24
  • Ah, there you go. PHP is either not installed or not setup properly, if you see actual code. – Funk Forty Niner Mar 06 '14 at 16:25
  • I've gotten php to work on my server and have created a separate form already which I have tested. I'm not positive that SQL is running but I don't know how to check that either... It says connected on wamp server and I was able to run phpmyadmin so I'm assuming its good... – user3389052 Mar 06 '14 at 16:25
  • Did you try running ``? – Funk Forty Niner Mar 06 '14 at 16:27
  • I just created a new folder on my localhost and named it test. I included that echo statement in the document and when I load the test.php file on localhost, I get "Hello World" to show up. This means that PHP is in fact running correct? – user3389052 Mar 06 '14 at 16:31
  • Yes, that's a start. Now you need to check if SQL is installed and running. – Funk Forty Niner Mar 06 '14 at 16:31
  • Do you know an easy way to do this? I can't seem to find much about this online for some reason :/ – user3389052 Mar 06 '14 at 17:00
  • Sorry, I don't. Google "wamp check if MySQL is loaded". – Funk Forty Niner Mar 06 '14 at 17:31
  • I figured out my issue. Everything was in place and working. The only thing I had to do was instead of going to localhost directly, use http://localhost instead. Not sure why this matters but its working now... Thanks for all your help, I really appreciate it – user3389052 Mar 06 '14 at 17:44

1 Answers1

-1

If you see just the PHP code you probably forgot the opening PHP tag before you start to write actual PHP code.

<?php
$dbc = mysqli_connect('localhost', 'root', 'password', 'leadmen') 
or die('Error connection to MySQL server.');

$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$email = $_POST['email'];

$query ="INSERT INTO leadmen_usernames (first_name, last_name, email)" .
"VALUES ('$first_name', '$last_name', '$email')";

mysqli_query($dbc, $query)
or die('Error querying database');

echo 'Username Added.';

mysqli_close($dbc);

?>

You can close it again with '?>'

fsperrle
  • 1,262
  • 1
  • 14
  • 26
  • Thanks but I have the opening and closing tags in place, just didn't think it was necessary to include them on here. – user3389052 Mar 06 '14 at 16:22
  • Are you sure your server is able to serve PHP files then? Have you set up a server or are you just using the HTML WYSISWYG editor / preview function of Dreamweaver? – fsperrle Mar 06 '14 at 16:25
  • I setup a wamp server and have been using php on that. When I test , it runs fine. For some reason its just showing my entire php code when I submit this. I think my login information might be wrong but even if it was, I would expect an error rather than it just submitting the code. I'm totally lost now :/ – user3389052 Mar 06 '14 at 17:13
  • Two last ideas: Are you using the short tags ( instead of – fsperrle Mar 06 '14 at 19:57