Pull information from an array - php

So basically I don't really know how to explain what i need or if the title is even correct but i need some help.I'll explain what I'm trying to do the best I can.
This is a picture of the page I'm working on and referring to
http://i40.tinypic.com/14m5euh.png .
SO basically what I need to do is,when the user uploads a file and clicks on upload it will upload to a database along with the job number,so far I've managed to get it to update into the database.
But I can't get the job number in the database because my information is all in an array from the database I tried to store the information into a session variable,its loops and gets replaced by the next one,so basically what ever the last job number is will be stored in the database which of cause is wrong?
The last problem I've is,how do I get the users answer from the drop down box into a variable ?
My code:
$con = mysqli_connect("localhost", "root", "","fixandrun") or die(mysqli_error());
$data = mysqli_query($con,"SELECT * FROM bookjob") or die(mysql_error());
Print "<table border cellpadding=10>";
Print "<table border='2'>";
echo "<table border='2' cellpadding=10>
<tr>
<th> Job number</th>
<th> Job details</th>
<th> Pc number </th>
<th> Job status</th>
<th> Upload report</th>
<th> Update Job</th>
</tr>";
while($row= mysqli_fetch_array($data))
{
echo "<tr>";
echo "<td>".$_SESSION['JOBNUM'] = $row['jobnumber'] . "</td>";
echo "<TD width=20% height=100>" . $row['jobdetails'] . "</td>";
echo "<td>" . $row['pcnumber'] . "</td>";
echo "<td> <select name='jobprogress'>
<option Value='pending'>pending</option>
<option value='Completed'>Completed</option>
<option value='In progress'>In progress</option>
<option value='Need more information'>Need more information</option>
</select> </td>";
echo "<td> <form action='reportupload.php' method='post' enctype='multipart/form-data' name='uploadform'>
<input type='hidden' name='MAX_FILE_SIZE' value='350000'>
<input name='report' type='file' id='reportupload' size='50'>
<input name='upload' type='submit' id='upload' value='Upload'>
</form>
</td>";
echo "<td> <a href='updateadmin.php'>Update information</a></td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";

There are many problems with your code. It would be extremely time consuming to go through them all, so I'll run down some things you need to look at to get this going.
FIrstly, you can make as many forms as you want, and with the help of PHP you can do this dynamically as youre doing. The problem is for every job, youre creating a new form with the same exact name. You need to find a way to make them the name of each form dynamic if youre going to do that.
Secondly, you probably shouldnt make a new form, so take all your opening and closing tags out of the loop, put the opening form tag before the while, and the ending form tag after the while loop.
I would suggest you go read about HTML forms a little more and PHP while loops and fetching information from a database with embedded forms before messing with this more. No offence, but I think you need a little more knowledge on this before fiddling with it.
I would suggest making a test database and table with maybe 3 fields, then making a small form with a couple field and seeing if you can fetch the data and update it with your form first.

i think i have done what you ment, is this right now?
$cnt = 0;
while($row= mysqli_fetch_array($data))
{
echo " <form action='test.php' method='post' enctype='multipart/form-data' name='uploadform' . $cnt>";
echo "<tr>";
echo "<td>".$_SESSION['JOBNUM'. $cnt] = $row['jobnumber'] . "</td>";
echo "<TD width=20% height=100>" . $row['jobdetails'] . "</td>";
echo "<td>" . $row['pcnumber'] . "</td>";
echo "<td> <select name='jobprogress'>
<option Value='pending'>pending</option>
<option value='Completed'>Completed</option>
<option value='In progress'>In progress</option>
<option value='Need more information'>Need more information</option>
</select> </td>";
echo "<td>
<input type='hidden' name='MAX_FILE_SIZE' value='350000'>
<input name='report' type='file' id='reportupload' size='50'>
</td>";
echo "<td><input name='upload' type='submit' id='upload' value='Upload'></td>";
echo "</tr>";
echo "</form>";
++$cnt;
}
echo "</table>";
echo "<br>";
?>

Related

How to multiply row from mysql by quantity from form?

I've got the following code which now I would like to make to multiply $row['Price'] by quantity from form once the 'Buy' link has been clicked. I am a beginner so please don't judge, any help appreciated :) thanks
<table style="width:50%" id="table1" align="center">
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Image</th>
<th>Quantity</th>
<th>Buy</th>
<?php
$query="SELECT * FROM products";
$result=mysqli_query($connection, $query);
while ($row=mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>";
echo $row['ProductName'];
echo "</td>";
echo "<td>";
echo "$".$row['Price'];
echo "</td>";
echo "<td>";
echo ' <img src="./images/'.$row['Image'].'" style="width:50px;height:50px"/><br />';
echo "</td>";
echo "<td>";
echo "<form method='get' action='buy.php'>";
echo "<fieldset>";
echo "<input type='number' name='quantity' style='width:30px'/>";
echo "</td>";
echo "<td>";
echo 'Buy';
echo "</td>";
echo "</tr>";
echo "</fieldset>";
echo "</form>";
}
Short version:
You will need to post the quantity and the productID. than in your buy.php get the product price for that particular productID from the database and multiply that by the quantity. DO NOT POST THE PRICE IN YOUR FORM!!! please for the sake of programming do not post the product price in your form
Long version:
What you need:
1: A form (you have that already)
1b: A hidden form field that holds your product ID:
<input type='hidden' name="ProductID" value="<? echo $row['ProductID'];?>"/>
2: a submit button for your form:
<input type='submit' />
3: a page to recieve the form (you have that already: buy.php)
Now in your buy.php you can get the FORM variables like this:
echo $_POST['quantity'] ;
echo $_POST['ProductID'] ;
Now you need to get that product form the database to get the price
$query="SELECT * FROM products where ProductID=$_POST['ProductID']";
than multiply the price with the quantity
Do NOT send the price in the form... because people can edit that value!
thats why we send only the product ID and get the price from the database!!!!!
Also note that in this brief explanation i did not take form validation in consideration. But obviously you need to make sure that data you get from the is valid and not dangerous (google SQL injection to learn more)
for example your form is something like that
<form method='post' action='thispage.php'>
<input type="text" name="quantity" vlaue="1"> <!--you can get this value from form input no problem -->
</form>
<!-- now on thispage.php write the script you have posted on the question and on line Buy link you have to do something like that-->
<?php
#$q = $_POST['quantity'];
$value= $q*$row['Price'];
?>
echo 'Buy';
now you can get this value on page buy.php something like that
$value = $_GET['price'];
echo $value;

