Forum Moderators: coopster
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.
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.
..............................
<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]
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
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
echo "This is an array value: {$row['region']}";
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.
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
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.