Jump to content


Photo

Inserting rows into a db from form data


  • Please log in to reply
10 replies to this topic

#1 g1c9

g1c9
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 16 January 2006 - 12:38 AM

Hey, I ran this code after submitting a form but for some reason I used phpmyadmin and there were no rows?

$email = $_POST['email'];
echo $email;
$link = md5($email);
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 

$query = "INSERT INTO tests VALUES ($email,$link)";
mysql_query($query);

mysql_close();

Is the problem the PHP code or the form? Here is the form.

<form action="signup.php" method="POST">
<input type="text" name="email" size="40"><br><br>
<input type="submit" value="Start tricking your friends!">
</form>

I don't know what is wrong...

EDIT: I have deduced that it is the form. But whats wrong with the form?

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 16 January 2006 - 04:16 AM

You should verify that your INSERT statement is being run correctly.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 g1c9

g1c9
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 16 January 2006 - 04:57 AM

$query = "INSERT INTO tests VALUES ('cameron.garvie@gmail.com','camerons')";
if (mysql_query($query)) {
echo 'inserted';
}
else {
echo 'nope';    
}

I ran that code, (values hardcoded), and it returned 'nope'. Why is taht?

tests is a table = and it has 2 string columns, email and link.

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 16 January 2006 - 05:04 PM

Run your query as LazyJones always suggests:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Always make your SQL queries like this:

$sql = "SELECT ....";
$result = mysql_query($sql) or die ("ERROR: ".mysql_error()." with query: $sql");[/quote]

And you'll find out what MySQL is complaining about -- personally, I can't stand the fact that your column names aren't explicitly stated.

Good luck.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 g1c9

g1c9
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 17 January 2006 - 06:22 PM

Ok, so now my code is looking like this:

$email = $_POST["email"];
echo 'email:'.$email.'<br>';
$link = md5($email);
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 

$query = "INSERT INTO tests VALUES ('cameron.garvie@gmail.com','camerons')";
$result = mysql_query($query) or die ("ERROR: ".mysql_error()." with query: $sql");
echo $result;

mysql_close();

And when run, it spits out this:

email:dasd1234asdasd@gmail.com
ERROR: Duplicate entry 'cameron.garvie@gmail.com' for key 1 with query:

So i check my DB, and it spits out this as the total:

email | link
-------------

email | link
cameron.garvie@gmail.com | camerons

Sorry, I'm new to all of this, whats wrong?

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 17 January 2006 - 07:24 PM

Apparently, you have a UNIQUE key on the email address column -- if so, MySQL will complain if you try and add another record with the same value for this column.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 g1c9

g1c9
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 17 January 2006 - 07:39 PM

I removed the unique key, and then using this code, tried again.

$email = $_POST["email"];
echo 'email:'.$email.'<br>';
$link = md5($email);
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 

$query = "INSERT INTO tests VALUES ($email,$link)";
$result = mysql_query($query) or die ("ERROR: ".mysql_error()." with query: $sql");
echo $result;

mysql_close();

I submitted the form email as cameron.garvie@gmail.com

email:Cameron.garvie@gmail.com
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com,0136542e3041addfa391312618e87388)' at line 1 with query:

Relevant server info.

PHP version      4.3.11
MySQL version     4.1.13-standard


#8 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 17 January 2006 - 07:58 PM

You need to quote your column values; try the following:

$query = "INSERT INTO tests VALUES ('$email','$link')";

BTW, I still think you should name your columns in the INSERT statement explicitly.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#9 g1c9

g1c9
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 17 January 2006 - 08:41 PM

THANK YOU!!!

It finally works, and I'll make sure to PM you when the whole project is up and running.

Also, I didn't mean to ignore your suggestion, I'm just not sure what [!--fonto:Lucida Console--][span style=\"font-family:Lucida Console\"][!--/fonto--]explicitly[!--fontc--][/span][!--/fontc--] means.

#10 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 17 January 2006 - 08:52 PM

Glad you got it working. By explicitly, I meant the following:

$query = "INSERT INTO tests ( email, link ) VALUES ('$email','$link')";

Otherwise, the parser simply assumes you mean the first two columns, which is unstable if you ever add any other fields to this table -- which is very likely.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#11 g1c9

g1c9
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 17 January 2006 - 09:36 PM

Thanks for all your help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users