Forum Moderators: coopster

Message Too Old, No Replies

PHP for dummies: form to sql to html page

How to get data from a form, into an sql database, then onto an html page

         

northernsky

2:41 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



I suspect this is going to be a bit basic for this forum, but please bear with me — I'm a PHP beginner.

I have a form on my html page to collect data from site visitors. I want to feed that into an sql database which I've created on my server. (I've created a table in the database, and suitably-named fields in the table.)

When the user clicks the 'send' button, the data needs to be squirted into the database as a new record. But I also want to display users' submissions at the bottom of the original page — the page with the form on it.

Is there a source of 'off the shelf' php for this that I can modify with my own field names?

Many thanks.

eelixduppy

4:41 pm on Mar 10, 2009 (gmt 0)



Hello and Welcome to WebmasterWorld!

Here is an example of extracting data from the database: [webmasterworld.com...]

You are going to be doing something very similar except using a slightly different query with INSERT [dev.mysql.com].

The MySQL functions [php.net] documentation also has examples that can be helpful for you in this learning process. There is also google if you need more examples.

Try to get something together first and then if you get stuck get back here and we'll be glad to help you out.

northernsky

5:40 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



Many thanks. Actually, I've done a fair amount of googling, but the explanations I've found are not always enlightening: I haven't done any programming since 1984, and I wasn't any good at it then :-)

northernsky

4:26 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



OK, well, I've put a page together, and the errors are starting to come in. First, here is the code:

..............................

<body>

<?php
$host = "localhost";
$user = "xx";
$pass = "xx";
$dbname = "nobloque_survey";

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$name =$_POST['name'];
$region =$_POST['region'];
$type =$_POST['type'];
$comments =$_POST['comments'];
$email =$_POST['email'];
$url =$_POST['url'];

?>

<!-- here's the html form -->

<table class=questiontable cellspacing=2>
<form method='post' action='survey.php'>
<tr>
<td class=fieldlabel>name</td>
<td class=field><input class=input type="text" name="name" size="35"></td></tr>
<tr>
<td class=fieldlabel>region</td>
<td class=field><input class=input type="text" name="region" size="35"></td></tr>
<tr>
<td class=fieldlabel>what type of area do you live in?</td>
<td class=field>

<table border=0 cellpadding=0 cellspacing=0>
<tr>
<td class=radiobuttons>big town</td>
<td class=radiobuttons><input type="radio" name="type" value="bigtown" checked="unchecked"></td></tr>
<tr>
<td class=radiobuttons>small town</td>
<td class=radiobuttons><input type="radio" name="type" value="smalltown" checked="unchecked"></td></tr>
<tr>
<td class=radiobuttons>country</td>
<td class=radiobuttons><input type="radio" name="type" value="country" checked="unchecked"></td></tr>
</table>

</td></tr>
<tr>
<td class=fieldlabel>your experiences</td>
<td class=field>
<textarea class=input wrap='virtual' style='width: 245px; height:100px' name=comments>
</textarea>
<tr>
<td class=fieldlabel>e-mail address</td>
<td class=field><input class=input type="text" name="email" size="35"></td></tr>
<tr>
<td class=fieldlabel>url of web-site, blog &c.</td>
<td class=field><input class=input type="text" name="url" size="35"></td></tr>
<tr>
<td colspan=2 class=fieldlabel>
<table style="float:right" border=0 cellpadding=0 cellspacing=0>
<td style='padding-right:10px'><input type="submit" value="Enviar"></td>
<td><input type="reset" value="Cancelar"></td>
</table>
</td></tr>
</form>
</table>

<!-- END OF html form -->

<?php

if ($comments != "")

{

$dbh=mysql_connect ($host, $user, $pass, $dbname) or die ('Error: Connection error: cannot connect to the database' . mysql_error());
mysql_select_db ($dbname) or die( "Error: Selection error, Unable to select database $dbname" . mysql_error());

$query = "INSERT INTO 'survey'
('name' , 'region' , 'type', 'comments' , 'email' , 'url')
VALUES
('$name' , '$region' , '$type', '$comments' , '$email' , '$url')
";

mysql_query($query);

}

// code below displayed on page

$sql = "select * from survey where name=" . $_POST['comments'] . " and comments='" . $_POST['comments'] . "'";
echo $sql;

$query = mysql_query($sql);

?>

<table class=questiontable cellspacing=2>

<?php

// Something wrong below: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

