Current location: Hot Scripts Forums » General Community » Script Requests » PHP for dummies: form to sql to html page


PHP for dummies: form to sql to html page

Reply
  #1 (permalink)  
Old 03-10-09, 09:45 AM
northernsky northernsky is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
PHP for dummies: form to sql to html page

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.
Reply With Quote
  #2 (permalink)  
Old 03-10-09, 12:03 PM
bizzar528's Avatar
bizzar528 bizzar528 is offline
Community Liaison
 
Join Date: Sep 2004
Location: Pennsylvania, US
Posts: 1,550
Thanks: 2
Thanked 16 Times in 15 Posts
There isn't really an off the shelf that wouldn't be complicated, but many of the basic php/mysql tutorials cover taking form data and storing it in a database.

here's one...
http://www.tizag.com/mysqlTutorial/

As you go through the tutorial, if something doesn't make sense, feel free to post what code you do have in the php section and I'm sure someone will be willing to point you in the right direction.

It's not difficult, but it can get tricky for someone new to it. But that's the best way to learn.
Reply With Quote
  #3 (permalink)  
Old 03-10-09, 01:53 PM
northernsky northernsky is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the lead

I'll see what I can cook up using the tutorials.
Reply With Quote
  #4 (permalink)  
Old 03-11-09, 11:31 AM
northernsky northernsky is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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 = "nobloque";
$pass = "calbears";
$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
Reply With Quote
  #5 (permalink)  
Old 03-11-09, 12:40 PM
bizzar528's Avatar
bizzar528 bizzar528 is offline
Community Liaison
 
Join Date: Sep 2004
Location: Pennsylvania, US
Posts: 1,550
Thanks: 2
Thanked 16 Times in 15 Posts
Put the return comments inside an if statement...

PHP Code:

if(isset($_POST)){


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

$query mysql_query($sql);


Make sure to use the [php] bb tags, makes things color coded and neater.

And be careful not to post your mysql password again. This forum doens't always let you edit after some time.
Reply With Quote
  #6 (permalink)  
Old 03-11-09, 01:14 PM
northernsky northernsky is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Don't let on about the password...

We seem to have stalemate. I'm getting this it the bottom of the screen:
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 131
after splicing in your code.

The complete code (with password cleverly removed) is here:

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


<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 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 (line below)

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

</table>

</body>
</html>

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

So there are still two glitches:

1) We're getting an echo of

select * from survey where name= and comments=''

on the screen even using your fix thus:


if(isset($_POST)){

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

$query = mysql_query($sql);

}



2) 'Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource' relates to this line:

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


Any clues?
Reply With Quote
  #7 (permalink)  
Old 03-12-09, 05:56 AM
bizzar528's Avatar
bizzar528 bizzar528 is offline
Community Liaison
 
Join Date: Sep 2004
Location: Pennsylvania, US
Posts: 1,550
Thanks: 2
Thanked 16 Times in 15 Posts
Well the first is because your echoing out your sql statement...

PHP Code:

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

echo 
$sql; <--- 
And the second is because your trying to parse results from your query and not your actual result set...

PHP Code:

mysql_query($query); 

Will run the query, but you need to assign the results to a variable.

PHP Code:

$results mysql_query($query);


while (
$row mysql_fetch_array($results)) { 
Reply With Quote
  #8 (permalink)  
Old 03-12-09, 02:48 PM
northernsky northernsky is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Brilliant. The form now squirts the punter's data straight into my db as required. This leaves one slight problem.

At the bottom of the page, I want to display two fields from each of the last (say) 15 records. That is, the 15 most recent. I thought the code I have would serve up these fields for the whole db, but actually it gives only the last record.

<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>

How would you iterate this for the last x records?

As ever, many thanks.

Simon
Reply With Quote
  #9 (permalink)  
Old 03-12-09, 07:07 PM
bizzar528's Avatar
bizzar528 bizzar528 is offline
Community Liaison
 
Join Date: Sep 2004
Location: Pennsylvania, US
Posts: 1,550
Thanks: 2
Thanked 16 Times in 15 Posts
You'll need a new result set that contains the last 15 results...

PHP Code:

<table class=questiontable cellspacing=2>

<?php
$last15query 
" ";
$results2 mysql_query($last15query);

while (
$row mysql_fetch_array($results2)) {
echo 
"<tr>
<td class='feedback'>"
$row['region']. "</td>
<td class='feedback'>"
$row['comments']. "</td></tr>";
}
?>
</table>
What would you write as the query for $last15query??

Consider it the newbie test. You have to write the query.
Reply With Quote
  #10 (permalink)  
Old 03-12-09, 08:45 PM
northernsky northernsky is offline
Newbie Coder
 
Join Date: Mar 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Nice one Bizzar. Thanks. I'm on the home straight...

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML Form 1 -> Perl -> return response to HTML form 2 Oleks Perl 13 10-18-06 04:59 PM
SQL with PHP, problem DenKain PHP 5 10-14-06 09:51 AM
PHP and Database HTML store NoMercy PHP 5 04-22-06 03:24 PM
ASP help for validation of HTML form with javascript dhanashree ASP 2 03-22-06 01:28 PM
change my field in this example sal21 ASP 3 07-14-03 02:49 AM


All times are GMT -5. The time now is 09:24 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.