php update through ajax

heloo i have an ajx call function which brings information from a dropdown populated into a table with text inputs by ajax.
i was wondering if there was anyway that i could update the record in the database by using these text fields and the UPDATE function i am relativity new and the internet didnt bring much to light.
i have a button appearing in this table from a drop down but as far as i am aware you cannot use forms within php and the page this would have been submitted from is already submitting a php function and 2 can not be submitted at once.
i was wondering if it was possible that when the data in the textboxes below is changed when the user clicks the button those details are updated in the database?
im new to ajax and php so help would be amazing.
ps. i know this isnt secure i want it to be functional first and before it goes live i will secure it.
here is the code:
<?php
$q = $_GET['q'];
$con = mysqli_connect('server','uid','pwd','dbname');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"account.php");
$sql="SELECT * FROM account WHERE name = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Your Name</th>
<th>Your Email</th>
<th>Your Password</th>
<th>Your User Level</th>
<th>Save Changes</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='text' name='txt_yourname' id='txt_yourname' value='" .$row['name']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_email' id='txt_email' value='" .$row['email']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_password' id='txt_password' value='" .$row['password']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_userLevel' id='txt_userLevel' value='" .$row['user_level']."' required='required' /> </td>";
echo "<td> <input type='button' name='btn_user' id='txt_user' type='submit' value='cheese'/> </td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
These statements execute user input, opening you up to a SQL Injection attack. You'll want to not do this.
$q = $_GET['q'];
$sql="SELECT * FROM account WHERE name = '".$q."'";
$result = mysqli_query($con,$sql);
To answer your question, in order to update a row like you want you'll need a way to identify in the database the row that has changed. One way to do it would be to include the row's primary key as a field/attribute in the HTML table row, but I'll leave it to someone more well versed in this area to say whether that's a good idea.
You're also going to want to escape and check the type of all of the fields the user can input when you go to do the update.

Dropdown menu inside a table - then update