while ($row = mysql_fetch_array($query)) {
echo "<tr>
<td class='feedback'>", $row['region'], "</td>
<td class='feedback'>", $row['comments'], "</td></tr>";
}
?>

</table>

</body>

...........................

The first note I've made in there is that some of the code is displayed on the page, below the html form. Below the code is displayed an error message. So what I see at the bottom of the screen, below the form, is this:

select * from survey where name= and comments=''
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/nobloque/public_html/survey.php on line 129

Line 129 is immediately beneath my second note. Any ideas, folks? The code is all second-hand, as you may have guessed.

Thanks,

Simon

[edited by: eelixduppy at 10:48 pm (utc) on Mar. 11, 2009]
[edit reason] snipped db specifics [/edit]

dreamcatcher

6:52 pm on Mar 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Simon,

Firstly, welcome to WebmasterWorld. :) Secondly, nothing is too basic here, we were all beginners once.

The reason you are seeing the data on the screen is because of this:

echo $sql;

You`ll need to remove that echo statement. Your query is probably failing because your comments value is not presented in the statement in apostrophes.

So, this:

$sql = "select * from survey where name=" . $_POST['comments'] . " and comments='" . $_POST['comments'] . "'";

Should be:

$sql = "select * from survey where name='" . $_POST['comments'] . "' and comments='" . $_POST['comments'] . "'";

And finally, always make sure you filter all incoming POST vars using mysql_real_escape_string. An example would be:

$sql = "select * from survey where name='" . mysql_real_escape_string($_POST['comments']) . "' and comments='" . mysql_real_escape_string($_POST['comments']) . "'";

dc

northernsky

7:29 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



Dreamcatcher, many thanks. The echo problem is a little obvious in retrospect, but stress can cause blindness :-)

Now my only problem is that I want the page to show two fields from each submission in a table below the entry form. I thought the code I had would do that, but it doesn't.

<table class=questiontable cellspacing=2>

<?php

while ($row = mysql_fetch_array($query)) {
echo "<tr>
<td class='feedback'>", $row['region'], "</td>
<td class='feedback'>", $row['comments'], "</td></tr>";
}

?>

</table>

I need to run through the database, pick out two fields ('region' and 'comments') from each record, and feed them into a row of a table. Can you tell me what I'm doing wrong, please? I'm still at the stage where php code looks pretty opaque.

Thanks again,

Simon

blang

3:26 am on Mar 12, 2009 (gmt 0)

10+ Year Member



Is there a specific reason you have commas delimiting the <td> elements and the data? They should be concatenation operators (the dot '.'). This may very well be your problem. In fact, if you intend on using "double quotes", you may as well include the data within the quotes - PHP will evaluate the variables and add the data right along in the string, e.g.

echo "This is an array value: {$row['region']}";

The {curly braces} help delimit the array element in the string.

An invaluable reference is the online PHP manual [php.net]. Please carefully read the Language Reference [us2.php.net] first! There's a huge amount of relevant information there. If I had to pick a place for a PHP neophyte to start, I'd recommend the section on data types [us2.php.net]. After all, programming is all about the data. Once you've mastered the concept of the different data types (even though PHP is a weakly typed language), it will give you a new perspective on how to code.

dreamcatcher

7:38 am on Mar 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The commas are fine. PHP supports both period and comma operators, but both work slightly differently. With 'echo' you can use both, with 'print' only the dot.

When using dots, all the parts are concatenated to form a single string that will be printed, while with commas, all the parts are printed one by one, without any concatenation.

Using commas is I believe, slightly faster than using dots

dc

blang

10:09 am on Mar 12, 2009 (gmt 0)

10+ Year Member



@dreamcatcher> thanks for posting, I don't recall ever seeing that before. After some searching, I did find a place in the manual that refers to use of the comma. It's buried though.

northernsky

7:39 pm on Mar 12, 2009 (gmt 0)

10+ Year Member



Thanks, guys. I know about the PHP manual, and I will get stuck in sooner or later, but at the moment I have a specific and (theoretically) simple job that I need to do quickly.

So how would you recommend iterating this —

<?php

while ($row = mysql_fetch_array($query)) {
echo "<tr>
<td class='feedback'>". $row['region']. "</td>
<td class='feedback'>". $row['comments']. "</td></tr>";
}
?>

(or something like it) so that it serves up the chosen fields from the most recent x records? I though the code I have here would give me these fields for the whole db, but it actually gives me just the last record.

Thanks again.