I have a table where ResultHRM column contains a textarea where the user can write comments. I would like to know, when I click on submit button how can update the correct line with the textarea content into my MySQL table NContrib ?
I read that textarea cannot have a value attribute.
My table NContrib has the following structure:
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| IdVariantNContrib | mediumint(9) | NO | PRI | NULL | auto_increment |
| ID | varchar(30) | YES | | NULL | |
| Reference | varchar(20) | YES | | NULL | |
| ResultHRM | varchar(60) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
In my php page, I display this table like that:
<form method='POST' action='SaveValidation.php'>
<?php
require_once 'config.php'; //database connection
$sql='SELECT ID, Reference, ResultHRM FROM NContrib';
$PerformSql=mysqli_query($conn,$sql) or die(mysqli_error($conn));
?>
<table style="width:100%" border='1px' CELLSPACING='0' cellpadding='2'>
<tr>
<th> ID </th>
<th> Reference </th>
<th> ResultsHRM </th>
</tr>
<?php
while($rowNcontrib = mysqli_fetch_assoc($PerformSql)) {
echo "<tr><td> ".$rowNcontrib["ID"]." </td><td> ".$rowNcontrib["Reference"]." </td><td><textarea name='ResultHRM[]' id='ResultHRM[]' cols='30' rows='1'></textarea></td></tr>";
}
?>
</table>
<input type="submit" name="sendEcht" value="Submit" />
</form>
1) use $rowNcontrib['ResultHRM'] in beetween <textarea></textarea> to show value
2) Also use have use name='ResultHRM[]' this will give you a array of textarea values which you have updated but you will not get track of which value belongs to which ID. so name='ResultHRM[$id]'. here id should be the id of row
So your code should be
like
while($rowNcontrib = mysqli_fetch_assoc($PerformSql)) {
$id = $rowNcontrib["ID"];
echo "<tr><td> ".$rowNcontrib["ID"]." </td><td> ".$rowNcontrib["Reference"]." </td><td><textarea name='ResultHRM[$id]' id='ResultHRM[]' cols='30' rows='1'>".$rowNcontrib['ResultHRM ']."</textarea></td></tr>";
}
now in page SaveValidation.php check for this
if(isset($_POST['sendEcht'])){
echo "<pre>";print_r($_POST);die;}
this will show you something like this
Array
(
[ResultHRM] => Array
(
[3] => hello its test
[11] => test string
[10] =>
)
[sendEcht] => Submit
)
// here 3,11, 10 are my id's of table in your case it will be the id's of your table then you can easily update your table `ResultHRM` field
There are certain points that I would like to mention here:
If ResultHRM field is going to hold the values from textarea in form, I would suggest that you change the datatype. Currently, its varchar(60). I suggest, it should be text.
After fetching the data from the database, you are displaying it in the table structure. I would suggest, you add textarea and fill it with existing value. Let's assume you named textarea ResultHRM_ta
Once you will follow point#2, and submit the form, you can have textarea value in $_POST['ResultHRM_ta'] on SaveValidation.php.
Save the value in database.
Please note, I have mentioned the very basic outline for the process you need to follow. You will have to take care of security measures and coding standards on your own.
Tow solve this you can call a AJAX request with the code and pass id for each in the javascript method.
and in html code in the you can call method in each table row as follows:
<table style="width:100%" border='1px' CELLSPACING='0' cellpadding='2'>
<tr>
<th> ID </th>
<th> Reference </th>
<th> ResultsHRM </th>
</tr>
<?php
while($rowNcontrib = mysqli_fetch_assoc($PerformSql)) {
echo "<tr><td> ".$rowNcontrib["ID"]." </td><td> ".$rowNcontrib["Reference"]." </td><td><textarea name='ResultHRM' id='ResultHRM' cols='30' rows='1'></textarea></td><td><button type='button' onclick='update(".$rowNcontrib["ID"].")'">Update</button>;
}
?>
</table>
Related
Does anyone know how can I have two rows on one column but only a single cell
---------------------
| x | |
Z |------| A |
| y | |
------------- -------
My table in PHP looks like this
Trans Table
Transaction ID - 1
Transactiion date - Feb 7
Transdetails Table
Details ID - 1
Transaction ID - 1
Medicine - 1
Details ID - 2
Transaction ID - 1
Medicine - 2
I need to show both medicine on one Transaction ID in one row
With <td rowspan="n"> you can achive that layout with a HTML Table.
<table border="1">
<tr>
<td rowspan=2">z</td>
<td>x</td>
<td rowspan="2">a</td>
</tr>
<tr>
<td>y</td>
</tr>
</table>
Hi I'm having trouble trying to split my db object result into the correct table for display.
Basically I'm trying to acheive this
<div class="panel"> $row->skill_group
<table class="collapse">
<thead>
<tr>
<th>$row->name</th>
</tr>
</thead>
<tbody>
<tr>
<td>$row->skill_name</td>
</tr>
<tr>
<td> $row->competency_name </td>
</tbody>
</table>
</div>
There can be many unique skill_groups and many skill_names but each person and their competency should only be displayed once for each skill.
My mysql returns the correct data however I can't figure out how to loop over the result so that I can split it in between the table that is nested from the div container.
For example the mysql result would be
======================================================================
| skill_group_name | skill_name | competency_name | name
======================================================================
| PHP Frameworks | Codeigniter | Working Knowledge | User name1
| PHP Frameworks | CakePHP | No Knowledge | User name1
| Database | T-SQL | Working Knowledge | User name2
======================================================================
and I need to fit it into the html structure above.
The result I'm after is
======================================================================
PHP Frameworks
======================================================================
| Codeigniter | Working Knowledge | User name1
| CakePHP | No Knowledge | User name1
======================================================================
Database
======================================================================
| T-SQL | Working Knowledge | User name2
======================================================================
I have managed to get the grouping of the skill_group sorted with:
if($row->skill_group_name != $skill_group_name)
{
echo '<div class="panel-heading"><a href="#" data-target=".skill'.++$counter.'" data-toggle="collapse">' . $row->skill_group_name;
$skill_group_name = $row->skill_group_name;
echo '</a></div>';
}
But I'm missing how to write the loops for the data table itself, how do I do this?
As I commented, I think you should save to an array, then implode() the sections:
//Just some fake data
$array[] = array('PHP Frameworks','Codeigniter','Working Knowledge','User name1');
$array[] = array('PHP Frameworks','CakePHP','No Knowledge','User name1');
$array[] = array('Database','T-SQL','Working Knowledge','User name2');
// Loop through the data
foreach($array as $row) {
// Save the table column html to array
$section[$row[0]][] = '<tr><td>'.$row[1].'</td><td>'.$row[2].'</td><td>'.$row[3].'</td></tr>';
}
// Loop through the section array you made
foreach($section as $title => $all) {
// Wrap it
echo '<div>'.$title;
// Implode
echo '<table>'.implode(PHP_EOL,$all).'</table>';
echo '</div>';
}
I am trying to make a website for a radio, and I want to make a page which will contain the playlist of the radio. I made a table with rows and columns (Song Name | DJ) and here is the code:
<table style="width:100%">
<h3>Radio PlayList</h3>
<tr>
<th>Song Name</th>
<th>DJ</th>
</tr>
<tr>
<td>Nil</td>
<td>Nil</td>
</tr>
</table>
I want to make a MySQL table that contains the names of the songs and their DJ. I want to make it so, for every ID (song name | dj) a new row is added, something like this:
Song Name | DJ
Song 1 | DJ 1
Song 2 | DJ 2
Song 3 | DJ 3
Assuming that you had already data inserted into your database table. In order to create a row you need to loop the records in the result set returned by the query.
Check example here
http://php.net/manual/en/function.mysql-fetch-array.php
Please specify your question.
You want to create a table? Like this?
CREATE TABLE songs(
id_song INT AUTO_INCREMENT,
song_name CHAR(255),
song_dj CHAR(255),
CONSTRAINT prim_key_songs
PRIMARY KEY (id_song)
);
I have been trying to store an array in a database column, but haven't succeeded yet.
I have been looking into php and sql for two weeks now, and I don't have a programming background. So please forgive my ignorance. I'm sure I've made lot's of mistakes.
Let me explain my situation further.
I created a web page so a admin can fill in some data concerning some game. This is after a login process.
When a admin arrives at this point, I'm not bothering with 'correct' input at the moment. I just want to get the data in the db.
I created a table called games with mysql, and filled it with data I have so far:
+--------+--------+-----+-----+------+------+------+
|game_id |round_id|team1|team2|score1|score2|result|
+--------+--------+-----+-----+------+------+------+
| 1 | 1 | A | B | NULL | NULL | NULL |
| 2 | 1 | C | D | NULL | NULL | NULL |
| 3 | 1 | E | F | NULL | NULL | NULL |
| 4 | 2 | E | C | NULL | NULL | NULL |
| 5 | 2 | A | D | NULL | NULL | NULL |
| 6 | 2 | F | B | NULL | NULL | NULL |
+--------+--------+-----+-----+------+------+------+
Then I created a form in a php page which uses data from this table to display the teams per game.
The admin uses this form to fill in the game score and results. See the form below.This form is also needed to store values in the above table.
input.php
<?php
mysql_connect("xxx", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
$query="SELECT games_team1, games_team2 FROM games WHERE round_id = 1";
$result = mysql_query($query);
?>
<form action="someform.php" method="post">
<table id="inputgameresults">
<tbody>
<tr>
<th>Home</th>
<th>Score 1</th>
<th>-</th>
<th>Score 2</th>
<th>Away</th>
<th>Result</th>
</tr>
<?php while($row = mysql_fetch_array($result)) { ?>
<tr>
<td><?php echo $row['team1']; ?></td>
<td><input type="text" name="score1[]"/></td>
<td> - </td>
<td><input type="text" name="score2[]"/></td>
<td><?php echo $row['team2']; ?></td>
<td><input type="text" name="result[]"/></td>
</tr>
<?php } ?>
<tr>
<td></td>
<td</td>
<td</td>
<td</td>
<td</td>
<td><input type="submit" value="Submit"/></td>
</tr>
</tbody>
</table>
</form>
This results into 3 rows in the table in the form above. This table displays the teams for each match and input fields for the scores and the result.
A header, three rows with the games, input fields behind the games, and a sumbit button below.
A admin can submit the input with the form below. I added the echo so I can check if the output is correct. The echo works like a charm.
This is where things don't go the way I'd like them to go. The sql query I used is stated in the form. It fails miserably, probably because of my lack of knowledge.
form1.php
<?php
mysql_connect("xxx", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
$sc1 = $_POST['score1'];
$sc2 = $_POST['score2'];
$res = $_POST['result'];
foreach($sc1 as $value) {
echo "score team 1 $value<br/>";
mysql_query("UPDATE games SET game_score1 = '".$value[]"' WHERE games_id = [1,2,3] ");
}
foreach($sc2 as $value) {
echo "score team 2$value<br/>";
}
foreach($res as $value) {
echo "res $value<br/>";
}
?>
I'm sure there are a lot things besides the main issue I can improve, or do more efficient. Although this is not my main focus at the moment, any help is welcome :D
The main focus now is to get the input from the form in the right place in the db. The data from the form needs to be send to the database in columns score1, score2 and result for a game.
Any help or advice is most welcome!
a simple way to store a whole array in a database column is to encode the array to json ... in the example, $sc1 is the array you want to store
mysql_query("UPDATE games SET game_score1 = '". json_encode($sc1) . "' WHERE
games_id = [1,2,3,4,5,6,7,8,9] ");
then when you get your array from the colomnu, it is still on json format, so you need to do this
$array = json_decode($result);
sometimes your array contains elemens with '' all you need to do is to use
addslashes(json_encode($sc1))
instead of using
json_encode($sc1);
It looks like you just need the syntax to insert a new row to mysql. If game_id increments automatically, you don't have to specify it in the statment:
INSERT games (round_id, team1, team2, score1, score2, result)
VALUES (3, 4, 6, 18, 1, 0, 1);
In advance, thank you guys for the possible solutions to my problem. I'm definately gonna look into them.
A friend of mine told me that I should use a 'key' so that it would be able to loop in the database. Kinda like when the data was retreived from the database. This seemed to work.
I used a $key++ so it would start at the first games_id.
foreach($sc1 as $key => $value) {
$key++;
mysql_query("UPDATE games SET game_score1 = '".$value[]"' WHERE games_id = '".$key."' ");
}
My next challenge is to retreive per round_id and also store per round_id.
And to make it more interesting, the current date is gonna point to the current round_id :D
I am trying to create a firearms site that will house about 1000 guns. This is not a lot of database entries, but I am trying to keep the database light as possible. I have created five tables, keeping normalization in mind, and I'm having problems putting data into all five tables in one query. My database is structured like so:
+-----------------+ +-----------------+ +-----------------+ +-----------------+
| make + | model | | image | | type |
+-----------------+ +-----------------+ +-----------------+ +-----------------+
| PK | make_id | | PK | model_id | | PK | model_id | | PK | type_id |
+-----------------+ +-----------------+ +-----------------+ +-----------------+
| | make_name | | | make_id | | | image_path | | | type_name |
+-----------------+ +-----------------+ +-----------------+ +-----------------+
| | type_id |
+-----------------+ +------------------+
| | caliber_id | | caliber |
+-----------------+ +------------------+
| | model_name | | PK | caliber_id |
+-----------------+ +------------------+
| | cost | | | caliber_name|
+-----------------+ +------------------+
| | description|
+-----------------+
This might be TOO normalized, but this is what I am working with ;)
Let me show the code:
form
<form action="post" method="addProduct.php" enctype="multipart/form-data">
make: <input type="text" name="make" />
model: <input type="text" name="model" />
type: <input type="text" name="type" />
caliber: <input type="text" name="caliber" />
cost: <input type="text" name="cost" />
desc.: <input type="text" name="description" />
Image: <input type="file" name="image" id="image" />
<input type="submit" name="submit" value="Add Item" />
</form>
addProduct.php
$make = $_POST['make'];
$model = $_POST['model'];
$type = $_POST['type'];
$caliber = $_POST['caliber'];
$cost = $_POST['cost'];
$description = $_POST['description'];
$image = basename($_FILES['image']['name']);
$uploadfile = 'pictures/temp/'.$image;
if(move_uploaded_file($_FILES['image']['tmp_name'],$uploadfile))
{
$makeSQL = "INSERT INTO make (make_id,make_name) VALUES ('',:make_name)";
$typeSQL = "INSERT INTO type (type_id,type_name) VALUES ('',:type_name)";
$modelSQL = "INSERT INTO model (model_id,make_id,type_id,caliber,model_name,cost,description,) VALUES ('',:make_id,:type_id,:caliber,:model_name,:cost,:description)";
$imageSQL = "INSERT INTO image (model_id,image_path) VALUES (:model_id,:image_path)";
try
{
/* db Connector */
$pdo = new PDO("mysql:host=localhost;dbname=gun",'root','');
/* insert make information */
$make = $pdo->prepare($makeSQL);
$make->bindParam(':make_name',$make);
$make->execute();
$make->closeCursor();
$makeLastId = $pdo->lastInsertId();
/* insert type information */
$type = $pdo->prepare($typeSQL);
$type->bindParam(':type_name',$type);
$type->execute();
$type->closeCursor();
$typeLastId = $pdo->lastInsertId();
/* insert model information */
$model = $pdo->prepare($modelSQL);
$model->bindParam(':make_id',$makeLastId);
$model->bindParam(':type_id',$typeLastId);
$model->bindParam(':caliber',$caliber);
$model->bindParam(':model_name',$model);
$model->bindParam(':cost',$cost);
$model->bindParam(':description',$description);
$model->execute();
$model->closeCursor();
$modelLastId = $pdo->lastInsertId();
/* insert image information */
$image = $pdo->prepare($imageSQL);
$image->bindParam(':model_id',$modelLastId);
$image->bindParam(':image_path',$image);
$image->execute();
$image->closeCursor();
print(ucwords($manu));
}
catch(PDOexception $e)
{
$error_message = $e->getMessage();
print("<p>Database Error: $error_message</p>");
exit();
}
}
else
{
print('Error : could not add item to database');
}
So when I add an item using the above code everything works fine, but when I add another item using the same manufacturer name it will duplicate it. I just want it to realize it already exists and not duplicate it.
I was thinking of putting some type of check to see if that data already exists and if it does then don't enter the data, but get the id and enter that in the other tables where required.
Another thing I thought of was to create a dropdown for the data that will most likely be duplicated and assign the value as the id. But, my simple mind can't figure out the best way to do it :( Hopefully all that makes sense, if not I will try to elaborate.
You need to work out what constitutes a unique (unduplicated) make, model, type, and caliber.
Then, you need to create unique indexes for those tables that enforce the uniqueness. See http://dev.mysql.com/doc/refman/5.0/en/create-index.html
For example you might use make_id, model_name, and caliber_id to uniquely identify a model. You'd need
CREATE UNIQUE INDEX UNIQUEMODEL ON MODEL(make_id, caliber_id, model_name)
to set up your unique index. Notice that a primary key index can be a unique index, but you can have other unique indexes as well.
You then can use INSERT ON DUPLICATE KEY UPDATE to populate your tables. See here: http://dev.mysql.com/doc/refman/5.5/en/insert-on-duplicate.html
For all this to work correctly, you'll have to make sure appropriate type, caliber, and make rows exist before you try to populate each model row: you need the ids from those first three tables to populate the fourth.
If you've got fields where there's only ever going to be a limit set of data (calibre is definitely one, and I suspect manfacturer will also work) you can pre-populate the tables in the database and turn it into a lookup field.
On your HTML form, instead of having a text input field, you can print out a select box instead. The value of the select is the ID in the lookup field - you don't need to worry about adding anything to the lookup fields in the database then.
When I've had to do this in the past, I've written a function to do the data insert; it checks to see if the value is in the table. If it is, it returns the index of the field; otherwise, it adds it as a new entry, and returns the ID for the new entry. It's not the most elegant solution, but it works well.