Jump to content

Basic php: Form to sql and back to page


northernsky

Recommended Posts

I'm working on my first ever PHP script, and it's causing stress.

 

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.

 

When the user clicks the 'send' button, the data is 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. So the data goes from the form on the page to the sql db, then two fields from that new record come back to the page and are added at the top of a table under the original form.

 

I think the code to send data to the db is working, but bouncing it back and building the table are another matter. Here is the code so far:

 

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

 

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

 

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

 

I'm still at the stage where php code is completely opaque, so I'd be very grateful if anyone could suggest where I'm going wrong.

 

The fields that I want to display are called 'region' and 'comments'.

 

Many thanks,

 

Simon

Link to comment
https://forums.phpfreaks.com/topic/148997-basic-php-form-to-sql-and-back-to-page/
Share on other sites

Hi, Redarrow.

 

Yes, I do, because I don't just want the last submission echoed — I want a table of two fields from each record in the DB. When the thing is working (!) I'll start to look into how to restrict it to the last 20 submissions.

Thanks again, Redarrow. I'll give it a try tomorrow. Unfortunately it's all blown up in my face. I've discovered that the form wasn't feeding anything into the db after all. It was empty, despite the apparent confidence with which the form was dispatching my dummy data and the complete lack of error messages. I've had enough of this for one day. Here's the code, in case you can spot anything.

 

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

 

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

OK: panic over. The form now squirts the user's data into the db as required. Great. There's just one problem left to fix.

 

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

while ($row = mysql_fetch_array($query)) {

echo "<tr><td class='feedback'>". $row['region']. "</td><td class='feedback'>". $row['comments']. "</td></tr>";}

?>

</table>

 

Can anyone suggest how to iterate this for the last x records?

 

Many thanks,

 

Simon

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.