Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting records to MySQL

Status
Not open for further replies.

jasek78

Programmer
Nov 5, 2001
298
US
I got a 'newsletter signup form' on my webpage in a layer with 2 fields 'name' and 'email'

I am using the POST method and opening up a 'addemails.php' page up in a new window that should be inserting records and displaying a 'thank you' message (I will be doing validation checking as soon as i know the Insert command works)

The database has one table called 'newsletter' with 3 fields: 'index' (auto-increment); 'name' 'email (both varchar 50)

Here is the 'addemails.php' code:

Code:
<? 
//initialize PHP

if($_POST['submit']) //If submit is hit
{
   //then connect as user
   //change user and password to your mySQL name and password
   mysql_connect("localhost","web","eso1com"); 
	
   //select which database you want to edit
   mysql_select_db("oakhills"); 

   //convert all the posts to variables:
   $name = $_POST['name'];
   $email = $_POST['email'];
   
   $sql = "INSERT INTO newsletter (index,name,email) VALUES ('', '$name', '$email')"
   //Insert the values into the correct database with the right fields
   $result=MYSQL_QUERY("INSERT INTO newsletter (index,name,email) VALUES ('', '$name', '$email')"); 

    //confirm
 ?>

Any help is much appreciated... I'm rather new to PHP and this is rather frustrating since I basically copy/pasted working code in and changed the information for my db...

Jason
 
First of all, $_POST['submit'] is the submit button itself. Depending on the browser, this value may not be submitted at all. You are much better off looking for the field value you are going to manipulate, such as $_POST['name'] or $_POST['email'].

Second, what problems are you having?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I think what is happening is that the variables are not passing to the PHP page and there are no records created in the table... any thoughts?
 
The php code was originally on the same page as the form, and that '$_POST['submit'] was to run the code only if the submit button was pressed. I will take it out and see if that is the problem.
 
Again, the meat of this script is inside this if-clause:

if($_POST['submit'])

If your browser does not send a submit value (and everyting in $_POST is browser-dependent), then that code-block will never run.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
the code was running. the window would pop up and show the 'thank you' text that I put inside the 'if' statement. but no values would get into the database.

I changed the 'if' statement to
Code:
if( isset($_POST['email'])
and now I just get a blank window....


Jason
 
Code:
$sql = "INSERT INTO newsletter (index,name,email) VALUES ('', '$name', '$email')"

if the field index is autoincrement you musn't put it in you're insert statement. it works automaticaly
 
I'm sorry, but I cannot comment on any output, as your script sample does not include it.

Verify that the script execution enters the if-block. Put a print statement inside it which will footprint the code. Put an else-block on that same if-block with another print statement to footprint that part.

Just before the if-block, add:

print '<pre>';
print_r ($_POST);
print '</pre>';

to verify your inputs.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
OK... First, thanks to you all for helping out. I will post all of the pertinant code for you to review.

I have a layer on the html page set up with a form:
Code:
<div id="newsletterlayer">
  <form action="addemail.php" method="post" name="addemail" target="_blank" id="addemail">
<table width="150" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width=15 align="center"><input name="name" type="text"  value="Name" maxlength=50 /></td>
  </tr>
  <tr>
    <td width=15 align="center"><input name="email" type="text" value="Email" maxlength=50 /></td>
  </tr>
  <tr>
    <td align="center"><input name="submit" type="Submit" value="Newsletter Signup!" /></td>
  </tr>
</table>
</form>


</div>

... and the entire PHP doc is this, currently:
Code:
<? 
//initialize PHP

//POST checking 
print '<pre>';
print_r ($_POST);
print '</pre>';

//insert records 

   //connect as user
   //change user and password to your mySQL name and password
   mysql_connect("localhost","web","eso1com"); 
	
   //select which database you want to edit
   mysql_select_db("oakhills"); 

   //convert all the posts to variables:
   $name = $_POST['name'];
   $email = $_POST['email'];
   
   //Insert the values into the correct database with the right fields
   $result=MYSQL_QUERY("INSERT INTO newsletter (name,email) VALUES ('$name', '$email')"); 

    //confirm
 ?>
<div align=center> 
Thank you <? $name ?> for signing up for the Newsletter.

</div>

 


	
</body>
<script>resizeTo(300, 300)</script>


The output I recieve is a blank page. No POSTs, nothing seems to execute. I set up the database using the PHPmyadmin and everything on that end is correct.

Any Ideas? or ways I can do this that will work? I got handed this project this morning with a deadline of 'asap'.

Thanks again for all your assistance
 
Yes I have... the output is a blank page:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>
<BODY></BODY></HTML>
 
change the following line:
Code:
  $result=MYSQL_QUERY("INSERT INTO newsletter (name,email) VALUES ('$name', '$email')");
to
Code:
  $result=MYSQL_QUERY("INSERT INTO newsletter (name,email) VALUES ('$name', '$email')") [red]or die(mysql_error())[/red];

This should come back with an error if the query was unable to complete, for whatever reason.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
It doesn't do any more. the PHP script is not executing for some reason. I'm going golfing now, work on this later.

I'm open to any suggestions on alternate methods, and thanks for the help.

Jason
 
Your php interpreter has not started. There must something wrong with the setup. Do other php scripts work normally on the server? The only thing I could suggest is changing <? to a more universal long version <?php.
 
OK... I've check the error logs, and changed the <? to <?php ...

I made a fairly simple mail form script that runs perfectly. I did a couple other things and finally found an error message.

Code:
Access denied for user: 'apache@localhost' (Using password: NO)

This baffled me as I don't use a 'apache' user to log in to the database. Is there a permission somewhere I need to set?

Thanks all!

Jason
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top