0

I am trying to take three questions yes or no questions and make them post into a database so I can analyse trends at a later date, I currently have the totals being tallied by JS and the result revealed by Jquery but I want to take it a step forward and hold onto the information from the three questions but I'm having no success I'm not getting an error message but it isn't creating entries into the database. The JS is still working however. Does anyone have any ideas on where I am going wrong? Thanks in advance!

<?php

$host="XXXXXXXX"; // Host name 
$username="XXX"; // Mysql username 
$password="XXX"; // Mysql password 
$db_name="XXXXXX"; // Database name 
$tbl_name="test_mysql"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
$question0=$_POST['question1'];
$question1=$_POST['question2'];
$question2=$_POST['question3'];

// Insert data into mysql 
$sql="INSERT INTO $tbl_name(Q1, Q2, Q3)VALUES('$question0', '$question1', '$question2')";
$result=mysql_query($sql);

?>



  <p class="click" id="blue"> You are only charged interest on the amount that is remaining at the end of the month<br>
                <input type="radio" name="question0" value="A" class="click" id="blue"> True </radio>
                <input type="radio" name="question0" value="B" class="click" id="blue"> False </radio>
            
               <p class="click" id="blue">I have to pay off the balance in full every month <br>
                <input type="radio" name="question1" value="A" class="click" id="blue"> True </radio> 
                <input type="radio" name="question1" value="B" class="click" id="blue"> False </radio> 
               <p class="click" id="blue">If I don't make a payment my credit score will be unaffected<br id="blue">
                <input type="radio" name="question2" value="A" class="click" id="blue"> True </radio>
                <input type="radio" name="question2" value="B" class="click" id="blue"> False </radio></p>
                
<button type="button" class="btn btn-primary btn-xl page-scroll" onclick = "returnScore()" id="click" width="560px">Results</button>
Jonas
  • 121,568
  • 97
  • 310
  • 388
Callum K
  • 183
  • 1
  • 12
  • 1
    uh... you need to actually POST those inputs for the PHP part to do something. You're currently not sending anything to your backend. Not that we can see at least. – ffflabs Mar 16 '17 at 22:29
  • 1
    If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Mar 16 '17 at 22:39

1 Answers1

1

First of all you can use the ENUM type to store answers with a schema similar to:

CREATE TABLE answers (
    id int NOT NULL AUTO_INCREMENT,
    q1 enum('true','false') DEFAULT 'false',
    q2 enum('true','false') DEFAULT 'false',
    q3 enum('true','false') DEFAULT 'false',
    primary key (id)
);

Then in PHP you can do:

$question1 = $_POST['question1'] == 'A' ? 'true' : false;
$question2 = $_POST['question2'] == 'A' ? 'true' : false;
$question3 = $_POST['question3'] == 'A' ? 'true' : false;

Also and it is very important:

  • mysql_* PHP extension has been deprecated for years now and does not even exists anymore in PHP7. Please consider upgrading to mysqli_* or even better, PDO.
  • You are not escaping database input. You are at risk of SQL injection and in Gab's utopian country, the sentence is 6 years in jail. It is very important to escape database input and, even better, use parameterized queries.
Gab
  • 3,404
  • 1
  • 11
  • 22