7

I am using PHP/MYSQL to create a form that includes radio buttons. I am trying and add the value of checked radio buttons to a table within a database. At the minute I can't get anything to add to the database. The table is called assessment.

QUESTION1.PHP

<?php 
include 'core/init.php';
include 'includes/overall/overall_header.php';
protect_page();
include 'includes/menu.php';
include 'includes/overall/navigate.php';
include 'includes/widgets/loggedin.php';

?>      

<h1>Assessment</h1>

<form action="save.php" method="post">

<p class="p1">
Question 1</p>
<p class="p4">
Are you tall or short?</p>

<p class="p3"> 
<input type="radio" name="q1" value="1" />
1
<input type="radio" name="q1" value="2" />
2
<input type="radio" name="q1" value="3" />
3
<input type="radio" name="q1" value="4" />
4
<input type="radio" name="q1" value="5" />
5
</p><br><br>
</form>

<img src="Images/image1.png" alt="Submit" class="thumbnail" align="right" width="58"      height="52" id="question2">
<img src="Images/save.png" alt="Submit" class="thumbnail" align="right" width="65" height="52">

<?php
}
 include 'includes/overall/overall_footer.php'; 
 ?>  

SAVE.PHP

<?php
session_start();
include('connection.php');
$q1=$_POST['q1'];
mysql_query("INSERT INTO `assessment` (q1) VALUES ('$q1')");
header("location: question2.php?");
mysql_close($con);
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Steven Trainor
  • 1,255
  • 4
  • 13
  • 20
  • I assume you checked the contents of the `$_POST`-variable in your save.php-page? Next, try to perform your query manually in your db by replacing `$q1` with a proper value. If that works, there's probably something wrong with your db connection. – Marty McVry Apr 07 '13 at 23:22
  • 1
    Welcome to Stack Overflow! [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Apr 07 '13 at 23:47
  • What are the contents of `$_POST['q1']` on save.php. Do you get any errors? – Explosion Pills Apr 07 '13 at 23:53
  • After a bit of messing about it it i have now got the values to add to the table from the form. Now i have another problem, i am trying to do a questionnaire with a different question on every page but every time i select a radio button and proceed to the next page the radio button is added to a new row rather than the same row but the next column :/ – Steven Trainor Apr 08 '13 at 00:15

4 Answers4

4

i would like to know, what is the purpose of these two image TAG. i just changed those img tag into button image .

<h1>Assessment</h1>
<form action="save.php" method="post">
  <p class="p1"> Question 1</p>
  <p class="p4"> Are you tall or short?</p>
  <p class="p3">
    <input type="radio" name="q1" value="1" />
    1
    <input type="radio" name="q1" value="2" />
    2
    <input type="radio" name="q1" value="3" />
    3
    <input type="radio" name="q1" value="4" />
    4
    <input type="radio" name="q1" value="5" />
    5 </p>
  <br>
  <br>
  <input type="image" src="Images/image1.png" />
  <input type="image" src="Images/save.png" />
</form>

save.php

 <?php
    if (isset($_POST['q1'])){
        $q1 = $_POST['q1'];
        mysql_query("INSERT INTO assessment (q1) VALUES ('$q1')");
    }
    ?>
Bharanikumar
  • 25,457
  • 50
  • 131
  • 201
2
if (isset($_POST['q1'])) {
    $q1 = $_POST['q1'];
    $stmt = $db->prepare("INSERT INTO members (q1) VALUES (:q1)");

    $stmt->execute(array(':q1' => $_POST['q1']));
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
-1

There is a radio button of gender to choose from male and female

<?php
$gender=$_POST['radiobutton_name'];
$sql="INSERT INTO table_name(column_name)
VALUES('$gender')";
?>
Aatish Sai
  • 1,647
  • 1
  • 26
  • 41
Av3ng34
  • 7
  • 3
-1

I am just follow your code and giving solution. You need to just few changes in save.php as follow:

<?php

if (isset($_POST['q1'])) {
    $q1 = $_POST['q1'];
    $sql = mysql_query("INSERT INTO assessment (q1) VALUES ('$q1')") or die("MySQL Error : ".mysql_error($conn));
    if ($sql) {
        echo "Data Saved";
    } else {
        echo "Data not saved";
    }
}

If find any issue please let me know.

Important Note : Please avoid mysql_* kind of function its deprecated in PHP 5.5.0 and onwards.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Prabhu Nandan Kumar
  • 1,205
  • 12
  • 22