I have a table populated from a mysql database. One of the fields is "status". I would like this cell to be a drop down box inside the table, so I can then update the particular field.
This code, correctly displays the table and currently it displays the "status" filed inside a text box that I can edit successfully. I would like this to be a drop down though.
<?php
require_once('db_connect.php');
$result = mysql_query("SELECT *
FROM queries
WHERE SR = '$_GET[SR]'
")
or die(mysql_error());
echo '<form name="Form" action="update.php" method="post">';
echo
"<table id='box-table-b'>
<thead>
<tr>
<th>SR</th>
<th>Product</th>
<th>Status</th>
</tr>
</thead>";
while($row = mysql_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['SR'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . '<input type="text" name="status" value="'.$row['status'].'" />' . "</td>";
echo "</tr>";
echo "</tbody>";
}
echo "</table>";
echo '<input type="submit" name="Save" value="Save" />';
echo '</form>';
?>
Can someone please show me how to do this ?
To answer the question, you should use the <select></select> tags. Example:
<select>
<option>Item 1</option>
<option>Item 2</option>
<option> ... </option>
<option selected="selected">Item N</option>
</select>
In this specific example, the dropdown would appear with "Item N" selected by default.
As a side note, isn't using mysql_* functions bad practice in general?
by drop down menu I guess you mean a select tag, anything more complicated that this requires a custom implementation.
This requires 2 steps: first you need to create the select tag and populate it with option tags, then you need to set the desired value as selected.
to create the select tag:
$myselect="<select id='status' name='status'>";
foreach($status_values as $e){
$myselect.="<option value='$e'>$e</option>";
}
$myselect.="</select>";
$status_value is a array, you can have in your code or get it from a query.
To select the correct one you can add the following if to the code above:
$myselect="<select id='status' name='status'>";
foreach($status_values as $e){
if($e == $row['status']){
$myselect.="<option value='$e' SELECTED>$e</option>";
}else
$myselect.="<option value='$e'>$e</option>";
}
}
$myselect.="</select>";

Using $_GET with a submit button

I have a table with 3 columns like so in index.php:
ID | StName | Inspect
_________________________________
1 Wisconsin INSPECT
2 Alabama INSPECT
3 Nebraska INSPECT
The right most column is a submit button which takes the user to a page called inspect.php. I want to send the corresponding ID column value to inspect.php when a user clicks the INSPECT button so that I can use that value inside inspect.php, how can I do this?
Currently I have:
//index.php
echo "<table border = 1>
<tr>
<td>ID</td>
<td>StName</td>
</tr>";
// go into a loop to create more rows -- omitted
echo "<tr>";
echo "<td>" . $resultArr[0] . "</td>";
echo "<td>" . $resultArr[1] . "</td>";
echo "<td> <form action='inspect.php' method='get'>
<input type='Submit' value='Inspect'>
</form>
</td>";
echo "</tr>";
echo "</table>";
$resultArr[0] contains the ID value I want to send to inspect.php
So add an input field inside the form with a name attribute which will be an index key name on inspect.php page
<form action='inspect.php' method='get'>
<input type='hidden' name="id" value='<?php echo $resultArr[0]; ?>'>
<input type='submit' value='Inspect'>
</form>
A better and simple way to go for this is without a submit, just use a hyperlink like
Inspect
And later process this id on inspect.php page
I think I got what you wanted to work. I am using different data in the array, but you should be able to change to your needs anyway.
On index.php:
$resultArr = array("dave","fff","erere");
echo "<table border = 1>
<tr>
<td>ID</td>
<td>StName</td>
</tr>";
// go into a loop to create more rows -- omitted
echo "<tr>";
echo "<td>" . $resultArr[0] . "</td>";
echo "<td>" . $resultArr[1] . "</td>";
echo "<td> <form action='inspect.php' method='get'>
<input type='hidden' name='id' value=' $resultArr[0] '>
<input type='submit' name='inspect' value='Inspect'> </td>";
echo "</tr>";
echo "</table>";
And on inspect.php:
if( $_GET['inspect']){
$name = $_GET['id'];
echo $name;
}
This outputs the value of $resultArr[0], which I think you wanted.

How to create submit form in a row and relate input to that row?

Hey Guys, I have a question for you.
Imagine that I wanted to be able to keep track of how many miles I've ran every week, so that I could
compare it to the goals I've set for each week. So i've created this table by the use of mysql_fetch_row.
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><form><input method='post' type='number'></form></td>";
echo "</tr>";
}
echo "</table>";
This piece of code resultet in a table with 10 weeks with 10 goals - and a column for the actual number of miles. This column should include 10 input forms where the actual number of miles can be submitted. But how do I relate the input from the submit form to the row in which the submit form is positioned?
The primary key is the week - so this would be the one to relate to.
Hope you understand what my problem is:)
To do this you would use a hidden input field.
When you echo each row, and the form in that row, you would simply add an extra line:
`<input type="hidden" name="row_id" value="' . $row['id_column'] . '" />';
In full, your code would be:
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>
<form>
<input method='post' type='number'>
<input type='hidden' name='row_id' value='" . $row['id_column'] . "' />
</form>
</td>";
echo "</tr>";
}
echo "</table>";
I think there should be some modifications that has to be done in loop.
echo "<td><form method='post'><input type='number' value='".$rows['col_name']."'><input type='submit' ></form></td>";
This code adds a submit button to each row. But, this shouldn't be what I think.
It should be rather this way,
echo "<form method='post'> ";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><input type='number' value='".$rows['col_name']."'></td>";
echo "</tr>";
}
echo "<input type='submit' ></form>";
Or make the input field look like this.
'<input type="text" name="row['.$row['id_column'].'][miles]" />';
It will return you an array when you post it.
foreach($_POST['row'] as $key => $value){
// $key is your primary key
// $value['miles'] is your input value
}

Categories