Jump to content

Inserting rows into a db from form data


g1c9

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?

Link to comment
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.

Link to comment
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.

Link to comment
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?

Link to comment
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

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

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.