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
https://forums.phpfreaks.com/topic/3205-inserting-rows-into-a-db-from-form-data/
Share on other sites

$query = "INSERT INTO tests VALUES ('[email protected]','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.

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.

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 ('[email protected]','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:[email protected]
ERROR: Duplicate entry '[email protected]' for key 1 with query:

 

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

 

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

email | link
[email protected] | camerons

 

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

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 [email protected]

 

email:[email protected]
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

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.

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.

Archived

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

×
×
  • 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.