Jump to content

Archived

This topic is now archived and is closed to further replies.

g1c9

Inserting rows into a db from form data

Recommended Posts

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?

Share this post


Link to post
Share on other sites

You should verify that your INSERT statement is being run correctly.

Share this post


Link to post
Share on other sites

$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.

Share this post


Link to post
Share on other sites

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");

 

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

×

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.