Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
HTML
<html>
<head>
<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
    text-align: left;
}
</style>
</head>
<body>
<form method="post">
  <h4>Answer all the questions on the scale (1)Strongly agree to (5)Strongly disagree </h4>
  <table width="1000" border="1" cellpadding="10">
    <tbody>
      <tr>
        <th style="width:50% ; text-align:center" scope="col">Questions</th>
        <th style="width:10% ; text-align:center" scope="col">Strongly agree</th>
        <th style="width:10% ; text-align:center" scope="col">Agree</th>
        <th style="width:10% ; text-align:center" scope="col">Neutral</th>
        <th style="width:10% ; text-align:center" scope="col">Disagree</th>
        <th style="width:10% ; text-align:center" scope="col">Strongly disagree</th>
      </tr>
      <tr>
        <td>1.People are clear about their role and performance levels expected of them.</td>
        <td><input type="radio" name="q1" value="q1a">
          1</td>
        <td><input type="radio" name="q1" value="q1b">
          2</td>
        <td><input type="radio" name="q1" value="q1c">
          3</td>
        <td><input type="radio" name="q1" value="q1d">
          4</td>
        <td><input type="radio" name="q1" value="q1e">
          5</td>
      </tr>
      <tr>
        <td>2.People understand how their role fits with the responsibilities of others.</td>
        <td><input type="radio" name="q2" value="q2a">
          1</td>
        <td><input type="radio" name="q2" value="q2b">
          2</td>
        <td><input type="radio" name="q2" value="q2c">
          3</td>
        <td><input type="radio" name="q2" value="q2d">
          4</td>
        <td><input type="radio" name="q2" value="q2e">
          5</td>
      </tr>
      <tr>
        <td>3.People understand the contribution they are making to organisational objectives.</td>
        <td><input type="radio" name="q3" value="q3a">
          1</td>
        <td><input type="radio" name="q3" value="q3b">
          2</td>
        <td><input type="radio" name="q3" value="q3c">
          3</td>
        <td><input type="radio" name="q3" value="q3d">
          4</td>
        <td><input type="radio" name="q3" value="q3e">
          5</td>
      </tr>
      <tr>
        <td>4.People are clear about the behaviours expected of them in achieving their objectives.</td>
        <td><input type="radio" name="q4" value="q4a">
          1</td>
        <td><input type="radio" name="q4" value="q4b">
          2</td>
        <td><input type="radio" name="q4" value="q4c">
          3</td>
        <td><input type="radio" name="q4" value="q4d">
          4</td>
        <td><input type="radio" name="q4" value="q4e">
          5</td>
      </tr>
      <tr>
        <td>5.Values and attributes are aligned with each other and support critical behaviour such as customer service or innovation.</td>
        <td><input type="radio" name="q5" value="q5a">
          1</td>
        <td><input type="radio" name="q5" value="q5b">
          2</td>
        <td><input type="radio" name="q5" value="q5c">
          3</td>
        <td><input type="radio" name="q5" value="q5d">
          4</td>
        <td><input type="radio" name="q5" value="q5e">
          5</td>
      </tr>
      <tr>
        <td>6.People feel the organisational systems are 'in sync' with their objectives.</td>
        <td><input type="radio" name="q6" value="q6a">
          1</td>
        <td><input type="radio" name="q6" value="q6b">
          2</td>
        <td><input type="radio" name="q6" value="q6c">
          3</td>
        <td><input type="radio" name="q6" value="q6d">
          4</td>
        <td><input type="radio" name="q6" value="q6e">
          5</td>
      </tr>
      <tr>
        <td>7.People believe they are given space to try new ideas or ways of doing things.</td>
        <td><input type="radio" name="q7" value="q7a">
          1</td>
        <td><input type="radio" name="q7" value="q7b">
          2</td>
        <td><input type="radio" name="q7" value="q7c">
          3</td>
        <td><input type="radio" name="q7" value="q7d">
          4</td>
        <td><input type="radio" name="q7" value="q7e">
          5</td>
      </tr>
      <tr>
        <td>8.Knowledge-sharing enables people to learn from one another.</td>
        <td><input type="radio" name="q8" value="q8a">
          1</td>
        <td><input type="radio" name="q8" value="q8b">
          2</td>
        <td><input type="radio" name="q8" value="q8c">
          3</td>
        <td><input type="radio" name="q8" value="q8d">
          4</td>
        <td><input type="radio" name="q8" value="q8e">
          5</td>
      </tr>
      <tr>
        <td>9.Cross-functional working enables people to learn from others' experiences.</td>
        <td><input type="radio" name="q9" value="q9a">
          1</td>
        <td><input type="radio" name="q9" value="q9b">
          2</td>
        <td><input type="radio" name="q9" value="q9c">
          3</td>
        <td><input type="radio" name="q9" value="q9d">
          4</td>
        <td><input type="radio" name="q9" value="q9e">
          5</td>
      </tr>
      <tr>
        <td>10.People watch for changes within the external world that might impact on the organisation.</td>
        <td><input type="radio" name="q10" value="q10a">
          1</td>
        <td><input type="radio" name="q10" value="q10b">
          2</td>
        <td><input type="radio" name="q10" value="q10c">
          3</td>
        <td><input type="radio" name="q10" value="q10d">
          4</td>
        <td><input type="radio" name="q10" value="q10e">
          5</td>
      </tr>
    </tbody>
  </table>
  <input type="submit" id="submit" name="submit">
