I have a mySQL table called "clients" with this structure:
Name | Phone | ID
Now I am using $_GET to extract an ID from a URL, then I would like to delete the entire row (name, phone and ID) from the database that has that ID.
How woudl I do this last part (selecting the row and removing it)? I've tried some stuff but I am starting with php and mysql and it's kind of confusing.
Thank you for your help.
I hope this could help you out to understand better :)
<?php
$Action=$_REQUEST['a'];
$ID=$_REQUEST['id'];//i assume your ID's are numeric so to avoid injections check for numeric
if(is_numeric($ID)){
if($Action=="del" and $ID){
mysql_query("DELETE FROM clients WHERE id=".$ID);
}
$q=mysql_query("SELECT * FROM clients WHERE id=".$ID);
while($qd = mysql_fetch_array($q)){
echo "Name: ".$qd['name']."<br>Phone: ".$qd['phone']."<br><a href='?a=del&id=".$qd['id']."'>Delete</a><br><br>";
}
}
?>
Related
Just a little assistance, This is a pretty simple problem but it doesn't seem to work right. I am just comparing the value in a variable with all the values in a sql column. Same as if I were to compare a username input to the list of usernames in a sql column. This however is just to compare that the item id being stored in the column for that row is not an item id that is already in use.
I tested the value that I am getting back from the sql query and it is equal to the item id I typed in the input. What you will see below is the actual test to see if the id I am getting back is the one that I am looking for as well as the id of the row I can find that value in. The results I get is
2, 000002 (which is correct) that is what I am looking for.
$itemId = $_POST['itemId'];
if($sqlItemId = $dbCon->query("SELECT * FROM CVCinStoreCoins WHERE itemId = '$itemId'")){
while($data = $sqlItemId->fetch_assoc()){
printf("<p>%s, %s</p>", $data['id'], $data['itemId']);
die();
}
Then I took this out and tried to compare the value in the variable which is the same itemId already stored (000002). that is where I am going wrong.
I modified the code to look like this for further testing. Seems straight forward yet i am getting a FALSE response providing the latter echo statement "Item Id is not in use" But it is in the DB. I tried it a few different ways based on what I read in stackoverflow but none are giving me the right answer.
$sqlItemId = $dbCon->query("SELECT * FROM CVCinStoreCoins WHERE itemId = '$itemId'");
if($itemId == $sqlItemId){
echo "This item id is already in use. \n";
die();
} else {
echo "Item Id is not in use:";
die();
}
At one point I even tried a while statement to fetch the associated values prior to testing it but that didn't turn up a positive result either. Any suggestions?
Inside $sqlItemId you have the full table row (if any), not only its ID; change the SQL into a count and check the number of rows returned (if greater than 0 you have a duplicate):
$rowsCount = $dbCon->query("
SELECT COUNT(*)
FROM CVCinStoreCoins
WHERE itemId = '$itemId'
");
I don't know what $dbCon is (Doctrine DBAL? mysqli?) so I can't tell you how to use query's result.
Wy don't you just count it,
$result = $dbCon->query("SELECT COUNT(itemId) FROM CVCinStoreCoins WHERE itemId = $itemId");
if $result > 0
Im working on a web application where i need to let users update several tds in a tr of a table. As of now i can fetch and show the database values in the respective td's. But when i try to update them, the last entered values in the last tr gets updated for all the td's in the table.
Below is the part of the query im trying. All the values are considered array since values are gathered from several input box.
$cno="123";
$c = count($ndv);
for($i=0;$i<$c;$i++)
{
$query_dv="UPDATE device SET cid = '$cno',name = '$ndv[$i]',type = '$tdv[$i]',serialno = '$sdv[$i]',model = '$mdv[$i]',location = '$ldv[$i]' WHERE cid = ".$cno;
$sql_device = mysqli_query($conn, $query_dv) or die(mysqli_error($conn));
}
so say right now i try to update with values 123,1,1,1,1,1 and 123,2,2,2,2,2
i get 123,2,2,2,2,2 and 123,2,2,2,2,2 .. i do understand that its getting reupdated due to the for loop. So im trying to fix this part of the code. And im struggling to fix it. Any help would be appreciated.
Your issue is you are not uniquely identifying the row you are wanting to update, todo this add a column to your database called something like id make it the primary key and have it auto increment, then in your table add a hidden/disabled input containing the id and update your query to be something along the lines of
$query_dv="UPDATE device SET name = '$ndv[$i]',type = '$tdv[$i]',serialno = '$sdv[$i]',model = '$mdv[$i]',location = '$ldv[$i]' WHERE id = " . $id[$i];
I removed the update for cid because from your question it doesn't look like it changes so no need to update.
im using codeigniter and mysql as db. i know might can be solved with mysql query, but if anyone can solve with codeigniter query it will be awesome, otherwise normal PHP mysql query can work also.
I have Table with 3 columns, Well it have many feilds but i have shown here 4 enteries, i didnt wanted to make a table with many columns but only 1 row of data. so instead i went for this style of table as someone suggested me on this website.
Problem is i never worked with this kind of table.
SettingsID SettingsKey SettingsValue
----------------------------------------------------------------------
1 | facebookLink | facebook.com
2 | twitterLink | twitter.com
3 | youtubeLink | youtube.com
4 | googlePlusLink | googleplus.com
Now i want to run a query that should return me rows in to columns. i searched over net and found some solutions but i am not good with this new queries.
I suppose this guy had same kind of problem like i have but in his case he has same value repeated in column, where my SettingsKey has all the values unique.
Here is the link to similar kind of question :
mysql select dynamic row values as column names, another column as value
i cant understand his query and that query i am not sure if is any use of me.
Please can anyone help me build a query to return row values of SettingsKey as columns.
This should work:
<?php
$db = new PDO('mysql:host=localhost;dbname=DBNAME;charset=utf8', USERNAME, PASSWORD);
$stmt = $db->query("SELECT SettingsKey, SettingsValue FROM Settings");
$links = array();
while($row = $stmt->fetch()) {
$links[$row['SettingsKey']] = $row['SettingsValue'];
}
$db = null;
This code queries this table and for each row the SettingsKey and the SettingsValue will be shown.
Now you can get the facebookLink like this:
echo $links['facebookLink'];
Hope this helps.
$sql = "
SELECT SettingsKey, SettingsValue
FROM Settings
";
$q = $this->db->query($sql);
return $q->result_array();
Go to your controller
$data['settings'] = $this->model_selection->your_function();
$this->load->view('example', $data);
And lasty on your view
<?php if(!empty($settings)): ?>
<?php foreach($settings as $k => $v): ?>
//print your staff in here mix with html and go crazy
<?php endforeach; ?>
<?php endif ?>
Hi everyone I have a little problem with this one to many statment. I am adding images in the mysql database and in a folder that part is perfect. But when I want to display them that is my tricky part. I don't know where is my problem in the way I've created the database or in my code. So here are both:
My two tables are text and imagess
In the text table:
text_id - primary
text_field - unique
In the imagess table:
id - primary
name
title
path
My code is this:
<?php
$result = mysql_query("SELECT * FROM text text_id, imagess id WHERE (text_id=id AND text_field=title) ");
while($row = mysql_fetch_assoc($result)){
echo '<br>№'.$row['text_id'].'<br>';
echo''.$row['title'].'<br>'
.$row['text_field'];
echo'<br><img src="'.$row['path'].'?url='.$row['path'].'/>';
}
?>
My goal is to learn how to display the 1 title + text with 2-3 images. I hope someone could explain me where are my mistakes because from the book I am reading there is not such thing.
Thank you in advance.
In a 1-to-many relation, all you have to ignore multiples occurences of the "1".
You also have to join your imagess table on a foreign key.
There are errors in your query, "FROM text text_id" means you create an alias on text named text_id.
<?php
$currentTextId = -1;
$result = mysql_query("SELECT * FROM text, imagess WHERE (text.text_id=imagess.text_id) ORDER BY text.text_id");
while($row = mysql_fetch_assoc($result)){
if($row['text_id']!=$currentTextId){
$currentTextId=$row['text_id']
echo '<br>№'.$row['text_id'].'<br>';
}
echo''.$row['title'].'<br>'.$row['text_field'];
echo'<br><img src="'.$row['path'].'?url='.$row['path'].'/>';
}
?>
I'm trying to write my first PHP script with mySQL and I desperately need some help. I'm sure this is relatively simple, but if I have one field in my table (username, for example), and I want to fetch another field (name, for example), that is in the same row as the given username, how do I do that?
Again, I'm sure this is easy, but I'm lost, so I'd really appreciate any help. Thanks!
$sql = "SELECT username, name FROM table";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
echo "This {$row['username']} has the name {$row['name']}\n";
}
halfdan's answer sort of works, but it fetches all rows and displays them. What you want is a WHERE clause, which lets you filter the contents of the table so the query only returns the row(s) you want:
SELECT username, name
FROM sometable
WHERE (username = 'johndoe');
This will return only the rows where the username field is equal to 'johndoe'. Conceptually, it's equivalent to:
$results = mysql_query("SELECT username, name FROM table");
while($row = mysql_fetch_assoc($results)) {
if ($row['username'] == 'johndoe') {
// do something, this is a row you want
} else {
// not a row you want. ignore it, or deal with it some other way
}
}
the main difference is that for large data sets in the database, doing client-side filtering like this is expensive, as the entire contents of the table has to be transferred over. Using a WHERE clause to limit things to just what you want is far more efficient in the long run.