php-mysql update without deleting data already there - php

I want to update a Comment Blob in a mysql database without deleting data already in it.
$SQLstring="INSERT INTO Participant(Role,Comment)
VALUES('$Costume','$Comment')
WHERE Fname='$FirstName' AND Lname='$LastName'";
This doesn't want to work if there is already data in the "Comment" field and if I use UPDATE it will replace the data.
I want to keep data that is there and simple add to it

You can use this to do it:
$Rolequery = mysql_query("UPDATE `Participant` SET Role=CONCAT(Role, ',$role') WHERE Fname='$FirstName' AND Lname='$LastName'");
$Commentquery = mysql_query("UPDATE `Participant` SET Comment=CONCAT(Comment, ',$Comment') WHERE Fname='$FirstName' AND Lname='$LastName'");

Related

How to update a current row on Parse.com using php

I want to update single row base on id, i have questions table and there is column question_id, which has unique date for each row.
Now i want to update that row column based on that question_id.
I have code written below, but not getting success. It created New Record not update old record. I want to update old record.
I have tried both ParseObject and ParseQuery but not getting success.
$query = new ParseObject("question");
$query->equalTo("question_id", "2");
$query->set("correct_percentage", "55.5");
$query->save();
You should load the object in question first, manipulate the data and then save it. Your current code, as you already noticed, simply creates new objects each time it is run.
// Fetch the question object where question_id == 2
$query = new ParseQuery("question");
$query->equalTo("question_id", "2");
$question = $query->first();
// .. optionally verify that $question has a value ...
// Manipulate the object and save it
$question->set("correct_percentage", "55.5");
$question->save();

How to get the last ID with mysql using php

I have a form with two stages, the first is to put the information but the second is the UPDATE to add the photo. So what I want to do is retrieve the last auto incremented ID to update the data, I used LAST_INSERT_ID ()
but I realize it is the update everywhere. Do you have a suggestion for me to help me? Thank you and sorry sorry for my english.
You can see here what I did:
$sql = 'UPDATE 'jj_news' SET
cover_picture="'.$large_image_name.$_SESSION['user_file_ext'].'",
min_picture="'.$thumb_image_name.$_SESSION['user_file_ext'].'", statut="1",
edited="'.date('Y-m-d h:i:s').'" WHERE id_news= LAST_INSERT_ID(id_news)';
mysql_query($sql) or die('Erreur SQL !'.$sql.'<br />'.mysql_error());
You cannot get your id in the "second stage". You have to get it immediately after running insert.
So, get it in the first stage, store it in a session and then use in the second one.
Bloody hell, why don't you just store the damn id, into a hidden post field and pass it to the second page on form submit?
mysql_query("INSERT INTO `xy` (`xy`) VALUES ('$_POST[xy]');");
$insertId = mysql_insert_id();
mysql_query("UPDATE xy SET xy='$xy' WHERE id='$insertId'");
only know from the mysql_* function...
use mysqli or PDO, but if this helps you i´m glad.....
You can try the following function.
function getLastId($tablename,$id_field){
$id=0;
$sql="select ".$id_field." from ".$tablename." order by ".$id_field." desc limit 1" ;
$res_obj=mysql_query($sql) or die(mysql_error);
if(mysql_num_rows($res_obj)>0){
$result=mysql_fetch_array($res_obj); //we have only on row and column.
$id=$result[$id_field]; //set the last greator Id value;
}
return $id;
}
$newid=getLastId("yourtable","id_field")+1;
//insert your data using $newid instead of autocomplete.
//use $newid during update if update is on new page make a session of $newid. like
$_SESSION['update_id']=$newid;
this function make manual increment of id column, instead of auto increment. I think it is ease to control your situation using this function.

How to delete and update data in MySQL at the same time

Basically what I am trying to do is when I delete a client with an ID of lets say 6 and I have 50 clients, I then want to update the client with the ID of 50 to 6.
This is my code, in PHP, but it won't execute 2 mysql_query-s at the same time at least I think that's the problem. Otherwise the SQL syntax works fine.
public function delete () {
$last=$this->numrow; //contains last ID works fine
if (isset ($_GET['x'])) {
mysql_query('DELETE FROM proba WHERE ID ='.$_GET['x']);
mysql_query('UPDATE proba SET ID='.(int)$_GET['x'].'WHERE ID='.(int)$last);
}
}
The $_GET['x'] contains the ID on which it was clicked . But only the first mysql_query gets executed how do i make it so the second one gets executed also ?
And another question is is it possible to get <a href="munka/index.php?x=5" > [-] </a> the x=5 with a $_POST ?
You might save yourself a lot of trouble by using mysql's replace query:
see http://dev.mysql.com/doc/refman/5.6/en/replace.html
for details.
most probably you are facing a php error on the first query. Check the php error log.
for the second question $_GET is used to take parameters from the URL for example
munka/index.php?x=5
$_POST is used to get parameters posted on http post (usually on form submits).
just change the update query with a space before the where clause
mysql_query('UPDATE proba SET ID='.(int)$_GET['x'].' WHERE ID='.(int)$last);
Better to use transactions support by using InnoDB Mysql DB Engine, so both delete and update execute together wuth COMMIT without miss , and in case anything goes wrong your delete changes get ROLLBACK
if (isset($_GET['x'])) {
mysql_query('DELETE FROM proba WHERE ID =' . $_GET['x']);
mysql_query('ALTER TABLE `proba` DROP `ID`;');
mysql_query('ALTER TABLE `proba` ADD `ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST');
}
Try in Phpmyadmin delete record no of 5 and drop id column and recreate id column.
It is work.