</form>
</body>
</html>


PHP
$servername = "localhost";
$username = "root";
$password = "*********";
$dbname = "survey";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// Create database
$sql = "CREATE DATABASE alignment";
if ($conn->query($sql) === TRUE) {
    echo "Database created successfully";
} else {
    echo "Error creating database:alignment " . $conn->error;
}

$conn->close();

// create table
$sql = "CREATE TABLE survey (
sid INT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
q1 VARCHAR(20) NOT NULL,
q2 VARCHAR(20) NOT NULL,
q3 VARCHAR(20) NOT NULL,
q4 VARCHAR(20) NOT NULL,
q5 VARCHAR(20) NOT NULL,
q6 VARCHAR(20) NOT NULL,
q7 VARCHAR(20) NOT NULL,
q8 VARCHAR(20) NOT NULL,
q9 VARCHAR(20) NOT NULL,
q10 VARCHAR(20) NOT NULL,
)";

if ($conn->query($sql) === TRUE) {
    echo "Table survey created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

$conn->close();


//insert values
$sql = "INSERT INTO survey (sid, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10,)
VALUES (' ', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>


What I have tried:

first i had only the html code. I dont know much of php and db still i managed to write some code. But i dont know the sequence for it. Also i tried executing it but it shows errors. It is not connecting to database on xampp. Please can someone tell me where i am going wrong.
Posted
Updated 11-May-16 21:43pm
v3

1 solution

Try this:

PHP
$sql = "INSERT INTO survey (sid, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10,)
VALUES (' ', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?')";


You can get each of the question value like below :

PHP
$q1 = isset($_POST['q1']) ? $_POST['q1'] : 0;
$q2 = isset($_POST['q2']) ? $_POST['q2'] : 0;
$q3 = isset($_POST['q3']) ? $_POST['q3'] : 0;
$q4 = isset($_POST['q4']) ? $_POST['q4'] : 0;
$q5 = isset($_POST['q5']) ? $_POST['q5'] : 0;
$q6 = isset($_POST['q6']) ? $_POST['q6'] : 0;
$q7 = isset($_POST['q7']) ? $_POST['q7'] : 0;
$q8 = isset($_POST['q8']) ? $_POST['q8'] : 0;
$q9 = isset($_POST['q9']) ? $_POST['q9'] : 0;
$q10 = isset($_POST['q10']) ? $_POST['q10'] : 0;


and then in your PHP code, you can do the following :

PHP
// Prepared Statement
$stmt = $conn->prepare("INSERT INTO survey (q1, q2, q3, q4, q5, q6, q7, q8, q9, q10) VALUES ('?', '?', '?', '?', '?', '?', '?', '?', '?', '?')");

// Bind the required parameters along-with type(string (as you have the column type as varchar)) 
$stmt->bind_param("ssssssssss", $q1, $q2, $q3, $q4, $q5, $q6, $q7, $q8, $q9, $q10);

// execute the statement to INSERT the data in DB.
$stmt->execute();


Though I have not tested the above code, but I hope this will help you :). Please write me back if you need any further assistance.

Note: You have mentioned, you are getting error for DB connection, that means you must be getting some error message for $conn->connect_error. Please paste the error you are getting so that we can further help you accordingly.
 
Share this answer
 
v2
Comments
Member 12518338 13-May-16 2:49am    
only the first value is getting stored into the database. Can u suggest why this is happening. only the q1 column is storing the answer(q1a/q1b/q1c/q1d/q1e). Other nine columns show the answer as 0.
Prava-MFS 13-May-16 3:21am    
Have you tried executing my code? If yes, then please paste the value of $q2, $q3, ..., $q10 value over here.
Member 12518338 13-May-16 4:49am    
Fatal error: Call to a member function prepare() on line 23
this is the line
$stmt = $conn->prepare("INSERT INTO survey (q1, q2, q3, q4, q5, q6, q7, q8, q9, q10) VALUES ('?', '?', '?', '?', '?', '?', '?', '?', '?', '?')");
Member 12518338 13-May-16 5:18am    
Tried your code only. With the code i used only first value of $q1 is getting stored, rest 9 values are 0.
Prava-MFS 16-May-16 1:56am    
Paste here the error message you are getting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900