How to insert chosen radio to table?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

How to insert chosen radio to table?

Post by nitediver »

i have page like this

Code: Select all

<form name="char" method="post" action="process.php">
<table width="90%" border="0">
  <tr>
    <td><img src="img/moghul.gif" width="64" height="64"><br>
    Moghul</td>
    <td><img src="img/dragon-knight.gif" width="64" height="64"><br>
    Dragon</td>
    <td><img src="img/Huth.gif" width="64" height="64"><br>
      Wolf</td>
  </tr>
  <tr>
    <td><input type="radio" name="hero" value="moghul" id="moghul"></td>
    <td><input type="radio" name="hero" value="dragon"></td>
    <td><input type="radio" name="hero" value="wolf"></td>
  </tr>
</table>
<input type="submit" name="submit" value="submit">
</form>
 
process.php

Code: Select all

<?
include "connect.php";
mysql_query("insert into dotable(hero) values('$hero')");
echo "OK";
?>
 
The database is connected, but when I press submit the data doesn’t insert into the table.
What’s wrong, is it the data type?
What is the suitable data type for radio, actually? Im new in php
Last edited by Benjamin on Wed May 27, 2009 10:04 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: How to insert chosen radio to table?

Post by jaoudestudios »

Dont use $hero, you need to use $_POST['hero'].

And change your query to...

Code: Select all

INSERT INTO dotable SET hero = '$_POST['hero']'
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Re: How to insert chosen radio to table?

Post by lmg »

I am having a similar problem although my code is like so:

html:

Code: Select all

<td width=60%><font face="Arial">This is the first question.<br><br>
    <INPUT TYPE="radio" NAME="group1" VALUE="a"><a href="#Definitions" title="Click for more information.">Choice1</a>
    &nbsp; &nbsp;<!--These insert a non breaking space (similar to using tab in a text document).-->
    <INPUT TYPE="radio" NAME="group1" VALUE="b"><a href="#Definitions" title="Click for more information.">Choice2</a>
    &nbsp; &nbsp;
    <INPUT TYPE="radio" NAME="group1" VALUE="c"><a href="#Definitions" title="Click for more information.">Choice3</a>
    &nbsp; &nbsp;
    <INPUT TYPE="radio" NAME="group1" VALUE="d"><a href="#Definitions" title="Click for more information.">Choice4</a>
    &nbsp; &nbsp;
    <INPUT TYPE="radio" NAME="group1" VALUE="e"><a href="#Definitions" title="Click for more information.">Choice5</a><br><br>

php:

Code: Select all

$answer1=$_POST['group1'];
$answer2=$_POST['group2'];
$answer3=$_POST['group3'];

Code: Select all

 
//other code here
$query = "INSERT INTO data VALUES ('$answer1','$answer2','$answer3','$comment1','$comment2', '$comment3','','','','','','$test')";
mysql_query($query);
It will access the database, but when I look in the database, the only thing written are the '' entries and $test which is hardcoded.

Is my syntax incorrect somewhere?
Last edited by Benjamin on Fri May 29, 2009 10:22 am, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: How to insert chosen radio to table?

Post by jaoudestudios »

You have no form tags! Add form tags to your html.

I dont see a 'test' form field??!?
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Re: How to insert chosen radio to table?

Post by lmg »

Sorry..I didn't post the entire code as it was pretty lengthy. Here is the whole code:

html

Code: Select all

<!--Gather all data and input into database-->
<?php 
/*session_start();
 
if (!isset($_SESSION['username'])){
    header("Location: login.php");
}*/
?>
<html>
<head>
<title>Self Audit</title>
</head>
<body>
<form method="link" action="selfAudit_adaptedPHP.php">
 
<h1><center><font face="Trebuchet MS">Self Audit</font></center></h1>
<hr>
<br>
<a href="session.php"><font face="Arial">Return to Main Menu</font></a><br><br>
<br>
<font face="Arial"><b>Section 1: This is Section One</b></font><br><br><br>
<table width=100% border=0 cellspacing=0 cellpadding=5 width=616>
<tr>
 
