With the code below, im trying to duplicate a product row with ajax in a webshops table, named termek.
If the last product is ID 90, and i copy this, the new product will be ID 91.
Sometimes, and i dont know why, the ID 90(what i copyed), loose the image, the thumb and big cell in the table, or it is renamed or i dont know what going on.
Is this code okay for copying, or whats wrong whit it?
<?php
include_once("../../files/connect.php");
if(!empty($_POST))
{
$id = mysqli_real_escape_string($kapcs, $_POST['id']);
$sql =
"
INSERT INTO termek
(
termek_nev,
termek_seo,
termek_rovid,
termek_hosszu,
termek_thumb,
termek_big,
termek_seo_title,
termek_seo_rovid,
termek_seo_kw,
termek_status,
termek_view,
termek_akcio,
termek_normal_ar,
termek_akcios_ar,
termek_cikkszam,
termek_egyseg,
termek_fooldal,
termek_kiemelt,
termek_suly,
termek_keszlet_db,
termek_keszlet_warning,
termek_min_order,
termek_allapot,
termek_gyarto,
termek_jobb_arat_btn,
termek_tipus,
termek_szavak
)
SELECT
termek_nev,
termek_seo,
termek_rovid,
termek_hosszu,
termek_thumb,
termek_big,
termek_seo_title,
termek_seo_rovid,
termek_seo_kw,
termek_status,
termek_view,
termek_akcio,
termek_normal_ar,
termek_akcios_ar,
termek_cikkszam,
termek_egyseg,
termek_fooldal,
termek_kiemelt,
termek_suly,
termek_keszlet_db,
termek_keszlet_warning,
termek_min_order,
termek_allapot,
termek_gyarto,
termek_jobb_arat_btn,
termek_tipus,
termek_szavak
FROM termek WHERE termek_id = '$id'
";
if(mysqli_query($kapcs, $sql))
{
echo (int)1;
}
else
{
echo mysqli_error($kapcs);
}
}
?>
To solve the problem, i uploaded a noimage.jpg to the products folder. When im editing the copied or the old product and changing its image, i dont delete the old image, if the thumb or big column == noimage.jpg.
Sorry for bad english.
Related
I have a Table where user can create row with adding input fields dynamically (combination with jquery). I'm successfully able to insert it into the mysql database.
If users want to edit the added already existing fields, I have an edit page where the values are fetched from the mysql DB and populated again into the dynamically creatable table.
Now there are the below probabilities:-
User only makes minor changes on the existing values. In that case
the table has to be UPDATED with the changed values
User Deletes one/multiple row(randomly selected and as per users wish). So when form submitted the php query should only DELETE that perticular row/s in the DB.
User ADDS another row to the previous existing row values, in that case the php query should UPDATE the previous values and INSERT the newly added row values.
The above sequence is not necessarilly restricted the same order. User can perform all the above three function simultaneously at the same time.
Now my problem is(only for the backend) I'm finding a hard time to frame a php & sql query so as to update to the mysql.
my php
if(isset($_POST['submit'])){
$number1 = count($_POST['item']); //
for($i=0; $i<$number1; $i++){
$item = strip_tags(trim($_POST['item'][$i]));
$description = strip_tags(trim($_POST['description'][$i]));
$unitcost = strip_tags(trim($_POST['unitcost'][$i]));
$qty = strip_tags(trim($_POST['qty'][$i])); // Quantity
$sno = strip_tags(trim($_POST['sno'][$i]));// serial number
//QUERY1 if minor updates to above variable then UPDATE (eg, qty value is changed from 3 to 4)
//QUERY2 if row is deleted then DELETE that particular row from db (eg, sno 3 deleted from the table should DELETE corresponding mysql DB values also)
//QUERY3 if row is added then that particular row values should be INSERT (eg, sno 4 is added which is not in the mysql db. So this has to be INSERTED.)
}
}
Pardon me to have asked such question. I'm wasting a whole lot of time with the above queries unable to execute properly. I only require an idea not necessarily the whole code.
Hope all of you out there would advice me some ideas on how this could be implemented. Thanks for the help in advance. Expecting a positive reply.
NB: Just to remind you again, The front end is a Dynamically ADD/DELETE Input Field table
This sounds like a frontend problem. You need to define how you tell the backend whats happening.
<input name="items[$i][name]" />
This will show up as nice array to loop through in php.
foreach($_POST[items] AS $item){
if( $item['delete'] ){
//delete code
}
}else{
//Insert/Update
}
If you want to delete something simply make the field hidden and add a flag to it.
<input type="hidden" name="items[$i][delete]" value="1" />
<input type="hidden" name="items[$i][id]" />
Thank for the reply, appreciate #ckrudelux and #codefather for their intention to help me.
Although their advise didn't help me to structure my query. So I had a long workaround and found out below solution. I'm posting the solution because I couldn't find any article online when it comes to UPDATE/DELETE a dynamically generated input table.
Hope this would be of help to someone.
So what I did basically is that I took all the values into array.
In my dynamically generated add input table code, I added an <input type="hidden" name="sno[]" value="newrow">. So this will be clubbed with the form post. I'm using the normal html post and not ajax.
now my submit.php has ben changed to below
if(isset($_POST['submit'])){
$productid = $_POST['productd'];// No striptag functions
// due to illustration purpose
// First of all, we need to fetch the querying db table.
// This is required in order to compare the existing row values
// with the posted values
$fetchproduct = $link->prepare("SELECT * FROM product WHERE productid=?");
$fetchproduct ->bind_param('s',$productid);
$fetchproduct ->execute();
$fetchresult = $fetchproduct ->get_result();
$serialnumber=array(); // Assigning array to fetch the primary key: Serial Number
while($row = $fetchresult->fetch_assoc()){
$serialnumber[] = $row["sno"];
}
//Newly Inserted Values
//$_POST['sno'] is taken from the dynamic input field defined earlier in this post.
//Basically what we are doing here is we are comparing (the values
//which have been posted from the primary page) and (values present in the db table).
//The difference will give an array of newly inserted table input field values
$insert = array_diff($_POST['sno'],$serialnumber);
//Deleted Values
// This will Difference those values in the db table and values which are
// deleted from the primary dynamic table page
$delete = array_diff($serialnumber,$_POST['sno']);
$countdelete = count($delete); // Counting how many values have been
// lined up for deleting
//Updated Values
// array_intersect will give us the common values present in both the array.
// This means that there is no deletion or insertion to the dynamic table fields.
$intersect = array_intersect($serialnumber, $_POST['sno']);
$update = array_values($intersect);
$countupdate = count($update);
//INSERT ADDED VALUES TO DB
foreach($insert as $key=>$ivalue){
// ID
if(isset($_POST['id'][$key]) && !empty($_POST['id'][$key])) {
$id = strip_tags(trim($_POST['id'][$key]));
}
// ITEM
if(isset($_POST['item'][$key]) && !empty($_POST['item'][$key])) {
$item = strip_tags(trim($_POST['item'][$key]));
}
// DESCRIPTION
if(isset($_POST['description'][$key]) && !empty($_POST['description'][$key])) {
$description = strip_tags(trim($_POST['description'][$key]));
}
// UNITCOST
if(isset($_POST['unitcost'][$key]) && !empty($_POST['unitcost'][$key])) {
$unitcost = strip_tags(trim($_POST['unitcost'][$key]));
}
// QUANTITY
if(isset($_POST['qty'][$key]) && !empty($_POST['qty'][$key])) {
$qty = strip_tags(trim($_POST['qty'][$key]));
}
// AMOUNT
if(isset($_POST['amount'][$key]) && !empty($_POST['amount'][$key])) {
$amount = strip_tags(trim($_POST['amount'][$key]));
}
// INSERT INTO THE DATABASE
$inserttable = $link->prepare("INSERT INTO product (productid, item, description, unitcost, qty, amount) VALUES(?,?,?,?,?,?)");
$inserttable->bind_param('ssssss', $id, $item, $description, $unitcost, $qty, $amount);
$inserttable->execute();
if($inserttable){
header( 'Location:to/your/redirect page.php' ) ; // NOT MANDADTORY, You can put whatever you want
$_SESSION['updatemsg'] = "Success";
}
}
//UPDATE EXISTING VALUES TO DB
for($j=0; $j<$countupdate; $j++){
// ID
if(isset($_POST['id'][$j]) && !empty($_POST['id'][$j])) {
$uid = strip_tags(trim($_POST['id'][$j]));
}
// ITEM
if(isset($_POST['item'][$j]) && !empty($_POST['item'][$j])) {
$uitem = strip_tags(trim($_POST['item'][$j]));
}
// DESCRIPTION
if(isset($_POST['description'][$j]) && !empty($_POST['description'][$j])) {
$udescription = strip_tags(trim($_POST['description'][$j]));
}
// UNITCOST
if(isset($_POST['unitcost'][$j]) && !empty($_POST['unitcost'][$j])) {
$uunitcost = strip_tags(trim($_POST['unitcost'][$j]));
}
// QUANTITY
if(isset($_POST['qty'][$j]) && !empty($_POST['qty'][$j])) {
$uqty = strip_tags(trim($_POST['qty'][$j]));
}
// AMOUNT
if(isset($_POST['amount'][$j]) && !empty($_POST['amount'][$j])) {
$uamount = strip_tags(trim($_POST['amount'][$j]));
}
// UPDATE THE DATABASE
$updatetable = $link->prepare("UPDATE product SET item=?, description=?, unitcost=?, qty=?, amount=? WHERE sno=?");
$updatetable->bind_param('ssssss', $uitem, $udescription, $uunitcost, $uqty, $uamount, $update[$j]);
$updatetable->execute();
if($updatetable){
$_SESSION['updatemsg'] = "Success";
}
}
//DELETE VALUES FROM DB
foreach($delete as $sno){
$deletetable = $link->prepare("DELETE FROM product WHERE sno=?");
$deletetable->bind_param('s', $sno);
$deletetable->execute();
if($deletetable){
$_SESSION['updatemsg'] = "Success";
}
}
}else {
$_SESSION['updatemsg'] = "Error";
}
}
I'm developing an app about books and I had a table for contents. before, I made a mistake and I split content in myself and insert them in database. after inserting 90 books I had 1000 records and I found out that it was wrong. Now, I create a table of content and I insert all content of every books(every book has a record), so I want to split content with character(*).
but it doesn't work.
It is my Previous code:
$sql2 = "SELECT tblcontent.content
from tblcontent,tblstory
where tblcontent.storyid=tblstory.storyid
and tblcontent.storyid='$storyid'";
$r2 = #mysqli_query($dbLink,$sql2);
$result2 = array();
while($res2 = #mysqli_fetch_assoc($r2))
{
$result2[] = $res2;
}
and I've added this code:
$result2=explode("*",$result2);
and
if(isset($result2)){
echo json_encode(array("result2"=>$result2));
}
But it doesn't work.Thanks in advance
I'm quite new to php and mysql so forgive me if I'm doing this completely wrong. I am making a printing balance application and the code below is a part of it.
$command="SELECT itemname FROM items";
$results = mysql_query($command);
while($row = mysql_fetch_assoc($results))
{
foreach ($row as $key => $value) {
print "<input type='radio' name='itemtype' value='$value'>".$value."</input><br />";
}
}
This here is supposedly the price printing form where the user chooses between SHORT BOND PAPER and LONG BOND PAPER (the column itemname from items). The options appear as radio buttons. It works but now I'm stuck with the issue of being able to fetch the price as inserted in their respective rows. Since the itemname and their price are all user-inputted into the database, I'm not supposed to declare their specific price into the code itself, and should be able to retrieve it from the database. I want to be able to get the prices based on the item chosen by the user once they click submit, because I'd need to do this to compute for the price of printing when multiplied with the number of pages later.
I think it's definitely possible but I'm not quite sure how. Theoretically, it would be along the lines of SELECT itemprice FROM items WHERE itemname = $value but ha, I don't think it works that way.
solution edit: here's the complete solution for reference. $compute is just a sample to test if it works while 5 is a sample number of pages that would be entered.
if ($command = mysql_query("SELECT `itemprice` FROM `items` WHERE `itemname` LIKE '" . mysql_escape_string($_POST['itemtype']) . "'"))
{
while($row = mysql_fetch_assoc($command))
{
$compute = $row['itemprice'] * 5;
echo $compute;
}
}
else
{
echo (mysql_error ());
}
It would be something like that indeed.
SELECT itemprice FROM items WHERE itemname = $_POST['itemtype']
assuming that itemprice is the actuial colum of the price. HOwever, doing it like this, makes your code vulnerable to mysql injections. So before you contine, consider reading up a little.
In my PHP application I have products which have images in 3 sizes - big, thumbnails and small thumbnails. Each image is stored in a separate folder:
Images - /gallery
Thumbnails - /gallery/bigthumbs
Small thumbnails - /gallery/thumbs
Each product has its unique ID. The file names of all images of a certain product are the same. The path to the images for the product are being stored in the database with the same ID as the associated product ID.
Each product can have more than 1 image but in the database, where I store the paths and the names of the images for each product, all the images are stored with the same ID, the same as the product ID.
So, if a product with ID 56 has 2 images in the database, those images will be stored like this:
ID->56, image1name, bigthumb1name, thumb1name
ID->56, image2name, bigthumb2name, thumb2name
What I'm trying to do is - delete all the images associated with the product which is being deleted. The code I've written is as follows:
$imagename_query = mysql_query("SELECT image FROM gallery WHERE id='$productid'", $connect);
$imagename_result = mysql_fetch_array($imagename_query);
foreach($imagename_result as $imagename) {
$bigimage = "../gallery/$imagename";
unlink($bigimage);
$picture = "../gallery/bigthumbs/$imagename";
unlink($picture);
$thumb = "../gallery/thumbs/$imagename";
unlink($thumb);
}
$gallery_query = mysql_query("DELETE FROM gallery WHERE id='$productid'", $connect);
$query = mysql_query("DELETE FROM products WHERE ID='$productid'", $connect);
The problem is that the code above deletes only 1 image - no difference how many images there are with the same ID.
Could anybody help me figure this out?
Thanks in advance.
this line is the problem
$imagename_result = mysql_fetch_array($imagename_query);
should be
while ($row = mysql_fetch_array($imagename_query))
{
$bigimage = "../gallery/{$row['image']}";
unlink($bigimage);
// etc
}
I not sure is it truth for your case -- do you have multiple products with same name?
your approach could be a bit risky ...
you can make the folder path unique by embed the product id
such as
/gallery/{$product_id}/{$product_name}.*
since you have more images you shoud use:
while ($current_imagename_result = mysql_fetch_array($imagename_query))
instead of
$imagename_result = mysql_fetch_array($imagename_query);
and after you will have to use $current_imagename_result instead of yor varable
Thanks everybody for help. I've managed to solve this in the following way:
$imagename_query = mysql_query("SELECT image FROM gallery WHERE id='$productid'", $connect);
$rows_count = mysql_num_rows($imagename_query);
for($i=0; $i<$rows_count; $i++){
$id = mysql_result($imagename_query, $i, 'image');
$bigimage = "../gallery/$id";
unlink($bigimage);
$picture = "../gallery/bigthumbs/$id";
unlink($picture);
$thumb = "../gallery/thumbs/$id";
unlink($thumb);
}
Hope this will help somebody else.
I want insert some articles. I want make a judge first, if the image value has already in the database, insert a null value.
For more explain:
If there have these data:
title1,image1
title2,image2 //this is image2 first appear, so insert it
title3,image2 //image2 has already in the database, so insert an empty value for just image field
title4
title5,image3
the final data insert into the database should like this:
title | image
title1,image1
title2,image2
title3
title4
title5,image3
Here is my code, but it still insert the repeat value into the database. So is there any one can help me? thanks.
foreach{//first here has a foreach to make all the articles as a queue
...
if(!empty($image)){ //first judge if the insert article had a image value
$query1 = mysql_query("select * from articles where .image = '".$image."' ");
while ($row = mysql_fetch_array($query1)) {
$image1 = $row['image'];
}
}
if(!empty($image1)){ //make a query first if the image has already in the database
mysql_query("INSERT INTO articles (title,image) SELECT '".$title."','' ");
}else{
mysql_query("INSERT INTO articles (title,image) SELECT '".$title."','".$image."' ");;
}
}
Your first query contains an error. You have to remove the dot before "image":
where **.**image
As Francesco mentioned that dot could be a problem for that query.
Not really clear what you are trying to do here. But if you are trying to stop same image file being stored twice then you should use something like this below:
function is_same_file( $image1, $image2 ){
if( filesize($image1) != filesize($image2)
return false;
if( hash_file('md5', $image1) != hash_file('md5', $image2) )
return false;
return true;
}