@ Barand,
sorry to bother you again.
I tried to do it your way but I'm little confused, I'm really beginner with a big B, so please have patience.
I'll try to post what I have done so far.
This is my .php file used to connect to db, it is not viewed in browser:
<?php
$server = "localhost";
$username = "xyz";
$password = ""123;
//mysql server connection
if (!$spoj=@mySQL_connect($server, $username, $password))
{
die ("<b>Connection to DB error</b><br>");
}
else{
echo "<b>Connected successfully, right DB.<b>" ;
}
//select DB
if (!mySQL_select_db("xyz", $spoj))
{
die ("<b>Connected successfully.</b><br>");
}
else{
echo "<b>Connection error</b><br>" ;
}
$sql = "CREATE TABLE person (
persId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50)
)";
$db->query($sql);
$sql = "INSERT INTO person (name) VALUES
('John Doe'),
('Jane Brown'),
('Harry Potter')";
$db->query($sql);
$sql = "CREATE TABLE attribute (
attrId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
description VARCHAR(50)
)";
$db->query($sql);
$sql = "INSERT INTO attribute (description) VALUES
('Hair'),
('Clothes'),
('Education'),
('Other'),
('Etc')";
$db->query($sql);
$sql = "CREATE TABLE rating (
ratingId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
persId INT,
attrId INT,
rating TINYINT
)";
$db->query($sql);
?>
This is file that I want to use for selecting grades, of course it is viewed in browser. This is biggest problem for me, can't really understand how to connect choosing User ID and selecting it from DB, also how to connect ratings with DB:
<?php
include "spoj.php";
$sql = "CREATE TABLE person (
persId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50)
)";
$db->query($sql);
$sql = "INSERT INTO person (name) VALUES
('John Doe'),
('Jane Brown'),
('Harry Potter')";
$db->query($sql);
$sql = "CREATE TABLE attribute (
attrId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
description VARCHAR(50)
)";
$db->query($sql);
$sql = "INSERT INTO attribute (description) VALUES
('Hair'),
('Clothes'),
('Education'),
('Other'),
('Etc')";
$db->query($sql);
$sql = "CREATE TABLE rating (
ratingId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
persId INT,
attrId INT,
rating TINYINT
)";
$db->query($sql);
//
// define the options for the rating dropdowns
//
$ratings = "<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>";
//
// use attribute table to build the rating selections
// and alsoto store the headings for the report
//
$sql = "SELECT attrId, description
FROM attribute
ORDER BY attrId";
$res = $db->query($sql);
$ratingSelections = '';
$attrs = array();
while (list($id, $des) = $res->fetch_row()) {
$ratingSelections .= "<div class='descrip'>$des</div>
<div class='rating'><select name='rating[$id]'>$ratings</select></div>
<div style='clear:both'></div>";
$attrs[$id] = $des; // store heads for table
}
?>
<form method="POST" action="upis.php">
<Select ID <br />
<label for="select-choice"></label>
<select id="selected" name="selected" class="select" value="test">
<option value="persId">John Doe</option>
<option value="persId">Jack Jonhson</option>
<option value="persId">John Jackson</option>
</select>
<input type="submit" value="Upis" name="upis">
<br>
</form>
This is .php file I want to use for submitting ratings, not viewed in a browser:
<?php
include "spoj.php";
if (isset($_POST['persid'])) {
// id of person being rated
$persid = intval($_POST['persid']);
// insert a row for each attribute into rating table
$data = array();
foreach ($_POST['rating'] as $attrid => $rating) {
$data[] = sprintf("(%d, %d, %d)", $persid, intval($attrid), intval($rating));
}
$sql = "INSERT INTO rating (persId, attrId, rating) VALUES " . join(',', $data);
$db->query($sql);
}
?>
This is .php file used for presenting data from DB, of course viewed in browser. Big problem for me is how to present this data in a browser:
<?php
include "spoj.php";
$query="SELECT * FROM rating";
$q=mysql_query($query);
if (mysql_num_rows($q)==0)
{
echo "There is no data in table!";
}
else
{
/// I know I need rows names etc, but not sure how to do it!
?>
I did not paste HTML code, assuming it is irrelevant. Connection with server and DB is OK, but I can't get tables inserted into DB.
I know this is not the best way to design whole process, but it's the best I could make. Hopefully you can help me.
Thanks.