I'm new to PHP so this might be something obvious I missed.
Im trying to make a button which increments a value in my database:
<?php
$alist = mysqli_query($conn, "SELECT * FROM `posts` ORDER BY `posts`.`id` DESC");
$results = mysqli_num_rows($alist);
if ($results > 0){
while($row = mysqli_fetch_array($alist)) {
echo $row['uid']. " says: ".$row['postText']." <button onclick=".mysqli_query($conn, "UPDATE `posts` SET `postLikes` = postLikes+1 WHERE uid = ".$row['uid'])." name='likebtn'>👍</button>".$row['postLikes']."<br>";
}
}
?>
The part that makes the button is on line 6
I just want to find how I can use the mysqli_query on button click
by the way, I already tried this: "https://stackoverflow.com/questions/3862462/php-mysql-run-query-on-button-press-click" but with no result
Thanks in advance
if your current page is called first.php,
put inside button
<button></button>
<?php
if ( isset($_GET['p']) && $_GET['p']=="like") {
do your query
}
?>
if you dont want to reload then you need ajax,
hope this was helpful. :)
You have a syntax error here
echo $row['uid']. " says: ".$row['postText']." <button onclick=".mysqli_query($conn, "UPDATE posts SET postLikes = postLikes+1 WHERE uid = ".$row['uid'])." name='likebtn'>👍</button>".$row['postLikes']."<br>";
you can either do this
$q = mysqli_query($conn, "UPDATE posts SET postLikes = postLikes+1 WHERE uid = ".$row['uid']);
echo $row['uid']. " says: ".$row['postText']." <button onclick=".$q." name='likebtn'>👍</button>".$row['postLikes']."<br>";
Related
I got my first theorem working on the web but when I click on one of the buttons I want it shows the next theorem.
PHP
<?php
echo" <button class='true'>true</button><textarea type='text' disabled>";
$query = "SELECT `sText` FROM statement WHERE `sID` ='1'";
if($result = mysqli_query($conn, $query))
{
$row = mysqli_fetch_assoc($result);
echo $row['sText'];
}
echo "</textarea><button class='false'>false</button>";
?>
Try to use a link temporarily. If you want to use button, prolly you need to add a javascript to redirect when you click. So link is easier for now.
// your current id
$id = #$_GET['id'] ?: 1;
// get the next id
$nextIDQuery = "SELECT `sID` FROM statement WHERE `sID` >'".(int)$id."' limit 1";
// then fetch it as $nextID
// then change your current query to
$query = "SELECT `sText` FROM statement WHERE `sID` ='".(int)$id."'";
// your next link will be
echo "<a href='youfile.php?id=$nextID'>Next</a>";
Hope this can help you understand the logic.
Welcome to PHP.
I am trying to post php code from my messages table. But it is not posting. For example:
<textarea name="update">User can write php codes here for sending</textarea>
<div class="send">Send</div>
When i click send button the text code <?php echo $message;?> must be in my messages table row post_text . But post_text is empty, you can see it in the screenshot.
What is the problem here. What i shoud do to fix this problem? Anyone can help me here ?
<?php
include_once '../inc/inc.php';
if(isset($_POST['update'])) {
$update=mysqli_real_escape_string($db,$_POST['update']);
if($update){
$data=$DoDo->InsertText($uid,$update);
echo $update;
}
}
?>
InsertText function
public function InsertText($uid, $update) {
$time=time();
$ip=$_SERVER['REMOTE_ADDR'];
mysqli_query($this->db,"SET character_set_client=utf8") or die(mysqli_error($this->db));
mysqli_query($this->db,"SET character_set_connection=utf8") or die(mysqli_error($this->db));
$query = mysqli_query($this->db,"SELECT post_id,post_text FROM `posts` WHERE uid_fk='$uid' order by post_id desc limit 1") or die(mysqli_error($this->db));
$result = mysqli_fetch_array($query, MYSQLI_ASSOC);
$query = mysqli_query($this->db,"INSERT INTO `posts` (post_text, uid_fk,time) VALUES (N'$update', '$uid','$time')") or die(mysqli_error($this->db));
//The newquery to select for message
$newquery = mysqli_query($this->db,"SELECT M.post_id, M.uid_fk, M.post_text, M.time,U.username FROM posts M, users U where M.uid_fk=U.uid and M.uid_fk='$uid' order by M.post_id desc limit 1 ") or die(mysqli_error($this->db));
$result = mysqli_fetch_array($newquery, MYSQLI_ASSOC);
return $result;
}
Note: This problem will be come when i send just php code. If i send
normal text then normal text sending from post_text.
I want to send php code like this screenshot.
In your update query, you did not check the right variable;
$update=mysqli_real_escape_string($db,$_POST['update']);
if($update){
$data=$DoDo->InsertText($uid,$update);
echo $update;
}
also in your insert query you have a funny character, remove the N
VALUES ('$update', '$uid','$time')"
$query = mysqli_query($this->db,"INSERT INTO `posts` (post_text, uid_fk,`time`) VALUES ('$update', '$uid','$time')") or die(mysqli_error($this->db));
Do this below
<textarea name="update"><?php echo '<?php echo $message;?>';?></textarea>
Instead if($login){ use if($update){
change this
VALUES (N'$update', '$uid','$time')"
to
VALUES ('$update', '$uid','$time')"
wait, you literally want the string '<?php echo $message;?>' to be inserted, not the value of $message ?
if that is the case, you need to either make sure that PHP sees it as a string:
<textarea name="update"><?php echo '<?php echo $message;?>';?></textarea>
... or maybe it would be enough to post only the code, not the php tags
<textarea name="update">echo $message;</textarea>
<?php
include('core/init.php');//database connection
if(isset($_POST['btn_submit'])){
$sqlQuery = mysql_query("UPDATE `position` SET `ATR`='".mysql_real_escape_string($_POST['ATR'])."',
$resultQuery = mysql_query($connection, $sqlQuery) or die (mysql_error($connection));
if(mysql_affected_rows($resultQuery) > 0){
echo "updated";
}else{
echo "failed";
}
header('Location:position2.php');
$result = mysql_query("SELECT * FROM position WHERE ID='" .$_POST["id"]. "'");
$row2 = mysql_fetch_array($result);
}
?>
//What this code does it it updates the database based on user input and I am trying to loop through each of the user input as update and display but it doesn't seem to work
($_POST['ATR'])."',
Is missing a closing "
Also formatting code makes it easier to read and debug.
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 am trying to make a delete button but it doesn't work.
$interogare = "SELECT * FROM comments JOIN users ON users.user_id = comments.user_id WHERE movie_id='$movie_id' ORDER BY date_posted DESC";
$result = mysqli_query($dbc, $interogare) or die(mysqli_error($dbc));
while($rand = mysqli_fetch_assoc($result))
{
echo 'Delete';
if(isset($_GET['com'])) {
$haidi = mysqli_real_escape_string($dbc,$_GET['com']);
$sql_del = "DELETE FROM comments WHERE comment_id = '$haidi'";
mysqli_query($dbc,$sql_del);
header('location: film.php?id='.$_GET['id'].'');
exit();
}
}
When i click the delete link it takes me to
film.php?com='.$rand['comment_id'].'
page but nothing happens,it should delete my comment and take me back to the page where the comment was.Can someone please help me figure this out ?
does this work?
<?php
$query = "
SELECT *
FROM comments
JOIN users ON users.user_id = comments.user_id
WHERE movie_id=" . $movie_id . "
ORDER BY date_posted DESC";
$result = mysqli_query($dbc, $query) or die(mysqli_error($dbc));
if (isset($_GET)) {
$getData = $_GET;
}
while ($rand = mysqli_fetch_assoc($result))
{
echo 'Delete';
if (isset($getData) && $getData['com']) {
$id = mysqli_real_escape_string($dbc, $getData['com']);
$query = "
DELETE FROM comments
WHERE comment_id=" . $id;
$result = mysqli_query($dbc, $query);
if($result)
{
header('location: film.php?id=' . $id . '');
}
exit();
}
}
i would rather grab the post once, and then make sure we are passing the same object around, so i set GET to a var. then, i didnt like the way the ids were being set in the query, because it wasnt as easy to see. then i got down to the way you were creating the header, and it looked like you were passing the id from some GET data, instead of the id you just extracted and set to a varibale, see above..., or was that your intention to use $_GET['id'] for that actually instead?
also, you might need to pass your data link in the mysqli_real_escape_string() method