How to delete particular columns data in database using php mysql [duplicate] - php

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 7 years ago.
Hi iam trying to delete certain columns data from database.Here is my code for delete query.
//getting column id by comparing the id need to delete the data from that row.
$id=$_GET['id'];
$res = "DELETE rental_annual_rent,
rental_block,
rental_street,
rental_area,
rental_town,
rental_state,
rental_pincode
FROM house_details
WHERE house_details_id='$id'";
$result=mysql_query($res);
if(mysql_affected_rows()){
echo "successfully deleted";
session_start();
header("Location:property.php");
}else{
echo "Failure";
}
First iam inserting house_details data into database but i need to delete only particular data from that columns

What you want is an UPDATE, not a DELETE since you want to keep the row, but just clear/blank/unset certain columns within the row.
$res = "UPDATE house_details SET
rental_annual_rent = NULL,
rental_block = NULL,
rental_street = NULL,
rental_area = NULL,
rental_town = NULL,
rental_state = NULL,
rental_pincode = NULL
WHERE house_details_id='$id'";
Note that you should really be sanitizing your id input before using it in the query, you should parameterize it, and you should also migrate from the mysql library to mysqli or PDO

Related

How to get ID of updated row? [duplicate]

This question already has answers here:
How to get ID of the last updated row in MySQL?
(12 answers)
Closed 1 year ago.
I would like to know the ID of the updated row and I tried this:
$sql = $db->prepare("UPDATE `timeslots` SET `service` = ? WHERE `status` = ?");
$sql->bind_param("is", 0, "open");
$sql->execute();
if ($sql->execute()) {
echo "ID: ".$db->insert_id."<br />";
}
But the result is everytime this instead of the ID:
ID: 0
ID: 0
The documentation for insert_id clearly states:
Returns the ID generated by an INSERT or UPDATE query on a table with a column having the AUTO_INCREMENT attribute.
Your query does not generate a new ID. You can't use $db->insert_id as there was no new ID reported by MySQL server.
You can trick MySQL into providing this value. Just reset the ID to the value that it had previously by regenerating it again.
$sql = $db->prepare("UPDATE `timeslots`
SET `service` = ?, Id=LAST_INSERT_ID(Id)
WHERE `status` = ?");
See How to get ID of the last updated row in MySQL?

How to properly retrieve unsigned bigint from MySQL column with PHP? [duplicate]

This question already has answers here:
Display binary(16) column as hex in mysql
(4 answers)
PHP mysql bigint issue
(3 answers)
Closed 2 years ago.
I'm not sure if it's PHP or MySQL that's messing me up.
Say, I have a table with BIGINT UNSIGNED column (let's name it flgs). I read data from that table with PHP as such:
$query = "SELECT * FROM `$tbl_nm` ORDER BY `$colID` ASC";
$res = mysqli_query($link, $query);
if($res)
{
while($aRow = mysqli_fetch_assoc($res))
{
echo("flags=0x".dechex($aRow['flgs'])."<br>");
}
}
if the flgs column has value with bit-63 reset, then I get the correct result. But if bit-63 is set, the return value is 0x7fffffffffffffff. Hmmmm?
For instance, if flgs is set to 0x8000000000000000 in the database, my code above prints:
flags=0x7fffffffffffffff
Why, PHP, why?

How to see if mysql replace or insert my query [duplicate]

This question already has answers here:
Find out if REPLACE statement has replaced or just inserted in MySQL
(3 answers)
Closed 4 years ago.
if I have a query with field 1 being a primary key:
$rep = "Replace into table (field1,field2) values ('value1','value2')";
$stmt = $db->query($rep);
Is there a way to tell if mysql inserted the row, or found and replaced the row?
For Posterity:
$rowCount = $stmt->rowCount();
if $rowCount == 1 it was an insert, if $rowCount == 2, it was a replace.
INSERT INTO AggregatedData (datenum,Timestamp)
VALUES ("734152.979166667","2010-01-14 23:30:00.000")
ON DUPLICATE KEY UPDATE
Timestamp=VALUES(Timestamp)
To achieve this type of task mysql provide us DUPLICATE KEY UPDATE.
Below is the example how you will create new record if record is not exists in database otherwise it will update record
$rep = "INSERT into table (primaryField,field2) values ('value1','value2') ON DUPLICATE KEY UPDATE primaryField=VALUES(primaryField)";
$stmt = $db->query($rep);
For more detail you can read this
https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
I think this will help you.

How can I change this code to INSERT instead of UPDATE if content doesn't exist? [duplicate]

This question already has answers here:
"INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"
(12 answers)
Closed 8 years ago.
I have a query that will update a row in the database, which works fine providing there is a row there to begin with.
How could I say; update if exists insert if doesn't?
require_once('../scripts/includePDO.php');
$who = $_SESSION['who'];
$formText = $_POST['protext'];
$sql = "UPDATE tbl_profiles SET proText = :formText WHERE user_id = :who";
$q = $conn->prepare($sql);
$q->bindValue(':who',$who,PDO::PARAM_INT);
$q->bindValue(':formText',$formText,PDO::PARAM_STR);
$q->execute();
header("Location: ../settings/?status=Done");
Assuming user_id is a unique key in the db:
$sql = "INSERT INTO tbl_profiles (user_id, proText) VALUES (:who, :formText) ON DUPLICATE KEY UPDATE proText = :formText";
Your SQL query should be:
INSERT INTO tbl_profiles (user_id,proText) VALUES (:who,:formText)
ON DUPLICATE KEY UPDATE proText=:formText
This is assuming that user_ID is a unique id
1- simple way is use ORM such as Dotrine
2- How ORM handle this :
usually tables has primary key(id) that should not be null .if you have update then you had select that load this data . in you select load id field in you data structure (array or object or something else) . in save method only check current row you want save that it has id (if this record has id then it exist and need to update else you should save).

How to insert data into the first row of the databse mysql php [duplicate]

This question already has answers here:
Update the first row mysql php
(3 answers)
Closed 9 years ago.
I have a query that should insert data into the first row of data in the database but for some reason it does nothing. The only reason why I can think it doesn't work is because there is nothing in the table. Even so I declare what should happen if NULL.
Here is my code:
foreach ($player_fromsite as $match_player_in_game) {
$querytwo = 'UPDATE `'.$tablename.'` SET `'.$match_player_in_game.'`="'.'yes'.'" WHERE `'.$match_player_in_game.'` IS NULL ORDER BY `'.$match_player_in_game.'` ASC LIMIT 1';
for ($a = 0; $a < 11; $a++) {
if ($match_player_in_game == $home_players[$a]) {
// Insert a row of information into the table "example"
mysql_query($querytwo) or die(mysql_error());
} else {
}
}
}
The UPDATE clause will update any matching records. If there are no records you need to INSERT:
INSERT INTO `table` (aField,otherField) VALUES ("Foo","Bar");
Or to insert more than one record you can use the batch form:
INSERT INTO `table` (aField,otherField) VALUES ("Foo","Bar"),("Second Foo","Second Bar");

Categories