Help with a beginner php/mysql script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dazang
Forum Newbie
Posts: 10
Joined: Tue Aug 30, 2005 2:29 pm
Location: USA
Contact:

Help with a beginner php/mysql script

Post by dazang »

Hello,

I am very new to PHP/MySQL and need some very simple help on my very simple script. What I am wishing to accomplish is to have a simple HTML form submit to a php script which adds the values of the form input into my MySQL database.

My MYSQL database table has 7 fields, one of which is an autoincremental integer. The rest are data fields.

I have 3 scripts total: HTML form, php submission form, php database connection info script (included in the submission form).

The following are my scripts:

HTML FORM:

Code: Select all

<html>
<head>
<title>SUBMISSION FORM</title>
<link href="test.css" rel="stylesheet" type="text/css">
</head>
<body>
<FORM  ACTION="submitform.php" METHOD="POST">

<table width="500" border="0" cellspacing="3" cellpadding="0">
  <tr>
    <td align="right" valign="top">Name:</td>
    <td align="left" valign="top"><INPUT TYPE="TEXT" NAME="name"></td>
  </tr>
  <tr>
    <td align="right" valign="top">* Email:</td>
    <td align="left" valign="top"><INPUT TYPE="TEXT" NAME="email"></td>
  </tr>
  <tr>
    <td align="right" valign="top">City:</td>
    <td align="left" valign="top"><INPUT TYPE="TEXT" NAME="city"></td>
  </tr>
  <tr>
    <td align="right" valign="top">State:</td>
    <td align="left" valign="top"><INPUT TYPE="TEXT" NAME="state"></td>
  </tr>
  <tr>
    <td align="right" valign="top">Testimony:</td>
    <td align="left" valign="top"><textarea name="testimony" cols="45" rows="15"></textarea></td>
  </tr>
  <tr>
    <td align="right" valign="top">&nbsp;</td>
    <td align="left" valign="top"><INPUT TYPE="SUBMIT" value="SUBMIT!"></td>
  </tr>
</table>

</FORM>

</body>
</html>


PHP SUBMISSION SCRIPT:

Code: Select all

<?php
$name=$_POST['name'];
$email=$_POST['email'];
$city=$_POST['city'];
$state=$_POST['state'];
$testimony=$_POST['testimony'];

include("dbinc.php");
mysql_connect(localhost,$user,$pw);
@mysql_select_db($db) or die( "Unable to select database");
$query = "INSERT INTO testimonies ( tid, name, email, city, state, testimony, posted ) VALUES ('','$name','$email','$city','$state','$testimony','N')";
mysql_query($query);

mysql_close();
?>

PHP DB CONNECTION SCRIPT:

Code: Select all

<?php
$user		=		"username"
$pw			=		"password"
$db			=		"contactus"
?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

so what is your question? Are you having a problem, getting an error what?

I can tell you right off the bat that your code won't work because you are missing quotes around "localhost", but you need to be more specific as to what you're after.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

you need to change a lot of things.

1. specify value field for text input controls
2. name the submit button
3. its good to give the form a name
4. write all tags in small case to be XHTML compliant
5. put you database connection code as a function in a file so you can extend it to other pages.
6. pass these as function parameters to db connection

Code: Select all

<?php 
$user        =        "username" 
$pw            =        "password" 
$db            =        "contactus" 
//hmmm, where are the semicolons???????
?>
7. dont have an open file for receiving post variables.
it should be
if (isset($_POST["submit_button_name"]){
if ($_POST["submit_button_name"] == $_POST["submit_button_value"]){
//your code to get and process POST variables
}
}
8. use a die statement for mysql_connect or mysql_pconnect as well.
9. use backticks for the sql statement field names.
10. if tid is auto_increment, it wont work if you have '', use NULL instead

hope this helps:)
dazang
Forum Newbie
Posts: 10
Joined: Tue Aug 30, 2005 2:29 pm
Location: USA
Contact:

Thanks!

Post by dazang »

Sorry for the vagueness. My problem was that it was not posting the data to the fields. I obviously did not include any scripting to error report, so I was not sure where the problem was lying.

Thanks for the semicolon catch as well as the quotes around the host, that was dumb of me. I knew that, but again, as I said, I am a stupid noob.

