In my AJAX-page I want to fetch data from a database. In my first page I have del image to delete a record. I passed the file cid as 'rmvfile' to the next page and fetched and displayed them on the 2nd page.
In my first page I pass cf_id:
<img src="image/delete1.png" alt="delete" style="width:10px;height:10px" title="Remove" onclick="myFunction('.$fet['cf_id'].');">
function myFunction(cid) {
var rmvfile=cid;
if (confirm("Are you sure you want to Delete the file?") == true) {
if(cid!='') {
$.ajax({
type: 'post',
url: 'delete_cli_file.php',
data: {rmvfile: rmvfile},
});
}
}
}
I my 2nd page I use:
<?php
include "config.php";
$s = $_POST['rmvfile'];
$sel = "select cf_id from client_file where cf_id='".$_POST['rmvfile']."'";
$sel1 = mysql_query($sel);
$sfet = mysql_fetch_assoc($sel1);
$file_name = $sfet['file_name']; //not fetched
$echeck = "delete from client_file where cf_id='".$_POST['rmvfile']."'";
$echk = mysql_query($echeck);
$del = "delete from client_file where file_name = '$file_name' ";
$del1 = mysql_query($del);
?>
$echeck = "delete from client_file where cf_id = '".$_POST['rmvfile']."'";
and $sel="select cf_id from client_file where cf_id='".$_POST['rmvfile']."'"; are working.
My problem is that the value is not fetched in $sfet['file_name'].
try
$del1 = mysql_query( $del ).mysql_error();
You are not fetching file_name from mysql query
$sel = "select cf_id from client_file where cf_id='".$_POST['rmvfile']."'";
Change query to fetch file_name
$sel = "select cf_id,file_name from client_file where cf_id='".$_POST['rmvfile']."'";
Please correct the sql query section in your code
currently it is
$sel = "select cf_id from client_file where cf_id='".$_POST['rmvfile']."'";
it should be
$sel = "select * from client_file where cf_id='".$_POST['rmvfile']."'";
As now it is fetching only cf_id, you didn't get the other fields.
In your 2nd page
if($sel1=mysql_query($sel)) //Wrong verification
Use == rather than =
if($sel1==mysql_query($sel))
Change your sql query to this
$myvar= $_POST['rmvfile'];
$sel="select cf_id from client_file where cf_id='$myvar'";
$sel1=mysql_query($sel);
$sfet=mysql_fetch_assoc($sel1);
$file_name=$sfet['file_name'];
You have not closed your image-tag properly and the quotes are not closed as well
<img src="image/delete1.png" alt="delete" style="width:10px;height:10px"
title="Remove" onclick="myFunction('.$fet['cf_id'].');">
And as stated by manikiran, check the if.condition.
Related
I cant figure out how to get multiple entries of data from my database. You can see below that I have newsID = 1, which allows me to get that to show on my database, but how should I change the code to be able to access other entries like newsID = 2 as well?
<?php
$queryAffinity = "SELECT * FROM affnews WHERE newsID = 1";
$stmt = $pdo->query($queryAffinity);
$row = $stmt->fetchObject();
?>
<?php if($row->newsID == 1) echo "<p>{$row->newsDescription}</p>"; ?>
this is the code im trying to use to echo the id 1
Edit your WHERE-Clause and put it in a loop. For example:
$queryAffinity = "SELECT * FROM affnews WHERE newsID = 1 OR newsID = 2";
$stmt = $pdo->query($queryAffinity);
while($row = $stmt->fetchObject())
{
//do something with $row
}
You Can use the following code:
$queryAffinity = "SELECT * FROM affnews WHERE newsID IN (1, 2)";
$stmt = $pdo->query($queryAffinity);
while($row = $stmt->fetchObject())
{
echo $row->id;
}
I'm just trying to delete a record by its row id from a table using php and ajax but when I click on the button it neither shows error nor performs the action.
here is my delete.php code:
<?php
session_start();
include 'db.php';
if(isset($_GET['name'])){
$user = $_GET['name'];
$query = mysqli_query($con, "SELECT * FROM login WHERE username='$user'") or die (mysqli_error());
$result = mysqli_num_rows($query);
while($row = mysqli_fetch_array($query)){
$_SESSION['user'] = $row['username'];
}
}
$query = mysqli_query($con, "SELECT * FROM pm WHERE touser = '".$_SESSION['user']."' ORDER BY pmdate DESC" ) or die (mysqli_error());
$result = mysqli_num_rows($query);
while($row = mysqli_fetch_array($query)){
$id = $row['id'];
$delquery = mysqli_query($con, "DELETE * FROM pm WHERE id ='$id'") or die (mysqli_error());
$delresult = mysqli_num_rows($delquery);
}
?>
and here is the ajax and delete code of html
<script>
$("#delete").click(function() {
$.ajax({
type: "POST",
url: "delete.php",
success: function(response){
$("#messages").html(response);
}
});
});
</script>
<div id="messages"></div>
any one have any ideas?
Change you query
-> your "DELETE * FROM pm WHERE id ='$id'"
-> correct "DELETE FROM pm WHERE id ='$id'"
You send POST method true AJAX and in php code you write GET, change it.
you set -> type: "POST" and use a GET method in your link
you then use -> $user = $_GET['name'];
+ did you try to echo GET / POST vars before you query and echo your query ?
+ I guess you send nothing to your page...
+ don't use * in DELETE query
You have few issues in your code.
1. you are not sending any parameters to the delete.php page. Please send the record ID to the delete.php page
2. remove * from DELETE query
3. you are adding PHP variables in HTML anchor tag without using PHP tags. you need to correct the link
4. you need to add event.preventDefault() inside your function
In the below script I want to fetch data from mysql using a explode function and also a variable within an explode function.
Here's how I want to get
<?php
include ('config.php');
$track = "1,2,3";
$i = 1
$trackcount = explode(",",$track);
$sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";
$retval = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "{$row['track_name']}";
}
mysql_free_result($retval);
?>
This is the code
$sql = "SELECT * FROM tracks WHERE id='$trackcount[$i]'";
I want sql to fetch data from tracks table where id = $trackcount[$i]
Whatever the value of $trackcount[$i] mysql should fetch but it shows a blank screen.
If I put this
$sql = "SELECT * FROM tracks WHERE id='$trackcount[1]'";
It works perfectly
save your $trackcount[$i] in one variable and then pass it in the query as given below
<?php
include ('config.php');
$track = "1,2,3";
$i = 1;
$trackcount = explode(",",$track);
$id=$trackcount[$i];
$sql = "SELECT * FROM tracks WHERE id='$id'";
$retval = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "{$row['track_name']}";
}
mysql_free_result($retval);
?>
and one more thing check your previous code with echo of your query and see what is passing ok.
echo $sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";//like this
problem is with your query
$sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";//change
to
$sql = "SELECT * FROM tracks WHERE id='$trackcount[$i]'";
Generally you would want to use the IN operator with this type of query, so for you this would be:-
$sql="SELECT * FROM `tracks` WHERE `id` in (".$track.");";
or, if the $ids are in an array,
$sql="SELECT * FROM `tracks` WHERE `id` in (".implode( ',', $array ) .");";
Continuing with my simple CRUD, I'm stuck again...
So I have a table created called "usuaris" and a column called "id" which is my auto-increment and then another column called "usuari_nom". Now, I want to add "delete function", so when I am displaying the records of my table I've added a to delete it:
<div id="main">
<?php
global $conn;
$query = "SELECT * FROM usuaris";
if($grup_usuaris = mysqli_query($conn, $query)) {
echo "<table>";
echo "<tr><th>Usuaris</th><th>Accions</th></tr>";
while($row = mysqli_fetch_assoc($grup_usuaris)) {
echo "<tr><td>" . $row['usuari_nom'] . "</td><td>Eliminar usuari</td></tr>";
}
echo "</table>";
echo "+ Afegeix Usuari";
mysqli_free_result($grup_usuaris);
} else {
echo "query failed";
echo("Error description: " . mysqli_error($conn));
}
?>
</div>
So now, If I click on "eliminar usuari" it goes to the file where I am adding the query to delete, plus the id of that user; for example: "http://localhost/calendario/elimina_usuari.php?subject=6". But then, in the file elimina_usuari.php, how do I select the id to know what record to delete?
I've thought with $_GET but it doesn't seems to work, either with $_POST:
elimina_usuari.php
<?php
global $conn;
$usuari_id = $_GET['id'];
$query = "DELETE FROM subjects WHERE id = {$usuari_id} LIMIT 1";
$result = mysqli_query($conn, $query);
if ($result && mysqli_affected_rows($conn) == 1) {
redirect_to("calendari.php");
} else {
echo "no eliminat";
}
?>
Any clue how can I get its id? Should I take it from the url somehow?
Thanks
you're doing fine.
just need to change this
$usuari_id = $_GET['id'];
to
$usuari_id = $_GET['subject'];
as you're setting subject instead of id in your url
http://localhost/calendario/elimina_usuari.php?subject=6
^
and if you want to process id, like $_GET['id'], you need to change URL.
"http://localhost/calendario/elimina_usuari.php?id=6"
^ change here
EDIT
as per your comment,
you can use any $variable to $_POST or $_GET, it has nothing to do with the database column name.
Like you can use following.
"http://localhost/calendario/elimina_usuari.php?eve_mf=6"
and on elimina_usuari.php page,
$id = $_GET['eve_mf'];
and second part, why can I do that and I don't need to call it id as it is called in my db table?
Again, it's not the issue what you call variables in you local environment, all you to do(and should take care of) is to put right parameters in your sql query.
$query = "DELETE FROM subjects WHERE id = {$usuari_id} LIMIT 1";
Here id is the name of your column name in your database. You can't change it here if you even want it to.
however, $usuari_id is your local variable, and you can change it whatever you want.
Hope I've explained what you're looking for :)
You can get the id with $_GET['subject'].
Please be aware about SQL injection as you are wrongly get the id of the user to be deleted:
$usuari_id = mysqli_real_escape_string($conn, $_GET['subject']);
<?php
global $conn;
$usuari_id = $_GET['subject'];
$query = "DELETE FROM subjects WHERE id = {$usuari_id} LIMIT 1";
$result = mysqli_query($conn, $query);
if ($result && mysqli_affected_rows($conn) == 1) {
redirect_to("calendari.php");
} else {
echo "no eliminat";
}
?>
You just need to Get the exact variable name or parameter name which you have sent with your url
I mean see your url contains subject=6
that means you have to get subject instead of id;
please replace this code
$usuari_id = $_GET['id'];
to
$usuari_id = $_GET['subject'];
try this in elimina_usurai.php
<?php
global $conn;
$usuari_id = $_GET['subject'];
$query = "DELETE FROM subjects WHERE id = {$usuari_id} LIMIT 1";
$result = mysqli_query($conn, $query);
if ($result && mysqli_affected_rows($conn) == 1) {
redirect_to("calendari.php");
} else {
echo "no eliminat";
}
?>
I'm trying to set up a simple comment system and I want to create the correlation between the comment and the page landed.... so when a user arrives at blog.php?id=3 they would be presented the correct comments.
What I'm doing is creating the comments table with a pageid column. The pageid column will be filled when a user posts to the page. Maybe a hidden form field? How do I make this correlation within my MYSQLI
This is what I was thinking...
<?php
include_once("includes/check_login_status.php");
?>
<?php
// Check to see the URL variable is set and that it exists in the database
if (isset($_GET['id'])) {
// Connect to the MySQL database
include "includes/db_conx.php";
$id = preg_replace('#[^0-9]#i', '', $_GET['id']); // filter everything but numbers
// Use this var to check to see if this ID exists, if yes then get the product
// details, if no then exit this script and give message why
$sql = "UPDATE content SET views=views+1 WHERE ID=$id";
$update = mysqli_query($db_conx,$sql);
$sql = "SELECT * FROM content WHERE id=$id LIMIT 1";
$result = mysqli_query($db_conx,$sql);
$productCount = mysqli_num_rows($result);
if ($productCount > 0) {
// get all the product details
while($row = mysqli_fetch_array($result)){
$article_title = $row["article_title"];
$category = $row["category"];
$readmore = $row["readmore"];
$author = $row["author"];
$date_added = $row["date_added"];
$article_content = $row["content"];
}
} else {
echo "That item does not exist.";
exit();
}
} else {
echo "Data to render this page is missing.";
exit();
}
?>
<?php
include_once "includes/db_conx.php";
$sql = "SELECT * FROM comment WHERE pageid ="$id"ORDER BY id DESC";
$sql_comments = mysqli_query($db_conx,$sql);
while($row = mysqli_fetch_array($sql_comments)){
$name = $row["name"];
$comment = $row["comment"];
$commentlist .= 'name : '.$name.'<br />comment : '.$comment.'<hr>';
}
//////////////
?>
Is the lower half in scope of the get variable? So that I can determine what page we're on? Can this type of variable be passed thorugh a variable in the comment form?
The 3rd sql statement contains an error:
$sql = "SELECT * FROM comment WHERE pageid ="$id"ORDER BY id DESC";
to
$sql = "SELECT * FROM comment WHERE pageid =".$id."ORDER BY id DESC";
You might also want to change the pre_replace statement to intval:
$id = preg_replace('#[^0-9]#i', '', $_GET['id']);
to
$id = intval($_GET['id']);
The reason being if $_GET['id'] = 'ABC123' then preg_replace will return 123 whereas the intval will return 0.