<td width=60%><font face="Arial">This is the first question.<br><br>
    <INPUT TYPE="radio" NAME="group1" VALUE="a"><a href="#Definitions" title="Click for more information.">Choice1</a>
    &nbsp; &nbsp;<!--These insert a non breaking space (similar to using tab in a text document).-->
    <INPUT TYPE="radio" NAME="group1" VALUE="b"><a href="#Definitions" title="Click for more information.">Choice2</a>
    &nbsp; &nbsp;
    <INPUT TYPE="radio" NAME="group1" VALUE="c"><a href="#Definitions" title="Click for more information.">Choice3</a>
    &nbsp; &nbsp;
    <INPUT TYPE="radio" NAME="group1" VALUE="d"><a href="#Definitions" title="Click for more information.">Choice4</a>
    &nbsp; &nbsp;
    <INPUT TYPE="radio" NAME="group1" VALUE="e"><a href="#Definitions" title="Click for more information.">Choice5</a><br><br>
    <font face="Arial" value="comment1">Comments:<br>
    <TEXTAREA COLS="75"></TEXTAREA>
<td width=40%><table border=1 cellspacing=0 cellpadding=0 width=100% bgcolor=#FF6600>
        <td><font face="Arial">More information here.</td></tr></table></td>
</tr>
 
<tr>
<td width=60%><font face="Arial">This is the second question.<br><br>
    <INPUT TYPE="radio" NAME="group2" VALUE="a"><a href="#Definitions" title="Click for more information.">Choice1</a>
    &nbsp; &nbsp;<!--These insert a non breaking space (similar to using tab in a text document).-->
    <INPUT TYPE="radio" NAME="group2" VALUE="b"><a href="#Definitions" title="Click for more information.">Choice2</a>
    &nbsp; &nbsp;
    <INPUT TYPE="radio" NAME="group2" VALUE="c"><a href="#Definitions" title="Click for more information.">Choice3</a>
    &nbsp; &nbsp;
    <INPUT TYPE="radio" NAME="group2" VALUE="d"><a href="#Definitions" title="Click for more information.">Choice4</a>
    &nbsp; &nbsp;
    <INPUT TYPE="radio" NAME="group2" VALUE="e"><a href="#Definitions" title="Click for more information.">Choice5</a><br><br>
    <font face="Arial">Comments:<br>
    <TEXTAREA COLS="75" value="comment2"></TEXTAREA>
<td width=40%><table border=1 cellspacing=0 cellpadding=0 width=100% bgcolor=#FF6600>
        <td><font face="Arial">More information here.</td></tr></table></td>
</tr>
 
<tr>
<td width=60%><font face="Arial">This is the third question.<br><br>
    <INPUT TYPE="radio" NAME="group3" VALUE="a"><a href="#Definitions" title="Click for more information.">Choice1</a>
    &nbsp; &nbsp;<!--These insert a non breaking space (similar to using tab in a text document).-->
    <INPUT TYPE="radio" NAME="group3" VALUE="b"><a href="#Definitions" title="Click for more information.">Choice2</a>
    &nbsp; &nbsp;
    <INPUT TYPE="radio" NAME="group3" VALUE="c"><a href="#Definitions" title="Click for more information.">Choice3</a>
    &nbsp; &nbsp;
    <INPUT TYPE="radio" NAME="group3" VALUE="d"><a href="#Definitions" title="Click for more information.">Choice4</a>
    &nbsp; &nbsp;
    <INPUT TYPE="radio" NAME="group3" VALUE="e"><a href="#Definitions" title="Click for more information.">Choice5</a><br><br>
    <font face="Arial">Comments:<br>
    <TEXTAREA COLS="75" value="comment3"></TEXTAREA>
<td width=40%><table border=1 cellspacing=0 cellpadding=0 width=100% bgcolor=#FF6600>
        <td><font face="Arial">More information here.</td></tr></table></td>
</tr>
</table>
 
<!--End of survey-->
 