Check for duplicate when editing a record in MySQL

I'm having problems with a piece of code and wonder if someone can help.
I have a form that submits information to a MySQL database, I have the correct code for checking to see if the submitted product code already exists, and if so shows a warning message and the record is not added.
That code is:
$result = mysql_query("SELECT * FROM listing_1 WHERE product_code='$product_code'");
$num_rows = mysql_num_rows($result);
if ($num_rows) {
adminwarnmessage("DUPLICATE REFERENCE CODE","FAILURE - <b>$product_name</b> has <b>NOT</b> been added because the reference number already exists.");
}
That works fine for Data Entry, however I have another form that allows users to edit the record, this is what is causing me a problem, as the above code only tells me that there is already a matching record in the database, Of course when I try to save (update) the record it now tells me I can't because it is a duplicate.
What I would like to happen is that it doesn't allow users to choose another productcode that already exists, but I want them to be able to update the record using the same product code the form fetched from the database.
Hope that makes sense, any help greatly appreciated.
If you have id (primary key) then You will have to compare with id of that product before updating the record. For example
$result = mysql_query("SELECT * FROM listing_1 WHERE product_code='$product_code' AND id!=$id");
$num_rows = mysql_num_rows($result);
if ($num_rows) {
echo "duplicate record";
}
Here $id is the id of the product that you should have while editing the record.
following is the step you need to follow when you managing the Database
First you need an primary key(auto_increment) in "ID" field
When you execute insert query that time first check where record is already available or not. if not available than only you should execute insert query.
use primary key filed for update, delete etc...
if you follow the above step than you never face this problem
Are you perhaps checking for duplicate occurrences in both of insert and update statement? If so, you shouldn't. Duplicate entry is relevant only when "inserting". You shouldn't use the same check for update. Hope that helps.
why the same code for update also? you can use another query for updating which is better for debugging if you have problems later. try this
$result = mysql_query("UPDATE listing_1 SET product_code='$new_product_code' WHERE product_code='$product_code' AND id='$id'");
if($result) {
echo "your product was updated.";
} else {
echo "your product is not in DB";
}
EDIT: be careful in updating or inserting things, take always id to check unless your product_code is unique
EDIT
I have resolved this issue by making $productcode field a unique Index.
Now when editing if there is a duplicate..
Mysql does not accept the update query, it returns an error code
I trap that error code and include it in an if statement...
if( mysql_errno() == '1062' )
adminwarnmessage("DUPLICATE REFERENCE CODE","FAILURE - $product_name has NOT been added because the reference number already exists");
}
adminmessage("Item Updated", Congratulations you updated $productname succesfully");
}
This now allows editing of $productcode but does not allow it to be changed to one already used in the database.
Thank you to everyone who took the time to offer help

MySQL won't update if content is changed

I got a PHP and MySQL problem, when I update the headline I got no problem. But when updating while having changed the content it won't update at all.
Any idea to what's going wrong.
$query = mysql_query("UPDATE about SET headline='$headline' WHERE content='$content'")
or die(mysql_error());
$query = mysql_query("SELECT * FROM about ORDER BY id DESC");
while ($row = mysql_fetch_assoc($query))
{
$id = $row['id'];
$headline = $row['headline'];
$content = $row['content'];
header("location: ../");
}
I'm coding in PHP and using and MySQL server.
I would prefer a solution that only calls the database once when updating.
When using an UPDATE statement, the WHERE clause is used to specify the record(s) to update. In the following case, where you've modified both the headline and content:
UPDATE about SET headline='$headline' WHERE content='$content'
The above query attempts to update the headline field where the content field equals the new content. There won't be any rows where the existing content matches the new content.
You'd have to do something similar to:
UPDATE about SET headline='$headline', content='$new_content' WHERE content='$old_content'
However, you should generally be selecting a record by an identifier, not a content field.
UPDATE about SET headline='$headline', content='$new_content' WHERE id = $id
If you update using a field like content, you may accidentally update all rows that have the same content, instead of just the row that you're trying to update. Using a unique identifier allows you to update only the specified row.
You could store the identifier in the session (on the server side).

Categories