Forum Moderators: open

Message Too Old, No Replies

PHP to MySQL

         

tonynoriega

5:20 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, heres the problem first.... PHP form, works fine. MySQl dbase seems to be working ....almost fine.

I created a form with 2 simple fields for testing. I submit the form, i get confirmation, i get the confirmation email, i check the MySQL dbase and 1 record added. I go back to the exact same page and submit the form again, with new data, and the 2nd record is not appended..... why would this happen? I didnt make any changes, to anything....it just cant get any additional records to show up....! im going mad....

Field "id" is auto increment, primary key, no other attributes to the table...

here is the code: (nevermind the comments tags in the PHP, the page functions fine, its the Dbase that i cant figure out...)
___________________________________________________________

<!--<?php

$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("dbase"); $dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("newhecom_leads");

if (isset($_POST['submit'])) {

if (!$_POST['fname'] ¦!$_POST['email'] )
{
die('<table width="500" border="0" align="center" bgcolor="#FFFFFF">
<tr>
<td><h1>You did not complete all of the required fields, please go <a href=http://example.com/Lead_Registration/index.php>back</a> and ensure all of your fields are complete.<br> Thank You.</h1></td>
</tr>
</table>');
}

$fname = $_POST['fname'];
$email = $_POST['email'];
$query = "INSERT INTO Registration_Data (id, fname, email)
VALUES ('$id','$fname','$email')";

mysql_query($query);

mysql_close();

$yoursite = "www.example.com";
$youremail = "anoriega@example.com";

$subject = "You have successfully enterd a lead";
$message = "A lead has been entered into the database. For $fname";
$email = $_POST['email'];
mail($email, $subject, $message, "From: $yoursite <$youremail>nX-Mailer:PHP/" . phpversion());

?>-->

<table width="750" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">
<tr>
<td><h1>Successful Lead Entry</h1></td>
</tr>
<tr>
<td><p>Thank you, for entering your data.</td>
</tr>
<tr>
<td> </td>
</tr>
</table>

<!--<?php
}
else
{
?>-->

<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<form action="http://example.com/index.php?action=page_display&PageID=25" method="post">
<tr>
<td >First Name: </td>
<td >
<input name="fname" type="text" id="fname" size="20"></td>
</tr>
<tr>
<td>Email:</td>
<td><input name="email" type="text" id="email" size="20"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Register" ></td>
</tr>
</form>
</table>

<!--<?php
}
?>-->

[edited by: jatar_k at 5:49 pm (utc) on May 7, 2007]
[edit reason] changed to example.com [/edit]

eelixduppy

6:02 pm on May 7, 2007 (gmt 0)



Change your code to resemble the following line:

mysql_query($query) or die([url=http://www.php/net/mysql-error]mysql_error[/url]());

Let us know if this is giving you any errors after you submit the form.

tonynoriega

6:06 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



well i started my trouble shooting by completely removing the "id" and "$id" from the INSERT statement and VALUES statement, and its working fine....

next question....how do i add the date and time? i cant figure it out to save my life....i have a field in my table called "datetime" and i have it set as a "datetime"

shouldnt i be able to just add this to the code below:
$datetime = NOW(); and then put it into the VALUES statement as: '$datetime'?
_____________________________________________________
$fname = $_POST['fname'];
$email = $_POST['email'];
$query = "INSERT INTO Registration_Data (fname, email)
VALUES ('$fname','$email')";
______________________________________________________

eelixduppy

9:40 pm on May 7, 2007 (gmt 0)



NOW();
is a MySQL function, not PHP. You need to put that within the query for the time. You can also make the field so that on insert it sets the current timestamp to that field.

And the reason why your previous query wasn't working was because you had the ID field auto incrementing. The key word in there is "auto". It does this by itself, you don't need to do it manually through the query. ;)

tonynoriega

5:44 pm on May 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Roger that....

Next question...if i may.

I have my confirmation page that says "you have successfully enterd a lead....blah, blah..." and i repeat back on the page what the user entered by calling the fields.... got that part down.

When the record is assigned the Unique ID via the "id" field, how can i call that from the dbase, to show on the confirmation field? obviously i cant call that field, due to the obvious, that is not entered on the user side...

can i make that ID show on the page for my users, that way they can (at a later time) call the records to modify based on ID...?

Tastatura

6:06 pm on May 8, 2007 (gmt 0)

10+ Year Member



My opinion based on your first post and issue with id field, and later reporting that everything works fine after you removed it ->
If I understood you correctly you set up id field, in SQL db, as an autoincrement and primary key, and nothing else (i.e. you are not passing/parsing any other values to it). If this is the case then you should replace $id with NULL in your code. It should look something like this:

$query = "INSERT INTO Registration_Data (id, fname, email)
VALUES (NULL,'$fname','$email')";

HTH

tonynoriega

6:10 pm on May 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I dont understand why NULL in the ID field?

I want each record to have a Unique ID....wouldnt setting that field to NULL just put "NULL" in the field in the actual table?

tonynoriega

6:13 pm on May 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



EELIX...

i am not following you when you say to put the NOW(); function in the "query" are you referring to this section of my code? and how? I tried to put "datetime="NOW();" into the Values statement, but that did not work...
...
$remarks = $_POST['remarks'];
$query = "INSERT INTO Registration_Data (fname,lname,address,...)
VALUES ('$fname','$lname','$address','...)";

mysql_query($query);

mysql_close();
...

[edited by: tonynoriega at 6:18 pm (utc) on May 8, 2007]

Tastatura

6:18 pm on May 8, 2007 (gmt 0)

10+ Year Member



I dont understand why NULL in the ID field?

I want each record to have a Unique ID....wouldnt setting that field to NULL just put "NULL" in the field in the actual table?

no, it should't put NULL in the table. In your php code write NULL, however don't set it up as NULL in SQL table (it should be int or whatever you like), and field should be set to auto increment...

Give it a try (be careful of the syntax, etc) and see if it works for you

tonynoriega

6:19 pm on May 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, i got that, but i dont understand the purpose?