Panic! HTML form - PHP - output
03-11-2009, 02:27 PM
|
Panic! HTML form - PHP - output
|
Posts: 9
Name: Simon Harvey
|
Hello everyone. Well, this is the first PHP coding I've ever done, it's cobbled together from all over the place, and not surprisingly it doesn't work.
I need to collect data from a simple html form, use php to add it to an sql database, then send a selection from the database to the page, so that visitors will see a couple of fields from the replies received so far.
I'm sure this is easy, but I don't know what I'm doing.
The code I've thrown together is this:
..........................
<body>
<?php
$host = "localhost";
$user = "nobloque";
$pass = "@@@@@@";
$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);
}
if(isset($_POST)){
// Following line echoes
$sql = "select * from survey where name=" . $_POST['comments'] . " and comments='" . $_POST['comments'] . "'";
echo $sql;
$query = mysql_query($sql);
}
?>
<table class=questiontable cellspacing=2>
<?php
// 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>
..............................
What I see is the html form, with these messages at the bottom:
select * from survey where name= and comments=''
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [path] on line 131
The first is an echo of the line marked by my first note; the second refers to the line marked by my second note.
Could anyone help, please? I don't even know what I'm looking for.
Many thanks.
|
|
|
|
03-11-2009, 02:45 PM
|
Re: Panic! HTML form - PHP - output
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
You forgot to quote the name parameter in your query. Also I changed it from $_POST['comments'] to $_POST['name'], that's what you meant right?
PHP Code:
$sql = 'SELECT * FROM survey WHERE name = \''. $_POST['name'] .'\' AND comments = \''. $_POST['comments'] .'\';';
If you're still having problems, try outputing each variable to make sure it is set.
Also:
You're missing some quotes in your form. class=input should be class="input"
Last edited by NullPointer; 03-11-2009 at 02:50 PM..
|
|
|
|
03-11-2009, 03:08 PM
|
Re: Panic! HTML form - PHP - output
|
Posts: 9
Name: Simon Harvey
|
Many thanks, Nullpointer.
I spliced your code in, and immediately lost my error message.
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
Last edited by northernsky; 03-11-2009 at 03:32 PM..
Reason: Correction
|
|
|
|
03-11-2009, 03:19 PM
|
Re: Panic! HTML form - PHP - output
|
Posts: 843
Name: Mike
Location: United Kingdom
|
Try:
PHP Code:
if(isset($_POST)){
$sql = 'SELECT * FROM survey WHERE region = \''. $_POST['region'] .'\' AND comments = \''. $_POST['comments'] .'\';';
//echo $sql;
__________________
My Blog/Site: Please login or register to view this content. Registration is FREE
|
|
|
|
03-11-2009, 04:00 PM
|
Re: Panic! HTML form - PHP - output
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
Just noticed that you're using comma (,) instead of . to concatenate strings. This should fix your problem:
PHP Code:
echo "<tr>
<td class='feedback'>". $row['region']. "</td>
<td class='feedback'>". $row['comments']. "</td></tr>";
By the way, for future reference, your code will be easier to read if you embed it in php tags:
[ php]
//code here
[ /php]
Without the space.
Last edited by NullPointer; 03-11-2009 at 04:02 PM..
|
|
|
|
03-11-2009, 05:55 PM
|
Re: Panic! HTML form - PHP - output
|
Posts: 9
Name: Simon Harvey
|
Thanks everyone. It's still not building me a table of submissions, so I'm going to spend a while poking around in the DB.

|
|
|
|
03-11-2009, 06:43 PM
|
Re: Panic! HTML form - PHP - output
|
Posts: 9
Name: Simon Harvey
|
OK dudes, well, it looks as though the form has just been pretending to feed my junk data into the database. There's nothing there. Which would adequately explain the lack of a submissions table at the end.
So — if the server is 'localhost', the db is called 'nobloque_survey', the table is called 'survey', and I've got the password right — and I'm getting no error message at any stage — what's so bad about this code?
...................
<body>
<?php
$host = "localhost";
$user = "nobloque";
$pass = "######";
$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);
}
if(isset($_POST)){
$sql = 'SELECT * FROM survey WHERE region = \''. $_POST['region'] .'\' AND comments = \''. $_POST['comments'] .'\';';
$query = mysql_query($sql);
}
?>
<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>
</body>
</html>
..................................
The html is all familiar enough. This line
<form method='post' action='survey.php'>
has the form targeting its own page: i.e., this page is survey.php — I assume this isn't a problem.
In this bit
$query = "INSERT INTO 'survey'
('name' . 'region' . 'type'. 'comments' . 'email' . 'url')
VALUES
('$name' . '$region' . '$type' . '$comments' . '$email' . '$url')
";
there were commas between the variable names, so I changed them to fullstops.
It still feeds nothing into the db.
Any ideas, anyone? This is really getting tedious...
Many thanks,
Simon
|
|
|
|
03-11-2009, 06:50 PM
|
Re: Panic! HTML form - PHP - output
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
There's a problem with your insert query:
PHP Code:
$query = "INSERT INTO 'survey'
('name' . 'region' . 'type'. 'comments' . 'email' . 'url')
VALUES
('$name' . '$region' . '$type' . '$comments' . '$email' . '$url')
";
Should be:
PHP Code:
$query = "INSERT INTO 'survey'
('name' , 'region' , 'type', 'comments' , 'email' , 'url')
VALUES
('$name' , '$region' , '$type' , '$comments' , '$email' , '$url')
";
I suggest outputing your variables to make sure everything is set properly.
|
|
|
|
03-11-2009, 07:02 PM
|
Re: Panic! HTML form - PHP - output
|
Posts: 9
Name: Simon Harvey
|
Thanks for that, Nullpointer. Actually, those fullstops were originally commas. I changed them because I was told off for using commas to separate variables somewhere else in the code. Oh boy...
Enough for one day, I think. Back tomorrow.
|
|
|
|
03-11-2009, 07:05 PM
|
Re: Panic! HTML form - PHP - output
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
Quote:
Originally Posted by northernsky
Thanks for that, Nullpointer. Actually, those fullstops were originally commas. I changed them because I was told off for using commas to separate variables somewhere else in the code. Oh boy...
Enough for one day, I think. Back tomorrow.
|
In an earlier post I mentioned that you were using , to concatinate strings instead of .
You need to use commas to seperate parameters in SQL (and php).
. is an operator like + or -
|
|
|
|
03-12-2009, 03:20 PM
|
Re: Panic! HTML form - PHP - output
|
Posts: 9
Name: Simon Harvey
|
Right, I think that's pretty much sunk in — thanks.
Now, the first part of the page is working. The punter clicks the Go button and their data is squirted into my sql db. Super. What I need to do next is to build a table at the bottom of the page that sets out two chosen fields from each of the last 15 (if there are 15) records in the db. That is, the most recent 15. Now, the code I thought would do it (actually I thought it would tabulate the whole db) just bounces the relevant fields from the last submission.
<table class=questiontable cellspacing=2>
PHP Code:
<?php while ($row = mysql_fetch_array($query)) { echo "<tr> <td class='feedback'>". $row['region']. "</td> <td class='feedback'>". $row['comments']. "</td></tr>"; } ?>
</table>
How can I get this routine to iterate through the last x records?
As ever, many thanks!
Simon
|
|
|
|
03-12-2009, 05:20 PM
|
Re: Panic! HTML form - PHP - output
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
If you want the entire table you need to alter your select query:
PHP Code:
$sql = 'SELECT region , comments FROM survey';
If you only want a certain number of records, use the limit parameter:
PHP Code:
//will return a maximum of 5 records
$sql = 'SELECT region , comments FROM survey LIMIT 5';
|
|
|
|
03-12-2009, 07:23 PM
|
Re: Panic! HTML form - PHP - output
|
Posts: 9
Name: Simon Harvey
|
Nullpointer, you are a star. Have a beer on me.

|
|
|
|
|
« Reply to Panic! HTML form - PHP - output
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|