Well, those 2 things alone fixed the problem of the data not posting. It posts now. But I like the rest of your info as well, and I will continue to work on those things.

I will have more questions regarding those, however.
dazang
Forum Newbie
Posts: 10
Joined: Tue Aug 30, 2005 2:29 pm
Location: USA
Contact:

# 7

Post by dazang »

raghavan20 wrote: 7. dont have an open file for receiving post variables.
it should be
if (isset($_POST["submit_button_name"]){
if ($_POST["submit_button_name"] == $_POST["submit_button_value"]){
//your code to get and process POST variables
}
}
I would like to implement this, however I have a question:

Where do I place the rest of the script inside the {} ?
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

EDITED

Post by raghavan20 »

Code: Select all

if (isset($_POST["submit_button_name"]){ 
	if ($_POST["submit_button_name"] == $_POST["submit_button_value"]){ 
		//your code to get and process POST variables
		$name=$_POST['name']; 
		$email=$_POST['email']; 
		$city=$_POST['city']; 
		$state=$_POST['state']; 
		$testimony=$_POST['testimony'];
		mysql_connect("localhost",$user,$pw) or die("Unable to establish a connection with the database"); 
		mysql_select_db($db) or die( "Unable to select database"); 
		$query = "INSERT INTO testimonies ( `tid`,`name`, `email`, `city`, `state`, `testimony`, `posted` ) VALUES (NULL, '$name', '$email', '$city', '$state', '$testimony','N')"; 
		mysql_query($query); 
		mysql_close(); 
	}
}
put the include statement as the first line in your page.
you can also use include_once() which I think is better
Last edited by raghavan20 on Tue Aug 30, 2005 4:35 pm, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

remember the quotes around "localhost" 8)
dazang
Forum Newbie
Posts: 10
Joined: Tue Aug 30, 2005 2:29 pm
Location: USA
Contact:

Re: EDITED

Post by dazang »

raghavan20 wrote:

Code: Select all

if (isset($_POST["submit_button_name"]){ 
	if ($_POST["submit_button_name"] == $_POST["submit_button_value"]){ 
		//your code to get and process POST variables
		$name=$_POST['name']; 
		$email=$_POST['email']; 
		$city=$_POST['city']; 
		$state=$_POST['state']; 
		$testimony=$_POST['testimony'];
		mysql_connect("localhost",$user,$pw) or die("Unable to establish a connection with the database"); 
		mysql_select_db($db) or die( "Unable to select database"); 
		$query = "INSERT INTO testimonies ( `tid`,`name`, `email`, `city`, `state`, `testimony`, `posted` ) VALUES (NULL, '$name', '$email', '$city', '$state', '$testimony','N')"; 
		mysql_query($query); 
		mysql_close(); 
	}
}
put the include statement as the first line in your page.
you can also use include_once() which I think is better
I did exactly this and the posting no longer works. It worked before I changed to this. I replaced submit_button_name with the proper name, and submit_button_value with its value. Is that correct? It is not posting info now.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

sorry submit_button_value should not be wrapped by POST. its just a value of the submit button

Code: Select all

if (isset($_POST["submit_button_name"]){ 
    if ($_POST["submit_button_name"] == "submit_button_value"){ 
        //your code to get and process POST variables 
        $name=$_POST['name']; 
        $email=$_POST['email']; 
        $city=$_POST['city']; 
        $state=$_POST['state']; 
        $testimony=$_POST['testimony']; 
        mysql_connect("localhost",$user,$pw) or die("Unable to establish a connection with the database"); 
        mysql_select_db($db) or die( "Unable to select database"); 
        $query = "INSERT INTO testimonies ( `tid`,`name`, `email`, `city`, `state`, `testimony`, `posted` ) VALUES (NULL, '$name', '$email', '$city', '$state', '$testimony','N')"; 
        mysql_query($query); 
        mysql_close(); 
    } 
}
if you come up with any error, give the error description along with the line number and your relevant code.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_POST["submit_button_name"] == $_POST["submit_button_value"] is always true.

.... and it's advisable to never rely on the submit button existing in the submission. (Try pressing the enter-key when inside a text field in IE, you'll find out why.)
Post Reply