<br>
<input type="submit" name="continue" value="Continue">
<br>
<br>
<br>
 
<!--Definitions Table-->
 
<a name=Definitions>
<font face="Arial"><b>Definitions</b></a>
 
<table border=1 cellspacing=0 cellpadding=10 width=100%>
<tr>
<td align=center><font face="Arial"><b>C1</b></td>
<td><font face="Arial">Choice 1</td>
<td><font face="Arial">Description</td>
</tr>
 
<tr>
<td align=center><font face="Arial"><b>C2</b></td>
<td><font face="Arial">Choice 2</td>
<td><font face="Arial">Description</td>
</tr>
 
<tr>
<td align=center><font face="Arial"><b>C3</b></td>
<td><font face="Arial">Choice 3</td>
<td><font face="Arial">Description</td>
</tr>
 
<tr>
<td align=center><font face="Arial"><b>C4</b></td>
<td><font face="Arial">Choice 4</td>
<td><font face="Arial">Description</td>
</tr>
 
<tr>
<td align=center><font face="Arial"><b>C5</b></td>
<td><font face="Arial">Choice 5</td>
<td><font face="Arial">Description</td>
</tr>
</table>
 
<!--End Definitions Table-->
<br>
<br>
 
<hr>
<p align="right"><font face="Arial">Page Information &copy; 2009</font></p>
 
</form>
</div>
</body>
</html>

php

Code: Select all

 
<?php
 
$answer1=$_POST['group1'];
$answer2=$_POST['group2'];
$answer3=$_POST['group3'];
$comment1=$_POST["comment1"];
$comment2=$_POST["comment2"];
$comment3=$_POST["comment3"];
$test="1";
 
echo "Answer1: ".$answers1;
echo "Answer2: ".$answers2;
echo "Answer3: ".$answers3;
echo "Comment1: ".$comment1;
echo "Comment2: ".$comment2;
echo "Comment3: ".$comment3;
echo "Test: ".$test;
 
$username="test2";
$password="test";
$database="test2";
 
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
 
$query = "INSERT INTO data VALUES ('$answer1','$answer2','$answer3','$comment1','$comment2', '$comment3','','','','','','$test')";
mysql_query($query);
 
mysql_close();
?>
 
<html>
<font face="Arial">Data successfully written to database.</font>
</html>
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: How to insert chosen radio to table?

Post by jaoudestudios »

Post your schema and results too
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Re: How to insert chosen radio to table?

Post by lmg »

Ok never mind. I got it working.

Here are the problems I found:

1. The method in the form tag was set to "link" instead of "post"
2. I had some extra 's' characters in my answer variables ($answers1 instead of $answer1) so it did not output on the next page
3. I forgot the variable $test had to be unique - there was an entry in the database with $test=1 so it wasn't writing my data
4. The variable comment1 in the first code was in the wrong place (not in the <textarea> tag)

I think those are all of the errors I found.

It's amazing what errors one can find if they leave the code alone for awhile :)
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: How to insert chosen radio to table?

Post by jaoudestudios »

lmg wrote:1. The method in the form tag was set to "link" instead of "post"
I did say :P
lmg wrote:Here are the problems I found:

1. The method in the form tag was set to "link" instead of "post"
2. I had some extra 's' characters in my answer variables ($answers1 instead of $answer1) so it did not output on the next page
3. I forgot the variable $test had to be unique - there was an entry in the database with $test=1 so it wasn't writing my data
4. The variable comment1 in the first code was in the wrong place (not in the <textarea> tag)

I think those are all of the errors I found.

It's amazing what errors one can find if they leave the code alone for awhile :)
Thanks for posting your findings, as it will help others in the future.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: How to insert chosen radio to table?

Post by nitediver »

jaoudestudios wrote:Dont use $hero, you need to use $_POST['hero'].

And change your query to...

Code: Select all

INSERT INTO dotable SET hero = '$_POST['hero']'
why it's still doesnt work
could you give me the exact example how to put the code, pls

and how to display the chosen image from database ?
Post Reply