Jump to content

Recommended Posts

Greetz,

 

 

I am trying to write a simple newsletter sign up form using HTML FORM + PHP + MySQL.

 

I seem to be having a problem getting it working and I suspect the issue is in my PHP code and was wondering if someone could take a look at it for me to see if they can spot any obvious mistakes.

 

<?php

$link = mysql_connect('localhost', 'testuser', 'pass1');

if (!$link) {

die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully';

mysql_select_db("testdb", $link);

$sql="INSERT INTO newsletters(mens)

VALUES

('$_POST[e-mail]')";

if (!mysql_query($sql,$link))

{

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

}

echo "Done!";

mysql_close($link);

?>

 

Note: My SQL Database is named "testdb", I have a table called "newsletters" and a field called "mens".

 

Kind regards.

There was really nothing wrong with the way you had it to start with. When you start concatenating variables into strings when it isn't necessary, all you do is introduce a greater number of chances to make a typo.

 

The more common way to write it would be to leave array indices that are strings quoted, and use complex notation.

 

$sql="INSERT INTO newsletters(mens)VALUES('{$_POST['e-mail']}')";

I see.

 

I tried your line Pikachu, but it didn't work either. Perhaps the problem is else where - perhaps PHP is not connecting to the database.. How would I check this?

 

Note: I'm not getting "Connected Successfully".

Note: I'm new to PHP/SQL.. sorry.

I wasn't getting anything. Error log showed this:

 

PHP Fatal error:  Call to undefined function mysql_connect()

 

Google led me to someone who said you need the PHP MySQL module installed. Problem might be found. :P

Just noticed you have a hyphen (AKA subtraction operator) in the value of an array key. Although it will usually work, it can have some unexpected results depending on how it's quoted. You'd be better off to omit it or use an underscore instead.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.