Im trying to fetch records from database, then i have to upload a image which should be stored in folder and also link to be updated in table.. Everything working fine only with last row.. For first row its not updating.. Please help me where im goin on.. Below is my code
<?php
$email1 = $_SESSION['email'];
$Vendor_id = "SELECT Vendor_id FROM vendors where email = '$email1' ";
$result = mysqli_query($conn, $Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$cpid = $row['Vendor_wallet_id'];
$tid = $row['request'];
$type = $row['amount'];
$pays = $row['status'];
$amt = $row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td><input type="file" value="<?php echo $amt; ?>" name="fileToUpload" id="fileToUpload" ></td>
<td><input type="submit" value="Submit" name="submit" >
</button>
</td> </tr>
<?php } ?>
</tbody>
</table>
</form>
upload.php is taken from https://www.w3schools.com/php/php_file_upload.asp
on your form
<?php
$email1 = $_SESSION['email'];
$Vendor_id = "SELECT Vendor_id FROM vendors where email = '$email1' ";
$result = mysqli_query($conn, $Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$cpid = $row['Vendor_wallet_id'];
$tid = $row['request'];
$type = $row['amount'];
$pays = $row['status'];
$amt = $row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td><input type="file" name="fileToUpload []" class="fileToUpload" ></td>
<input type="hidden" value="<?php echo $amt; ?>" name="fileToUploadLink []" class="fileToUploadLink" >
<td><input type="submit" value="Submit" name="submit" >
</button>
</td> </tr>
<?php } ?>
</tbody>
</table>
</form>
on upload.php
<?php
if(isset($_FILES['fileToUpload']['tmp_name'])&&isset($_POST['fileToUploadLink'])){
for($i=0;$i<=(count($_FILES['fileToUpload']['tmp_name'])-1);$i++){
move_uploaded_file($_FILES['fileToUpload']['tmp_name'][$i],$_POST['fileToUploadLink'][$i]);
}
}
?>
I have a few suggestions:
In your "php.ini" file, search for the file_uploads directive, and set it to On: file_uploads = On
...</button><!-- <<-- How are you using this? -->
Could try to echo $amt; at the bottom of the loop to see if the path is correct.
May want to check if the $amt with IF Empty condition/statement and maybe check the condition of your other variables.
$email1 = $_SESSION['email'];
$Vendor_id="SELECT Vendor_id FROM vendors where email = '$email1' ";
$result=mysqli_query($conn,$Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$cpid=$row['Vendor_wallet_id'];
$tid=$row['request'];
$type=$row['amount'];
$pays=$row['status'];
$amt=$row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid;?></td>
<td><?php echo $tid;?></td>
<td><?php echo $type;?></td>
<td><?php echo $pays;?></td>
<td><input type="file" value="<?php echo $amt;?>" name="fileToUpload" id="fileToUpload" ></td>
<td><input type="submit" value="Submit" name="submit" >
</button> <!-- WHY IS THIS HERE? -->
</td></tr>
<?php
echo $amt; //See if it is correct path
}//END WHILE ?>
</tbody>
</table>
</form>
There are a few ways in which you could do the file upload, one of which would be to make the form a child of a single table cell and have the file field as a direct child of that form. The submit button, to keep the layout, would not be a child of the form and would need to be changed to a simple button input type then use javascript to submit the form
<?php
$email1 = $_SESSION['email'];
$Vendor_id = "SELECT Vendor_id FROM vendors where email = '$email1' ";
$result = mysqli_query($conn, $Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysqli_fetch_array( $query ) ) {
$cpid = $row['Vendor_wallet_id'];
$tid = $row['request'];
$type = $row['amount'];
$pays = $row['status'];
$amt = $row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" value="<?php echo $amt; ?>" name="fileToUpload" />
</form>
</td>
<td><input type="button" value="Submit" name="submit" /></td>
</tr>
<?php
}
?>
</tbody>
</table>
<script>
var bttns=Array.prototype.slice.call(document.querySelectorAll('input[type="button"][name="submit"]'));
bttns.forEach(function(bttn){
bttn.addEventListener('click',function(evt){
this.parentNode.parentNode.querySelector('form').submit();
}.bind(bttn),false );
});
</script>
Seems like You want to make a form per row.
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td colspan="2">
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="Vendor_wallet_id" value="<?php echo $cpid;?>" />
<input type="file" value="<?php echo $amt; ?>" name="fileToUpload" />
<button>Submit</button>
</form>
</td>
</tr>
</form>
and remove <form> tag that wraps table
Related
*How to solve this..??
My while loop is finding the last iteration only, If I select last attendance A then all the student's values will be 0, if I select P then all the values will be 1. So, how can I solve this?*
Index.php file
<?php
include'connection.php';
$sql = "SELECT * FROM `student_info`";
$result = mysqli_query($conn,$sql);
?>
<table border="1">
<tr>
<th>Student ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Attendance</th>
</tr>
<?php
while($row=mysqli_fetch_assoc($result))
{
$id = $row['stu_id'];
?>
<tr>
<td><?php echo $row['stu_id'];?></td>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td>
<form action="attendance.php" method="post">
<select name="atten" id="">
<option value="1">P</option>
<option value="0">A</option>
</select>
</td>
</tr>
<?php
}
?>
</table>
<br>
<button type="submit">Submit</button>
</form>
attendance.php
<?php
include'connection.php';
$sql = "SELECT * FROM `student_info`";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
if(isset($_POST['atten'])){
$atten = $_POST['atten'];
echo $atten.'<br>';
}
}
Here is output
Image for Index.php file
Image for attendance.php file
You're not submitting the student ID in the form. You can use a hidden input for this.
<?php
while($row=mysqli_fetch_assoc($result))
{
$id = $row['stu_id'];
?>
<tr>
<td><?php echo $row['stu_id'];?></td>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td>
<form action="attendance.php" method="post">
<input name="stu_id" type="hidden" value="<?php echo $row['stu_id'];?>">
<select name="atten" id="">
<option value="1">P</option>
<option value="0">A</option>
</select>
</form>
</td>
</tr>
<?php
}
?>
Then in attendance.php you can update that student.
<?php
include'connection.php';
$stmt = $conn->prepare("UPDATE `student_info` set atten = ? WHERE stu_id = ?");
$stmt->bind_param("ii", $_POST['atten'], $_POST['stu_id']);
$stmt->execute();
echo "Attendance for student {$_POST['stu_id']} updated";
Im actually trying to add only a specific row from the table product to the table product_add but the last row is being inserted in the table product_add. What i want is that when I click on the button ADD that specific row is being inserted in the table product_add. I think that the code is not considering the if (isset($_REQUEST['submit'])) part.
<?php
include'connect.php';
$image = isset($_GET['image']) ? $_GET['image'] : "";
$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$price= isset($_GET['price']) ? $_GET['price'] : "";
$sql="SELECT * FROM product";
$result = mysql_query($sql);
if($result>0){
?>
<form method="POST" id="form" name="form">
<table border='1'>
<tr>
<th>Id</th>
<th>Image</th>
<th>Name</th>
<th>Price MUR</th>
</tr>
<?php
while ($row = mysql_fetch_array($result)){
extract($row);
?>
<tr>
<td><?php echo ($row['id']); ?></td>
<td><img src=<?php echo $row['image'] ?> width='120' height='100'/></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
<td>
<input id="submit" type="submit" name="submit" value='ADD'/>
</td>
</tr>
<?php
}
?>
</table>
</form>
<?php
}
if (isset($_REQUEST['submit']))
{
$insert = "INSERT INTO product_add(id, name, price) VALUES ('$id', '$name','$price')";
$insertQuery=mysql_query($insert);
}
?>
basically it a general coding issue
type submit and also name submit is conflicting
first add an attribute in form tag enctype="multipart/form-data" for image or video upload
then
creat a hidden field named 'something'
like
<input type="hidden" name="something" />
and after that use this
isset($_REQUEST['something']
add from in each while
like this updated code
<?php
include'connect.php';
$image = isset($_REQUEST['image']) ? $_REQUEST['image'] : "";
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : "";
$name = isset($_REQUEST['name']) ? $_REQUEST['name'] : "";
$price= isset($_REQUEST['price']) ? $_REQUEST['price'] : "";
$sql="SELECT * FROM product";
$result = mysql_query($sql);
if($result>0){
?>
<table border='1'>
<tr>
<th>Id</th>
<th>Image</th>
<th>Name</th>
<th>Price MUR</th>
</tr>
<?php
while ($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo ($row['id']); ?></td>
<td><img src=<?php echo $row['image'] ?> width='120' height='100'/></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
<td>
<form method="POST" action="" >
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="name" value="<?php echo $row['name']; ?>" />
<input type="hidden" name="image" value="<?php echo $row['image']; ?>" />
<input type="hidden" name="price" value="<?php echo $row['price']; ?>" />
<input id="submit" type="submit" name="submit" value='ADD'/>
</form>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
if (isset($_REQUEST['submit']))
{
$insert = "INSERT INTO product_add(id, name, price) VALUES ('$id', '$name','$price')";
$insertQuery=mysql_query($insert);
}
?>
NOTE: mysql_* is deprecated use mysqli_* or PDO
Good day. I want to delete selected item by the user. But it turns out what ever button clicked by user, the first data row is deleting. Im stuck here. Please help.
This is my code:
HTML
<form method="POST" action="<?php $_PHP_SELF ?>">
<?php
$query = "select * from tblsr";
$request = mysql_query($query)or die(mysql_error());?>
<table class="table table-hover">
<tr>
<th>Softwares</th>
<th>Difficulty</th>
<th></th>
<th></th>
</tr>
<?php while($row = mysql_fetch_array($request)){
?>
<tbody>
<tr class="success">
<td><?php echo $row['Software']; ?></td>
<td><?php echo $row['Difficulty']; ?></td>
<td><input type="submit" class="btn btn-sm btn-success" name="change" value="Change Difficulty"/></td>
<td><input type="submit" class="btn btn-sm btn-danger" name="remove" value="Delete"/></td>
<input type="hidden" name="id[]" value="<?php echo $row['id']; ?>"/>
</tr>
</tbody>
<?php
}
?> </form>
DELETE Query
if(isset($_POST['remove'])){
$count = count($_POST['id']); //get total number of array element
for ($i = 0; $i < $count; $i++) {
$id = $_POST['id'][$i];
$sql = "Delete FROM tblsr
Where id = '$id'";
$success = mysql_query($sql) or die (mysql_error());
mysql_close();
if ($success == TRUE){
?>
<script>
alert('Deleted.');
window.location.href='manageRequest.php';
</script>
<?php
}
}
}
Your code's problem is that you never increase $i ( $i++ ) , and always your delete query affect on the first row ( id ) . you can put $i++ in your while loop to fix it, but I recommend that you simply do like below and don't bother yourself with a loop.
Delete query :
$rm = $_POST['id'];
if (isset($_POST['remove'])) {
foreach ($rm as $key => $value) {
$rm[$key] = "'".$value."'";
}
$sql = "Delete FROM tblsr
Where id IN(" . implode(",", $rm) .")";
die($sql);
$success = mysql_query($sql) or die(mysql_error());
if ($success) {
?>
<script>
alert('Deleted.');
window.location.href = 'manageRequest.php';
</script>
<?php
}
}
your form example for delete users:
<form method="POST" action="<?php $_PHP_SELF ?>">
<?php
$query = "select * from tblsr";
$request = mysql_query($query) or die(mysql_error());
?>
<table class="table table-hover">
<tr>
<th>Softwares</th>
<th>Difficulty</th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($request)) {
?>
<tbody>
<tr class="success">
<td><?php echo $row['Software']; ?></td>
<td><?php echo $row['Difficulty']; ?></td>
<td>
<input type="checkbox" name="id[]" value="<?php echo $row['id']; ?>"/>
delete
</td>
</tr>
</tbody>
<?php
}
?>
</table>
<button type="submit" name="remove" class="btn btn-success">
delete checked user
</button>
</form>
I dont get what im doing wrong here..
i have about 125 product in the table but i get only the last product from the table so it shows only one item... this is a simple calculator to provide the sales person and the customer how much box they would need and how much it would cost a quick estimate.
Thank you for the help in advance..
<?php
include('admincik/config.php');
include ('birlikte/ac.thumbs.php');
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM Calculator ORDER BY isim") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
$resim = $info['resim'];
$isim = $info[isim];
$boyut = $info[boyut];
$pcs = $info[adet];
$sqft = $info[sqft];
$price = $info[price];
}
/////////Formdan gelen yada Giden//////////////
$length =htmlspecialchars(stripslashes($_POST['Length']));
$width =htmlspecialchars(stripslashes($_POST['Width']));
$TileNameList = "<option value=\"$sqft\">$isim $boyut</option>";
/////Matematiksel islemler/////////
$equals = $length * $width;
$box = round($equals / $sqft);
$sqftbox = $box * $sqft;
$TotalPrice = $sqftbox * $price
?>
<div class="ana">
<table width="900" height="199" border="1">
<tr>
<td width="150">Name</td>
<td width="150">Length</td>
<td width="150">Width</td>
<td width="150">Total Sqft Area</td>
<td width="200">Box Needed /Total Sqft</td>
<td width="100">Price</td>
</tr>
<tr>
<td><form id="form5" name="form5" method="post" action="">
<select name="TileName" id="TileName">
<?php echo ($TileNameList); ?>
</select>
</td>
<td><input name="Length" type="text" id="Length"/></td>
<td><input type="text" name="Width" id="Width"/></td>
<td><input type="text" name="Sqft" id="Sqft" value="<?php echo ($equals); ?>"/></td>
<td><?php echo "You will need <span style=\"color:red\">$box</span> Boxes<br> Which is <span style=\"color:red\">$sqftbox</span> "; ?></td>
<td><?php echo "$$TotalPrice"; ?></td>
</tr>
<tr >
<td colspan="6" align="center">
<input type="submit" name="Submit" id="Submit" value="Submit" />
</form></td>
</tr>
</table>
</div>
Your loop doesn't append to $TileNameList because it exists outside of it. It actually replaces it's value. Try:
<?php
include('admincik/config.php');
include ('birlikte/ac.thumbs.php');
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM Calculator ORDER BY isim") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
$resim = $info['resim'];
$isim = $info[isim];
$boyut = $info[boyut];
$pcs = $info[adet];
$sqft = $info[sqft];
$price = $info[price];
$TileNameList .= "<option value=\"$sqft\">$isim $boyut</option>"; // NOTE THE .=
}
/////////Formdan gelen yada Giden//////////////
$length =htmlspecialchars(stripslashes($_POST['Length']));
$width =htmlspecialchars(stripslashes($_POST['Width']));
/////Matematiksel islemler/////////
$equals = $length * $width;
$box = round($equals / $sqft);
$sqftbox = $box * $sqft;
$TotalPrice = $sqftbox * $price
?>
<div class="ana">
<table width="900" height="199" border="1">
<tr>
<td width="150">Name</td>
<td width="150">Length</td>
<td width="150">Width</td>
<td width="150">Total Sqft Area</td>
<td width="200">Box Needed /Total Sqft</td>
<td width="100">Price</td>
</tr>
<tr>
<td><form id="form5" name="form5" method="post" action="">
<select name="TileName" id="TileName">
<?php echo ($TileNameList); ?>
</select>
</td>
<td><input name="Length" type="text" id="Length"/></td>
<td><input type="text" name="Width" id="Width"/></td>
<td><input type="text" name="Sqft" id="Sqft" value="<?php echo ($equals); ?>"/></td>
<td><?php echo "You will need <span style=\"color:red\">$box</span> Boxes<br> Which is <span style=\"color:red\">$sqftbox</span> "; ?></td>
<td><?php echo "$$TotalPrice"; ?></td>
</tr>
<tr >
<td colspan="6" align="center">
<input type="submit" name="Submit" id="Submit" value="Submit" />
</form></td>
</tr>
</table>
</div>
I have created a form that update the data in a sql column, so far all good.
The problem I have is that I would like to choose what columns are going to be updated, with checkboxes.
So I want to categorize two files as movies, I just check the checkbox and write movies in the form.
But its not only that. Every column is already connected to a checkbox, but I use this checkbox with an delete query, so I can delete multiple rows.
So my question is more like, how can I combine this two function with "one" checkbox.
This is some of my code.
This is my table with the sql outputs, and with the delete button
<?php
$files = mysql_query("SELECT * FROM files WHERE `user_name` = '{$_SESSION['username']}' ORDER BY `file_time` DESC LIMIT $start, $per_page")
or die(mysql_error());
?>
<table id="table-3">
<thead>
<tr>
<th scope="col">Upload User</th>
<th scope="col">Filename</th>
<th scope="col">Upload date</th>
<th scope="col">IP adress</th>
<th scope="col">Category</th>
<th scope="col">Delete</th>
</tr>
</thead>
<?php
while (($row = mysql_fetch_assoc( $files )) !==false)
{
echo "<tr>";
echo "<td>".$row['user_name'] . "</td> ";
?>
<td class="download"> <?php echo $row['file_name']; ?></td>
<?php echo "<td>".$row['file_time'] . "</td> "; ?>
<?php echo "<td>".$row['file_ip'] . "</td> "; ?>
<?php echo "<td>".$row['category'] . "</td> "; ?>
<td>
<form action="delete.php" method="post">
<input type="checkbox" name="done[]" id="<?php echo $row['file_id'] ?>" value="<?php echo $row['file_id'] ?>">
</td>
<?php
}
echo "</tr>";
echo "</table>";
?>
<input type ="submit" value ="Delete">
</form>
This form is the one with the update function
<form action="function.php" method="post">
category: <input type="text" name="category" />
<input type="submit" />
</form>
and
function.php
include('core/inc/init.inc.php');
if(isset($_POST['category'])){
$category = $_POST['category'];
mysql_query("UPDATE files SET category='$category'
/*WHERE category='genom' */ ");
header("Location: profile.php?uid=" . $_SESSION['uid']);
}else{
header("Location: profile.php?uid=" . $_SESSION['uid']);
};
How can I combine these two functions?
You can have multiple submit buttons in the same form. So you can do something like this:
<?php
$files = mysql_query("SELECT * FROM files WHERE `user_name` = '{$_SESSION['username']}' ORDER BY `file_time` DESC LIMIT $start, $per_page")
or die(mysql_error());
?>
<form action="updateordelete.php" method="post">
<table id="table-3">
<thead>
<tr>
<th scope="col">Upload User</th>
<th scope="col">Filename</th>
<th scope="col">Upload date</th>
<th scope="col">IP adress</th>
<th scope="col">Category</th>
<th scope="col">Delete</th>
</tr>
</thead>
<?php
while (($row = mysql_fetch_assoc( $files )) !==false)
{
?>
<tr>
<td> <?php echo $row['user_name']; ?></td>
<td class="download"> <?php echo $row['file_name']; ?></td>
<td><input type="text" name="file_time[<?php echo $row ['file_id']; ?>]" value="<?php echo $row['file_time']; ?>" /></td>
<td><input type="text" name="file_ip[<?php echo $row ['file_id']; ?>]" value="<?php echo $row['file_ip']; ?>" /></td>
<td><input type="text" name="file_category[<?php echo $row ['file_id']; ?>]" value="<?php echo $row['category']; ?>" /></td>
<td><input type="checkbox" name="done[]" id="<?php echo $row['file_id'] ?>" value="<?php echo $row['file_id'] ?>"></td>
</tr>
<?php } ?>
</table>
<input type ="submit" name="submittype" value = "Update">
<input type ="submit" name="submittype" value = "Delete">
</form>
And then, in updateordelete.php:
if($_POST['submittype']=="Delete"){
(do delete code...)
}elseif($_POST['submittype']=="Update"){
(do update code...)
}
For each of the values, you can access them using $_POST['file_category']. For example:
$file_categories=$_POST['file_category'];
foreach($_POST['done'] as $file_id){
echo $file_categories[$file_id];
}