Page 1 of 1

Help with a beginner php/mysql script

Posted: Tue Aug 30, 2005 2:47 pm
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"
?>

Posted: Tue Aug 30, 2005 2:57 pm
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.

Posted: Tue Aug 30, 2005 2:57 pm
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:)

Thanks!

Posted: Tue Aug 30, 2005 3:45 pm
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.

# 7

Posted: Tue Aug 30, 2005 3:58 pm
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 {} ?

EDITED

Posted: Tue Aug 30, 2005 4:19 pm
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

Posted: Tue Aug 30, 2005 4:22 pm
by Burrito
remember the quotes around "localhost" 8)

Re: EDITED

Posted: Tue Aug 30, 2005 5:11 pm
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.

Posted: Tue Aug 30, 2005 5:14 pm
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.

Posted: Tue Aug 30, 2005 5:15 pm
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.)