My form
ITEM FORM
<form method="POST" action="index.php">
<div class="col-xs-4">ITEM ID<input type="text" name="itemid" class="form-control"/></div>
<div class="col-xs-4">ITEM NAME<input type="text" name="itemname" class="form-control"/></div>
<div class="col-xs-4">ITEM DETAIL<input type="text" name="itemdetail" class="form-control"/></div>
ITEM DESCRIPTION<input type="text" name="itemdescription" class="form- control"/>
<div class="col-xs-4">MANUFACTURER ID <input type="text" name="manufacturerid" class="form-control"/></div>
<div class="col-xs-4">TYPE ID <input type="text" name="typeid" value="4001" class="form-control"/></div>
<div class="col-xs-4">CATEGORY ID <input type="text" value="1003" name="categoryid" class="form-control"/></div>
<div class="col-xs-4">MODULE ID <input type="text" name="moduleid" class="form-control"/></div>
<input type="submit" name="itemSubmit" class="btn btn-default"/>
</form>
<?php echo $_GET['$lastid'] ?>
<table class="table table-hover">
<tbody>
<?php while($row = mysqli_fetch_array($allresult)) { ?>
<tr>
<td><?php echo $row['itemid']?></td>
<td><?php echo $row['itemname']?></td>
<td><?php echo $row['itemdetail']?></td>
<td><?php echo $row['manufacturerid']?></td>
<td><?php echo $row['moduleid']?></td>
</tr>
<?php } ?>
</tbody>
</table>
PRICE FORM
<form method="POST" action="index.php">
<div class="col-xs-4">ITEM ID <input type="text" name="itemid" class="form-control"/></div>
<div class="col-xs-4">SHOP ID <input type="text" name="shopid" class="form-control"/></div>
<div class="col-xs-4">PRICE <input type="text" name="price" class="form-control"/></div>
ITEM URL <input type="text" name="itemurl" class="form-control"/>
ITEM IMAGE <input type="text" name="itemimage" class="form-control"/>
<input type="submit" name="priceSubmit" class="btn btn-default"/>
</form>
My php
<?php
$servername = "localhost";
$username = "abc";
$password = "abc";
$database = "cd";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->mysqli_connect_error) {
die("Connection failed: " . $conn->mysqli_connect_error);
}
echo "Connected successfully";
$query1 = "
INSERT INTO items
( itemid
, itemname
, itemdetail
, itemdescription
, manufacturerid
, typeid
, categoryid
, moduleid
) VALUES
('".$_POST['itemid']."'
,'".$_POST['itemname']."'
,'".$_POST['itemdetail']."'
,'".$_POST['itemdescription']."'
,'".$_POST['manufacturerid']."'
,'".$_POST['typeid']."'
,'".$_POST['categoryid']."'
,'".$_POST['moduleid']."'
)";
$query2 = "SELECT COUNT(itemid) FROM products";
$query3 = "INSERT INTO prices (itemid, shopid, price, itemurl, itemimage VALUES ('".$_POST['itemid']."','".$_POST['shopid']."','".$_POST['price']."','".$_POST['itemurl']."','".$_POST['itemimage']."')";
$query4 = "SELECT * FROM items ORDER BY itemid DESC LIMIT 1";
if(isset($_POST['itemSubmit']))
{
mysqli_query($conn, $query1);
}
else if(isset($_POST['priceSubmit']))
{
mysqli_query($conn, $query3);
}
$lastid = mysqli_query($conn, $query2);
$allresult = mysqli_query($conn, $query4);
?>
Please check am I missing anything? It doesn't show any error and it doesn't insert data into table! Can I not implement two forms in one php file?
Also I tried to add logic that if I press itemSubmit or priceSubmit button then it should run respective query. Another thing is I have created a table and it doesn't display either.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "stack11";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->mysqli_connect_error) {
die("Connection failed: " . $conn->mysqli_connect_error);
}
echo "Connected successfully";
$query1 = "INSERT INTO items (itemid, itemname, itemdetail, itemdescription, manufacturerid, typeid, categoryid, moduleid) VALUES ('".$_POST['itemid']."','".$_POST['itemname']."','".$_POST['itemdetail']."','".$_POST['itemdescription']."','".$_POST['manufacturerid']."','".$_POST['typeid']."','".$_POST['categoryid']."','".$_POST['moduleid']."')";
$query2 = "SELECT * FROM items";
$query3 = "INSERT INTO prices (itemid, shopid, price, itemurl, itemimage) VALUES ('".$_POST['itemid']."','".$_POST['shopid']."','".$_POST['price']."','".$_POST['itemurl']."','".$_POST['itemimage']."')";
$query4 = "SELECT * FROM items ORDER BY itemid DESC LIMIT 1";
if(isset($_POST['itemSubmit']))
{
mysqli_query($conn, $query1);
}
else if(isset($_POST['priceSubmit']))
{
mysqli_query($conn, $query3);
}
$lastid = mysqli_query($conn, $query2);
$allresult = mysqli_query($conn, $query4);
?>
ITEM FORM
<form method="POST" action="#">
<div class="col-xs-4">ITEM ID<input type="text" name="itemid" class="form-control"/></div>
<div class="col-xs-4">ITEM NAME<input type="text" name="itemname" class="form-control"/></div>
<div class="col-xs-4">ITEM DETAIL<input type="text" name="itemdetail" class="form-control"/></div>
ITEM DESCRIPTION<input type="text" name="itemdescription" class="form- control"/>
<div class="col-xs-4">MANUFACTURER ID <input type="text" name="manufacturerid" class="form-control"/></div>
<div class="col-xs-4">TYPE ID <input type="text" name="typeid" value="4001" class="form-control"/></div>
<div class="col-xs-4">CATEGORY ID <input type="text" value="1003" name="categoryid" class="form-control"/></div>
<div class="col-xs-4">MODULE ID <input type="text" name="moduleid" class="form-control"/></div>
<input type="submit" name="itemSubmit" class="btn btn-default"/>
</form>
<?php echo mysqli_num_rows($lastid); ?>
<table class="table table-hover">
<tbody>
<?php while($row = mysqli_fetch_array($allresult)) { ?>
<tr>
<td><?php echo $row['itemid']?></td>
<td><?php echo $row['itemname']?></td>
<td><?php echo $row['itemdetail']?></td>
<td><?php echo $row['manufacturerid']?></td>
<td><?php echo $row['moduleid']?></td>
</tr>
<?php } ?>
</tbody>
</table>
PRICE FORM
<form method="POST" action="#">
<div class="col-xs-4">
ITEM ID
<input type="text" name="itemid" class="form-control"/>
</div>
<div class="col-xs-4">
SHOP ID
<input type="text" name="shopid" class="form-control"/>
</div>
<div class="col-xs-4">
PRICE
<input type="text" name="price" class="form-control"/>
</div>
ITEM URL
<input type="text" name="itemurl" class="form-control"/>
ITEM IMAGE
<input type="text" name="itemimage" class="form-control"/>
<input type="submit" name="priceSubmit" class="btn btn-default"/>
Change $query2 to $query2 = "SELECT * FROM items"; and replace <?php echo $_GET['$lastid'] ?> to <?php echo mysqli_num_rows($lastid); ?>
Related
My code is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SomuFinance - Personal Finance Manager</title>
<link rel="stylesheet" type="text/css" href="indexStyle.css">
<script src="scripts/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="scripts/jquery.validate.min.js"></script>
<style type="text/css">
#addItemContainer {
background-color: rgba(204,207,232,1);
}
</style>
<script type="text/javascript">
var flag=0;
</script>
</head>
<body>
<form id="list" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="container">
<input type="submit" class="button" name="edit" id="edit" value="Edit" />
<input type="button" class="button" name="delete" value="Delete" />
<input type="hidden" id="action" name="action">
<table id="listDB">
<tr>
<th>Select</th>
<th>ID</th>
<th>Category ID</th>
<th>Shop</th>
<th>Item</th>
<th>Quantity</th>
<th>Unit</th>
<th>Price Based On</th>
<th>MRP</th>
<th>Seller's Price</th>
<th>Last Updated On</th>
</tr>
<?php
$dbc = mysqli_connect('localhost','root','atlantis2016','itemDB')
or die("Error Connecting to Database");
if(isset($_POST['confirmDelete']))
{
if($_POST['action']=='confirmDelete')
{
foreach ($_POST['selected'] as $delete_id)
{
$query = "DELETE FROM grocery WHERE id = $delete_id";
mysqli_query($dbc, $query)
or die('Error querying database.');
}
}
}
$query1 = "SELECT DISTINCT category FROM grocery";
$result1 = mysqli_query($dbc, $query1)
or die("Error Querying Database");
while($row = mysqli_fetch_array($result1))
{
$category = $row['category'];
$query2 = "SELECT * FROM grocery WHERE category='$category' ORDER BY item ASC";
$result2 = mysqli_query($dbc, $query2)
or die("Error Querying Database");
echo '<tr>';
echo '<td class="catHead" colspan=11>'.$category.'</td>';
echo '</tr>';
$catCount=1;
while($inRow = mysqli_fetch_array($result2))
{
$id = $inRow['id'];
$shop = $inRow['shop'];
$item = $inRow['item'];
$qnty = $inRow['quantity'];
$unit = $inRow['unit'];
$price_based_on = $inRow['price_based_on'];
$mrp = $inRow['MRP'];
$sellers_price = $inRow['sellers_price'];
$last_updated_on = $inRow['last_updated_on'];
echo '<tr>';
echo '<td><input type="checkbox" id="selected" value="' . $id . '" name="selected[]" /></td>';
echo '<td>'.$id.'</td>';
echo '<td>'.$catCount.'</td>';
echo '<td>'.$shop.'</td>';
echo '<td class="leftAligned">'.$item.'</td>';
echo '<td>'.$qnty.'</td>';
echo '<td>'.$unit.'</td>';
echo '<td>'.$price_based_on.'</td>';
echo '<td class="pri">₹'.$mrp.'</td>';
echo '<td class="pri">₹'.$sellers_price.'</td>';
echo '<td>'.$last_updated_on.'</td>';
echo '</tr>';
$catCount++;
}
}
?>
</table>
</div>
<div class="dialogBG">
<div id="deleteConfirmDialog" class="dialog">
<div class="closeDialog"></div>
<p>Sure you want to delete the selected Data?</p>
<input type="submit" id="confirmDelete" class="dialogButton" name="confirmDelete" value="Delete" />
<input type="button" id="cancelDelete" class="dialogButton cancelButton" name="cancelDelete" value="Cancel" />
</div>
</div>
</form>
<div class="dialogBG">
<div id="addItemContainer" class="dialog">
<div class="closeDialog"></div>
<h1>Edit Item</h1>
<form id="data" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php
if(isset($_POST['action']))
{
if($_POST['action']=='edit')
{
echo '<script>flag=1;</script>';
foreach ($_POST['selected'] as $edit_id)
{
$query = "SELECT * FROM grocery WHERE id = $edit_id";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
break;
}
$inRow = mysqli_fetch_array($result);
$id = $inRow['id'];
$shop = $inRow['shop'];
$category = $inRow['category'];
$item = $inRow['item'];
$qnty = $inRow['quantity'];
$unit = $inRow['unit'];
$price_based_on = $inRow['price_based_on'];
$mrp = $inRow['MRP'];
$sellers_price = $inRow['sellers_price'];
$last_updated_on = $inRow['last_updated_on'];
}
}
?>
<div class="leftAligned">
<div class="inp">
<label for="shop">ID : </label>
<input type="text" id="id" name="id" value="<?php echo $id; ?>" required disabled>
</div> <br>
<div class="inp">
<label for="shop">Shop : </label>
<input type="text" id="shop" name="shop" value="<?php echo $shop; ?>" required>
</div> <br>
<div class="inp">
<label for="category">Category : </label>
<input type="text" id="category" name="category" value="<?php echo $category; ?>" required>
</div> <br>
<div class="inp">
<label for="item">Item : </label>
<input type="text" id="item" name="item" value="<?php echo $item; ?>" required>
</div> <br>
<div class="inp">
<label for="qnty">Quantity : </label>
<input type="text" id="qnty" name="qnty" value="<?php echo $qnty; ?>" required>
</div> <br>
<div class="inp">
<label for="unit">Unit : </label>
<input type="text" id="unit" name="unit" value="<?php echo $unit; ?>" required>
</div> <br>
<div class="inp">
<label for="price_based_on">Price based on : </label>
<select name="price_based_on" id="price_based_on">
<option value="kilos">Kilos</option>
<option value="packet">Packet</option>
<option value="bottle">Bottle</option>
<option value="box">Box</option>
<option value="piece">Piece</option>
</select>
</div> <br>
<div class="inp">
<label for="mrp">MRP (₹) : </label>
<input type="text" id="mrp" name="mrp" value="<?php echo $mrp; ?>" required>
</div> <br>
<div class="inp">
<label for="sellers_price">Seller's Price (₹) : </label>
<input type="text" id="sellers_price" value="<?php echo $sellers_price; ?>" name="sellers_price" required>
</div> <br>
<div class="inp">
<label for="last_updated_on">Last Updated on : </label>
<input type="date" id="last_updated_on" name="last_updated_on" value="<?php echo $last_updated_on; ?>" required>
</div>
</div>
<div class="inp">
<input id="insertButton" type="submit" name="submit" value="Insert">
</div>
<div id="message">
<?php
if(isset($_POST['submit']))
{
echo "<script> alert('$id'); </script>";
$shop = $_POST['shop'];
$category = $_POST['category'];
$item = $_POST['item'];
$qnty = $_POST['qnty'];
$unit = $_POST['unit'];
$price_based_on = $_POST['price_based_on'];
$mrp = $_POST['mrp'];
$sellers_price = $_POST['sellers_price'];
$last_updated_on = $_POST['last_updated_on'];
$result=null;
$query = "UPDATE grocery SET shop='$shop', category='$category', item='$item', quantity='$qnty', unit='$unit', price_based_on='$price_based_on', mrp='$mrp', sellers_price='$sellers_price', last_updated_on='$last_updated_on' WHERE id='$id'";
if(!empty($shop)&&!empty($category)&&!empty($item)&&is_numeric($qnty)&&!empty($unit)&&is_numeric($mrp)&&is_numeric($sellers_price)&&!empty($last_updated_on))
{
$result = mysqli_query($dbc, $query)
or die(mysqli_error($dbc));
}
if($result)
{
echo '<span class="success">Item Edited Successfully!</span>';
}
else
{
echo '<span class="failure">Failed to insert Item.</span>';
}
//header("Refresh:2");
}
?>
<script>
$(document).ready(function(){
$( "#data" ).validate({
rules: {
qnty: {
number: true
},
mrp: {
number: true
},
sellers_price: {
number: true
}
},
messages: {
qnty : {
number: '<br> <span class="failure err">Enter a valid quantity</span>'
},
mrp : {
number: '<br> <span class="failure err">Enter a valid MRP</span>'
},
sellers_price : {
number: '<br> <span class="failure err">Enter a valid Price</span>'
},
}
});
});
</script>
</div>
</form>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('.button').click(function(event){
if($(this).val()=="Delete")
{
$("#deleteConfirmDialog").show(200).parent(".dialogBG").fadeIn(200);
$("#action").val('confirmDelete');
}
else if($(this).val()=="Edit")
{
event.preventDefault();
$("#action").val('edit');
$("#list").submit();
}
});
if(flag===1)
{
console.log("This shouldn't be there if the page reloads!");
$("#addItemContainer").show(200).parent(".dialogBG").fadeIn(200);
}
$('#confirmDelete').click(function(){
$(".closeDialog").trigger("click");
});
$('#cancelDelete').click(function(){
$("input:checkbox[name='selected[]']").prop('checked', false);
});
$(".closeDialog").click(function (e){
$(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
});
$(".cancelButton").click(function (e){
$(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
});
});
</script>
<?php
mysqli_close($dbc);
?>
</body>
</html>
Notice that I obtain the id of the first selected element by using the lines:
foreach ($_POST['selected'] as $edit_id)
{
$query = "SELECT * FROM grocery WHERE id = $edit_id";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
break;
}
$inRow = mysqli_fetch_array($result);
$id = $inRow['id'];
Now, I use the id(which is exactly the same as edit_id) thus obtained to access the record in the DB, and show it in the popup.
<div class="leftAligned">
<div class="inp">
<label for="shop">ID : </label>
<input type="text" id="id" name="id" value="<?php echo $id; ?>" required disabled>
</div> <br>
<div class="inp">
<label for="shop">Shop : </label>
<input type="text" id="shop" name="shop" value="<?php echo $shop; ?>" required>
</div> <br>
<div class="inp">
<label for="category">Category : </label>
<input type="text" id="category" name="category" value="<?php echo $category; ?>" required>
</div> <br>
<div class="inp">
<label for="item">Item : </label>
<input type="text" id="item" name="item" value="<?php echo $item; ?>" required>
</div> <br>
<div class="inp">
<label for="qnty">Quantity : </label>
<input type="text" id="qnty" name="qnty" value="<?php echo $qnty; ?>" required>
</div> <br>
<div class="inp">
<label for="unit">Unit : </label>
<input type="text" id="unit" name="unit" value="<?php echo $unit; ?>" required>
</div> <br>
<div class="inp">
<label for="price_based_on">Price based on : </label>
<select name="price_based_on" id="price_based_on">
<option value="kilos">Kilos</option>
<option value="packet">Packet</option>
<option value="bottle">Bottle</option>
<option value="box">Box</option>
<option value="piece">Piece</option>
</select>
</div> <br>
<div class="inp">
<label for="mrp">MRP (₹) : </label>
<input type="text" id="mrp" name="mrp" value="<?php echo $mrp; ?>" required>
</div> <br>
<div class="inp">
<label for="sellers_price">Seller's Price (₹) : </label>
<input type="text" id="sellers_price" value="<?php echo $sellers_price; ?>" name="sellers_price" required>
</div> <br>
<div class="inp">
<label for="last_updated_on">Last Updated on : </label>
<input type="date" id="last_updated_on" name="last_updated_on" value="<?php echo $last_updated_on; ?>" required>
</div>
</div>
The ID and all the relevant details thus far are exactly as they should be.
However, further down, when I try to use the ID to update the record, using the statement
UPDATE grocery SET shop='$shop', category='$category', item='$item',
quantity='$qnty', unit='$unit', price_based_on='$price_based_on',
mrp='$mrp', sellers_price='$sellers_price',
last_updated_on='$last_updated_on'
WHERE id='$id'
somehow the id has changed to the last value of id in the table. So, instead of updating the table's correct record with the updated values, since the id "magically" changes at this point (after the sumbit button has been pressed), the last value of the id appears. I want the id to stay the same as the original id.
Why is this happening and how can I solve this?
I am trying to GET the selected ID from the previous form and insert the id into another database. After inserting the data from the form the ID should not be cleared whereas other details can be cleared.
Here is the screenshot of from the form where ID has to be inserted
Screeshot
When the user clicks on Add image particular ID in which Add image is clicked as to be posted in another form.
After submitting the form the ID shouldnt be cleared. Whereas the other textbox can get cleared.
Here is the code
$query2 = "SELECT * FROM abc";
$result2=mysql_query($query2) or die("Query Failed : ".mysql_error());
if(isset($_POST['submit']))
{
$id= $_POST['id'];
if( ! ctype_alnum($id) )
die('invalid id');
$query = "SELECT id FROM `abc` WHERE `id` =$id";
$run = mysql_query($query);
if(mysql_num_rows($run)>0){
echo "<script>window.open('addimg.php?id=".$id."','_self')</script>";
}
else {
echo "<script>alert('No is Invalid!')</script>";
}
}
?>
<table width='300' align='center' border='1' cellpadding="5px" cellspacing="0px" style="color:#494949;font-size:12px; font-family:Cambria;">
<tr>
<td>ID</td>
<td>URl Small Image</td>
<td>URL Large Image</td>
<td>Add Image</td></tr>
<?php
while($row=mysql_fetch_array($result2))
{?>
<form method="post" action="report.php">
<tr><td>
<input type="text" name="id" class="input" size="20" value="<?php echo $row['id']; ?>" readonly></td>
<td>
<input type="text" name="s_img" class="input" size="20" value="<?php echo $row['s_img']; ?>" readonly></td>
<td>
<input type="text" name="l_img" class="input" size="20" value="<?php echo $row['l_img']; ?>" readonly></td>
<td> <input type="submit" name="submit" value="ADD IMAGE" class="button" /></td>
</form>
<?php
}
?>
Addimg.php
<form name="XIForm" id="XIForm" method="POST" action="addimg.php">
<h1>News Description</h1>
<p>
<label for="News_id" class="uname"> News_ID</label>
<input type="text" id="news_id" name="news_id" type="text" value="<?php if(isset($_GET['id'])) { echo $_GET['id']; } ?>" readonly> <br />
</p>
<label for="img1" class="uname"> Img1</label>
<input type="text" id="img1" name="img1" type="text"/> <br />
</p>
PHP code
if(isset($_POST['XIsubmit'])) {
$news_id = $_POST["news_id"];
$img1 = $_POST["img1"];
if($img1!="")
{
$query=("INSERT INTO images (news_id,img)VALUES('$news_id','$img1')") or die(mysql_error());
$retval = mysql_query( $query, $dbhandle );
echo ""
if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}
}
}
?>
I am using Unique Key for the field 'name', but when I update the record using edit file through admin, it gives me an error for duplicate entry. I can't remove unique key, it's required..
<!--update process-->
<?php
if(isset($_POST['submit']) and !empty($_POST['token'])){
print_r($_POST);
$name = $_POST['name'];
$size = $_POST['size'];
$linkt = $_POST['link'];
$seeds = $_POST['seeds'];
$leechs = $_POST['leechs'];
$active = $_POST['active'];
$query = "UPDATE tplus_torrentlist SET name = '$name', size = '$size', link = '$linkt', seeds = '$seeds', leechs = '$leechs', active = '$active'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
$error = mysqli_error($link);
if(empty($error)){
$_SESSION['flash'] = '<blockquote style="background: green; color: #fff">One record updated</blockquote>';
header('location:dashbord.php');
}
else{
$_SESSION['flash'] = '<blockquote style="background: green; color: #fff">Sorry cant updated this record</blockquote>';
header('location:dashbord.php');
}
}
?>
<!--fetch values from database according to id-->
<?php
$sql = "SELECT * FROM tplus_torrentlist WHERE id = $id limit 1";
$result = mysqli_query($link ,$sql) or die(mysqli_error($link));
$row = $result->fetch_array();
?>
<div class="container">
<div class="row clearfix">
<div class="col-md-8 col-md-offset-2">
<?php if(!empty($response)){echo $response;} ?>
<h3>Edit this torrent</h3>
<hr/>
<form role="form" class="form" action="edit.php?id=<?php echo $_GET['id']?>" method="POST">
<label>Name</label>
<input type="text" name="name" value="<?php echo $row['name']?>" class="form-control input-sm">
<label>Size</label>
<input type="text" name="size" value="<?php echo $row['size']?>" class="form-control input-sm">
<label>Link</label>
<input type="text" name="link" value="<?php echo $row['link']?>" class="form-control input-sm">
<label>Seeds</label>
<input type="text" name="seeds" value="<?php echo $row['seeds']?>" class="form-control input-sm">
<label>Leechs</label>
<input type="text" name="leechs" value="<?php echo $row['leechs']?>" class="form-control input-sm">
<label>Active</label>
<select name="active" class="form-control input-sm">
<?php if($row['active'] ==0){?>
<option value="0">Active</option>
<option value="1">InActive</option>
<?php }else{ ?>
<option value="1">InActive</option>
<option value="0">Active</option>
<?php } ?>
</select>
<br/>
<input type="hidden" name="token" value="<?php echo rand(100, 100000)?>">
<input type="submit" name="submit" value="update torrent" class="btn btn-info">
</form>
</div>
</div>
</div>
Any solution without removing unique key from the field 'name' ?
Your update query need to correct as following -
$query = "UPDATE tplus_torrentlist SET name = '$name', size = '$size', link = '$linkt', seeds = '$seeds', leechs = '$leechs', active = '$active' WHERE id = $id limit 1";
I am trying to retrieve value on php page but it's not retrieving value.
Here is my code
retrieve
<html>
<body>
<?php
include('conn.php');
$per_page = 3;
if($_GET)
{
$page=$_GET['page'];
}
$start = ($page-1)*$per_page;
$select_table = "select * from clientreg order by id limit $start,$per_page";
$variable = mysql_query($select_table);
?>
<form name="frmUser" method="post" action="">
<div style="width:100%;">
<table border="0" cellpadding="10" cellspacing="1" width="100%" class="tblListForm">
<tr class="listheader">
<td></td>
<td width="230" >*****</td>
</tr>
<?php
$i=1;
$j=0;
while($row = mysql_fetch_array($variable))
{
if($j%2==0)
$classname="evenRow";
else
$classname="oddRow";?>
<tr class="<?php echo $classname;?>">
<td><input type="checkbox" name="users[]" value="<?php echo $row["id"]; ?>" ></td>
</tr>
<?php
$j++;}
?>
<tr class="listheader">
<td colspan="9"><input type="button" name="update" id="onclick" value="Update" /> <input type="button" name="delete" value="Delete" onClick="setDeleteAction();" />
<input type="button" name="assign" id="assign" value="Assign" onClick="setLeadAssignAction();" />
<?php
$sql = mysql_query("SELECT *FROM login where role=1");
while ($row = mysql_fetch_array($sql)){
?>
<tr class="listheader">
<td><input type="checkbox" name="eid[]" value="<?php echo $row["eid"]; ?>" ><?php echo $row["username"]; ?></td>
</tr>
<?php
}
?>
</td>
</tr>
</table>
</div>
</form>
<form class="form" method ="Post"action="" id="contact">
<?php
if(isset($_POST["submit"]) && $_POST["submit"]!="") {
$rowCount = count($_POST["users"]);
for($i=0;$i<$rowCount;$i++) {
$result = mysql_query("SELECT * FROM clientreg WHERE Id='" . $_POST["users"][$i] . "'");
$row[$i]= mysql_fetch_array($result);
echo "shakti";
echo $row[$i]['id'];
}
}
?>
<img src="button_cancel.png" class="img" id="cancel"/>
<div id="left" style="height:400px;width:47%;float:left;margin-left:20px;margin-top:15px;border-radius:10px;">
<label>Lead Owner: <span>*</span></label>
<br/>
<input type="text" name="leadowner[]" id="lead" placeholder="Lead Owner"value=""/><br/>
<br/>
<label>First Name: <span>*</span></label>
<br/>
<input type="text" name="fname"id="fname" placeholder="Fname"/><br/>
<br/>
<label>Last Name: <span>*</span></label>
<br/>
<input type="text" name="lname" id="lname" placeholder="Lname"/><br/>
<br/>
<label>Mobile No: <span>*</span></label>
<br/>
<input type="text" name="mobile"id="mobile" placeholder="Mobile"/><br/>
<br/>
<label>Email Id: <span>*</span></label>
<br/>
<input type="text"name="email" id="email" placeholder="Email"/><br/>
</div>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Where am I wrong in this code?
Please sort out my problem
My problem is here
if(isset($_POST["submit"]) && $_POST["submit"]!="") {
$rowCount = count($_POST["users"]);
Any help will be appreciated
I have updated my code and added submit button.
You have two forms. Only the second form has a submit.
Form 1:
<form name="frmUser" method="post" action="">
<input type="checkbox" name="users[]" value="<?php echo $row["id"]; ?> ">
</form>
Form 2:
<form class="form" method ="Post"action="" id="contact">
<input type="submit" name="submit" value="Submit">
</form>
The if construct does not receive the $_POST["users"], because it receives only the POST of the second submitted form.
$rowCount = count($_POST["users"]);
$rowCount will always be 0.
Form 2:
...
<?php
$rowCount = 0;
if ($_POST["users"] != "") {
$rowCount = count($_POST["users"]);
} else if ($_POST["rowcount"] != "") {
$rowCount = $_POST["rowcount"];
}
?>
...
<form class="form" method ="Post"action="" id="contact">
...
<input type="submit" name="submit" value="Submit">
<input type="hidden" name="rowcount" value="<?php echo $rowCount; ?>">
</form>
Then the variable $rowCount will contain the count of the rows after the submit of any of the 2 forms.
<?php
//include 'includes/connectie.php';
if (isset($_GET['id'])){
$product_id=$_GET['id'];
} else {
$product_id=$_POST['id'];
}
$user = 'userID';
$pass = 'mypassword';
$dbh = new PDO( 'mysql:host=localhost;dbname=webshop', $user, $pass );
$sql = "SELECT * FROM `producten` WHERE product_id='$product_id'";
$sql_result = $dbh->query($sql);
foreach($sql_result as $row)
{
$prijs=$row['prijs'];
$product_naam=$row['product_naam'];
$product_categorie=$row['product_categorie'];
$product_specificaties=$row['product_specificaties'];
$foto=$row['foto'];
$product_id=$row['product_id'];
$product_soort=$row['product_soort'];
echo "Product id nummer:", $product_id;
}
//$_SESSION['prijs'] = $prijs;
if ($_SERVER["REQUEST_METHOD"] == "POST"){
//if (!empty($product_naam) && !empty($product_specifcaties) && !empty($product_categorie) && !empty($prijs)
//&& !empty($product_soort))
If (isset($_POST['submit']))
{
$sql = "UPDATE producten
SET prijs='$prijs', product_naam='$product_naam', product_specificaties='$product_specificaties',
product_categorie='$product_categorie', product_soort='$product_soort',
WHERE product_id='$product_id'";
$query = $dbh->prepare( $sql );
$result = $query->execute();
if ($result){
echo "Product aangepast!!!!! in id:";
echo $product_id;
} else {
echo "Product NIET aangepast!!!!";
}
}
}
?>
<form name="admin" action="producten_echt_aanpassen.php" method="POST" enctype="multipart/form-data">
<p>
<label for 'product_id'>Product ID: </label><br>
<input type="text" name="id" value="<?php print $product_id; ?>"/>
</p>
<p>
<label for 'product_naam'>Naam: </label><br>
<input type="text" name="product_naam" value="<?php print $product_naam; ?>"/>
</p>
<p> <label for 'product_specificaties'>Specificaties: </label><br>
<textarea rows= "4" cols="50" name="product_specificaties"><?php print $product_specificaties; ?>
</textarea>
</p>
<p>
<label for 'prijs'>Prijs: </label><br>
<input type="text" name="prijs" value="<?php print $prijs; ?>"/>
</p>
<p>
<label for 'product_categorie'>Iphone: </label><br>
<input type="text" name="product_categorie" value="<?php print $product_categorie; ?>"/>
</p>
<p>
<label for 'product_soort'>Soort: </label><br>
<input type="text" name="product_soort" value="<?php print $product_soort; ?>"/>
</p>
<br/>
<label for 'uploadfile'>Kies foto <img src="<?php print $foto; ?>"></label><br>
<input type="file" name="file" ><br><br>
<input type="submit" name="submit" value="Submit">
</form>
I have a form in which I load properties of products like the product name, price, photo etc. The properties are then possible to change and then updated in the database. But the sql update statement does not execute. Can anybody help me out?
there is a , before the where on the update that should not be there. Try to activate error reporting like this: How to get useful error messages in PHP? so that you know wwhy things are failing