Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Panic! HTML form - PHP - output
Old 03-11-2009, 02:27 PM Panic! HTML form - PHP - output
Novice Talker

Posts: 9
Name: Simon Harvey
Trades: 0
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.
northernsky is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-11-2009, 02:45 PM Re: Panic! HTML form - PHP - output
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
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"
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 03-11-2009 at 02:50 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-11-2009, 03:08 PM Re: Panic! HTML form - PHP - output
Novice Talker

Posts: 9
Name: Simon Harvey
Trades: 0
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
northernsky is offline
Reply With Quote
View Public Profile
 
Old 03-11-2009, 03:19 PM Re: Panic! HTML form - PHP - output
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
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
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 03-11-2009, 04:00 PM Re: Panic! HTML form - PHP - output
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 03-11-2009 at 04:02 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-11-2009, 05:55 PM Re: Panic! HTML form - PHP - output
Novice Talker

Posts: 9
Name: Simon Harvey
Trades: 0
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.

northernsky is offline
Reply With Quote
View Public Profile
 
Old 03-11-2009, 06:43 PM Re: Panic! HTML form - PHP - output
Novice Talker

Posts: 9
Name: Simon Harvey
Trades: 0
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
northernsky is offline
Reply With Quote
View Public Profile
 
Old 03-11-2009, 06:50 PM Re: Panic! HTML form - PHP - output
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-11-2009, 07:02 PM Re: Panic! HTML form - PHP - output
Novice Talker

Posts: 9
Name: Simon Harvey
Trades: 0
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.
northernsky is offline
Reply With Quote
View Public Profile
 
Old 03-11-2009, 07:05 PM Re: Panic! HTML form - PHP - output
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by northernsky View Post
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 -
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-12-2009, 03:20 PM Re: Panic! HTML form - PHP - output
Novice Talker

Posts: 9
Name: Simon Harvey
Trades: 0
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
northernsky is offline
Reply With Quote
View Public Profile
 
Old 03-12-2009, 05:20 PM Re: Panic! HTML form - PHP - output
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
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'
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-12-2009, 07:23 PM Re: Panic! HTML form - PHP - output
Novice Talker

Posts: 9
Name: Simon Harvey
Trades: 0
Nullpointer, you are a star. Have a beer on me.

northernsky is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Panic! HTML form - PHP - output
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.19171 seconds with 12 queries