Show cell value where row and column intersects - php

I am trying to get cell value which is at the intersection of row and column number entered by user, as like in matrices, like if user enters row 5 and column 4 then my result should show the value present in the cell [5.4].
I am getting row and column number as:
<td>Please enter the temperature:</td>
<td><input type="text" name="temp" /></td>
<td>Please enter the column:</td>
<td><input type="text" name="col" /></td>
<td><input type="submit" name="go" /></td>
i am redirecting these values to find.php page, where i am doing this:
$entry1 = $_POST['temp'];
$entry2 = $_POST['col'];
$result = mysql_query("SELECT * FROM tables WHERE Temperature LIKE '".$entry1."'
AND COLUMN_NAME = '".$entry2."' ");
and showing my result as
<td ><?php echo (don't know what to write here) ; ?></td>
i know i am making lot of mistakes but do not know how to fix them. Please show me way.

If you want to get values at [Temperature ,COLUMN_NAME] then change query to
$result = mysql_query("SELECT * FROM tables WHERE Temperature = '".$entry1."'
AND COLUMN_NAME = '".$entry2."' ");
Because LIKE will look all string containing $entry1.
For example if $entry = 10 the it will select column with values like 10, 110, 100,...
after executing the query fetch data like this
if the no of rows returned is more than one the loop it with while like this
while ($row = mysql_fetch_array($result))
{
echo $row[$entry2];
}
or if it return only one value or you need only first value when it returns more than one value try this
$row = mysql_fetch_array($result);
echo $row[$entry2];
Also try to avoid using mysql_* functions and start using mysqli_* function or PDO

Related

How to display the value within the column only and display it (PHP & MySQL)

I want to get the value of that row within the column and display it. I don't want to display the whole row, just the value inside the column. Also I don't want to display all the array value at the same time. I just want to get the specific value within row of the column at a time using loop. Here is the value that I want to display
Here's the code too.
<?php
include('../../private/includes/dbh.php');
$mod_result = mysqli_query($conn, "SELECT id from moderator");
while($row = mysqli_fetch_row($mod_result)){
?>
<input type="text" name="moderator_id" value="<?php echo $row['id']; ?>">
<?php
}
?>
UPDATE: Here is the output of changing the mysqli_fetch_row into mysqli_fetch_assoc and mysqli_fetch_array. Here is the output
If you change your loop to use mysqli_fetch_assoc instead of mysqli_fetch_row, then you should be able to reference $row['id'].
From the PHP documentation:
mysqli_fetch_row($mod_result) - "Get a result row as an enumerated array". $row[0] is how you'd reference the first column.
mysqli_fetch_assoc($mod_result); - "Fetch a result row as an associative array". $row['id'] will return the value of the id column in this row.
<?php
include('../../private/includes/dbh.php');
$mod_result = mysqli_query($conn, "SELECT id from moderator");
while($row = mysqli_fetch_assoc($mod_result)){
?>
<input type="text" name="moderator_id" value="<?php echo $row['id']; ?>">
<?php
}
?>
So since you already have the moderator logged in in your webpage, you already have an identifier for this moderator, so seen your table if you have the user name you can just populate the variable with that value and make the query fetch the correspondent value
$moderator = 'June';
So the query will be:
SELECT id FROM moderator WHERE mod_username='$moderator'
You just need to determine what value you can use it from the logged user, maybe even if you have access to the id, you dont need to make another database call.
<?php
include('../../private/includes/dbh.php');
$mod_result = mysqli_query($conn, "SELECT id FROM moderator WHERE mod_username='$moderator'");
while($row = mysqli_fetch_assoc($mod_result)){
?>
<input type="text" name="moderator_id" value="<?php echo $row['id']; ?>">
<?php
}
?>
I hope this resolves it.

Retrieving multiple data and inserting multiple data with array and foreach [duplicate]

This question already has an answer here:
Insert multiple rows into a MySQL database from a table
(1 answer)
Closed 5 years ago.
I am listing product list from products table which i select product_group on previous page. In this list, i show product names and empty input in every row of table. Into the empty inputs, manually i write number of products which i count in my shop. After filling all the products numbers (rows in this html table) i want to insert them into another mysql table row by row for each product.
Here is my html table codes:
<table class="table table-responsive" width="100%" border="1">
<tbody>
<tr style="background-color:#C2E17C">
<td align="center" width="80%"> Stok İsmi</td>
<td align="center" width="20%"> Sayım Miktarı</td>
</tr>
<?php
$personelcek = mysql_query("SELECT * FROM stoklar WHERE sto_durum='acik' AND sto_altgrup_kod = '".$_REQUEST['sta_RECno']."' ORDER BY sto_isim ASC");
while ($a= mysql_fetch_array($personelcek)){
$sto_RECno = $a['sto_RECno'];
$sto_isim = $a['sto_isim'];
echo'
<tr>
<td align="left" width="80%"><input class="input1" type="hidden" name="stok_kodu[]" value="'.$sto_RECno.'" readonly>'.$sto_isim.'</td>
<td width="20%"><input style="text-align:right" class="input1" type="text" name="sayim_miktari[]" value=""></td>
</tr>';}?>
</table>
And also there is one more input outside of the table which i retrieve the product_group_id as this:
<input type="hidden" name="alt_grup_kod[]" value="<?php echo $_REQUEST['sta_RECno']; ?>">
And the last thing, i want to retrieve existing count numbers from mysql products table for each product.
As a summary, i want to insert every product one by one into another mysql table including product_group_id, product_id, existing_count_numbers and manually written numbers.
I tried like this but everytime i get errors:
$stok_kodu = $_POST['stok_kodu'];
$stoklar = array($stok_kodu);
$sayim_miktari = $_POST['sayim_miktari'];
$sayim_miktarlari = array($sayim_miktari);
$alt_grup_kod = $_POST['alt_grup_kod'];
$alt_gruplar = array($alt_grup_kod);
$stok_miktarlari = array();
$miktar_cek = mysql_query("SELECT * FROM stoklar WHERE sto_RECno = '".$stok_kodu."' ORDER BY sto_RECno");
while ($miktar_al = mysql_fetch_assoc($miktar_cek)){
$stok_miktari[] = $miktar_al['sto_miktar'];
foreach ($stok_kodu as $stoklar){
foreach ($sayim_miktari as $sayim_miktarlari){
foreach ($alt_grup_kod as $alt_gruplar){
foreach ($stok_miktari as $stok_miktarlari){
$sayim_kaydet = mysql_query("INSERT INTO sayim_sonucu (alt_grup_kod, stok_kodu, sayim_miktari, stok_miktari) VALUES ('$alt_gruplar', '$stoklar', '$sayim_miktarlari', '$stok_miktarlari')");
}}}}}
Any idea how i can tidy all these codes and insert every product into another table of mysql database?
Your foreach makes the trouble.Change your foreach like this. hope this will help.And also an added note 'you definitely needed to write a function to escape mysql injection for every post variable.
$count=0;
foreach ($stok_kodu as $stoklar){
$sayim_kaydet = mysql_query("INSERT INTO sayim_sonucu
(alt_grup_kod, stok_kodu, sayim_miktari, stok_miktari) VALUES
('$alt_grup_kod[$count]', '$stoklar',
'$sayim_miktari[$count]', '$stok_miktari[$count]')");
$count++;
}

Update mysql database fields with array php

I'm trying to achieve a multiple update in one submit. I have a table with a number of rows and want to be able to update one field in each row just by tabbing to the next insert box.
My code is thus:-
//start a table
echo '
';
//start header of table
echo '<tr>
<td width="60" align="center"><strong>Lab Item ID</strong></td>
<td width="60" align="center"><strong>Test Suite Name</strong></td>
<td width="60" align="center"><strong>Test Name</strong></td>
<td width="50" align="center"><strong>Result</strong></td>
</tr>';
//loop through all results
while ($row=mysql_fetch_object($sql)) {
//print out table contents and add id into an array and email into an array
echo '<tr>
<td align="center"><input name="id[]" value='.$row->lab_item_id.' readonly> </td>
<td align="center">'.$row->test_suite_name.'</td>
<td align="center">'.$row->test_name.'</td>
<td><input name="test_result[]" type="text" value="'.$row->test_result.'"></td>
</tr>';
}
//submit the form
echo'<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>';
//if form has been pressed proccess it
if($_POST["Submit"])
{
//get data from form
//$name = $_POST['name'];
//$_POST['check_number'] and $_POST['check_date'] are parallel arrays
foreach( $_POST['id'] as $id ) {
$tresult = trim($_POST['test_result']);
$query = "UPDATE tbl_lab_item SET test_result='$tresult' WHERE lab_item_id = '$id'";
//execute query
}
print_r($_POST);
var_dump($tresult);
//redirect user
$_SESSION['success'] = 'Updated';
//header("location:index.php");
}
?>
When I print the $_POST arrays, everything is populating fine, however the variable is Null. I know I can't do a foreach on multiple arrays (at least I don't think I can) so is there some other trick I'm missing please? I can't be far away as the $_Post print has the right data in it.
Incidentally, the whole thing is generated by a query, so I never know how many records I'll have to update.
I've been looking at this (and other) forums, but can't seem to get a solution. I thought I understood arrays, but now I'm beginning to wonder!
edit - it's the $tresult variable that isn't working.
Many thanks,
Jason
Edit Thursday 21st Feb (05:41 UK time)
Thanks for your input everybody. I've solved this one now, and your collective advice helped. The code that finally cracked it is:-
//get data from form
$id1 = $_POST['id'];
$test_result1 = $_POST['test_result'];
foreach ($id1 as $key => $value){
$query = "UPDATE tbl_lab_item SET test_result='$test_result1[$key]' WHERE lab_item_id=$value ";
//execute query
Working through which variables etc were populated and what they were populated with was the key. Back to first principles, isn't it?
Cheers all.
J
Actually, you might get it done by doing a simpler (basic) form of for loop:
//get data from form
//$name = $_POST['name'];
//$_POST['check_number'] and $_POST['check_date'] are parallel arrays
$numberOfData = count($_POST['id']);
for($index = 0; $index < $numberOfData; $index++)
{
$id = $_POST['id'][$index];
$tresult = trim($_POST['test_result'][$index]);
$query = "UPDATE tbl_lab_item SET test_result='$tresult' WHERE lab_item_id = '$id'";
//execute query
}
print_r($_POST);
I hope this helps.
Cheers
change the query like this :
$query = "UPDATE tbl_lab_item SET test_result=$tresult WHERE lab_item_id = $id";
By adding single quotes ' ' you tell it to read is as a String and not to take the value of the var.
Edit
Replace your foreach loop with the following and let me know :
$id1 = $_POST['id'];
$test_result1 = $_POST['test_result'];
foreach( $id1 as $key => $value ) {
$query = "UPDATE tbl_lab_item SET test_result='$test_result1[$key]' WHERE lab_item_id = '$key' ";
}
Problem is that you're telling PHP to build your input fields as arrays, but then treat it as a string later:
<td><input name="test_result[]" type="text" value="'.$row->test_result.'"></td>
^^--- array
$tresult = trim($_POST['test_result']);
^^^^^^^^^^^^^^^^^^^^^--- no array key, so you're assigning the entire array
$query = "UPDATE tbl_lab_item SET test_result='$tresult'
^^^^^^^^^^--- array in string context
trim() expects a string, but you pass in an array, so you get back a PHP NULL and a warning. That null then gets stuffed into your SQL statement, and there's your problem.

how do you store multiple rows and column array in mysql

I have an array of checkboxes.
<input type="checkbox" name="selection[]" value="move" />
<input type="checkbox" name="selection[]" value="move2" />
<input type="checkbox" name="selection[]" value="move3" />
<input type="checkbox" name="selection[]" value="move4" />
Depending on the number of checkboxes selected, a table with corresponding number of rows is generated.
for($x=0; $x<$N; $x++)
{
echo nl2br("<td><textarea name=art[] rows=10 cols=30></textarea> </td><td><textarea name=science[] rows=10 cols=30></textarea></td></textarea></td><td><textarea name=method[] rows=10 cols=30></textarea></td><td><textarea name=criteria[] rows=10 cols=30></textarea></td></tr>");
}
I cannot tell how many table rows with corresponding columns will be generated each time. So how to write the code to insert each set of row array is a problem. I have tried the
$optionsVal = implode(",", $data);
but that only works to store the selected options and not for the generated table rows and columns.Please can anyone help with this. Thanks in advance
Okay so I think I understand a little better, but perhaps you should relay your question in other terms.
Basically my understanding is that you are accepting an uncertain (within the boundaries of the number of checkboxes you have) number of checkboxes, which there in turn generate a row for each selected check box.
If you want to store these generated rows in mySQL you need to post the data back to the database
$result = mysqli_query($query, $conn);
$row = mysqli_fetch_array($result);
You need to set a $result similar to this, and store your check box values in it
In this example if the end-user hits the save button it inserts the values from the check box into a variable
if(isset($_POST["savebtn"]))
{
//inserting the new information
$id = $_POST[""];
$name = $_POST[""];
//iterate through each checkbox selected
foreach($_POST["checkbox"] as $loc_id)
{
$query = "INSERT INTO table(ID, Loc_Code) VALUES('$id', '$loc_id')";
$result = mysqli_query($query, $conn);
}
?>
This was just kinda taken from another example, but you are way off with the implode, you need to save the results of the php selection to variables first, and then assign them rows in mySQL by looping through the selection
UPDATE:
Okay, so you got them in an array, seelction[] - this is good now you would want to check to see if a certain value is selected...
if (in_array("move2", $_POST['selection'])) { /* move2 was selected */}
then you want to put that into a single string - you were right with the implode method
echo implode("\n", $_POST['selection']);
then echo it out with a foreach loop
foreach ($_POST['selection'] as $selection) {
echo "You selected: $selection <br>";
}

Parse CSV From HTML form and Insert into MYSQL Database

I'm trying to take an array and insert it into seperate rows in a MYSQL database.
Basically, there's an HTML form for a top 5 list that I want to input in one field, but with each value seperated by commas. So, for example (Artist 1, Artist 2, Artist 3, etc.)
These 5 values then have to be seperated into 5 values that are then inserted into 5 rows in a MYSQL database.
So the HTML form looks like this:
<tr>
<td>Loud Rock</td>
<td><label for="lr_topfive"><input type="text" placeholder="" name="lr_topfive" size="75"
maxlength="100" autofucus required /></label></td>
</tr>
and the form value is sent to another php file with this code in it:
$val = $_POST['lr_topfive'];
$data = str_getcsv($val);
??????
$lr_one = mysql_prep($_POST['lr_one']);
$lr_two = mysql_prep($_POST['lr_two']);
$lr_three = mysql_prep($_POST['lr_three']);
$lr_four = mysql_prep($_POST['lr_four']);
$lr_five = mysql_prep($_POST['lr_five']);
$query = "INSERT INTO xyz_wb (lr_one, lr_two, lr_three, lr_four, lr_five) VALUES ('{$lr_one}','{$lr_two}', '{$lr_three}', '{$lr_four}', '{$lr_five}')";
$result = mysql_query($query, $connection);
if ($result) {
redirect_to("email-ready.php");
} else {
//display error message
echo "<p>Yikes!</p>";
echo "<p>" . mysql_error() . "</p>";
}
I get that the 'lr_topfive' value is parsed and seperated into distinct values by the first two lines, but I dont know what to do before inserting these values into the MYSQL DB.
I think you are just looking for:
$top_five_array = explode(',', $_POST['lr_topfive']);
and then:
$lr_one = mysql_prep($top_five_array[0]);
$lr_two = mysql_prep($top_five_array[1]);
....
However, this will break if there are not exactly 4 comma's in your input field.
Apart from that you need to switch to prepared statements using PDO or mysqli, see the comments below your question.

Categories