I have this while-loop code for returning data from database
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
(some not important lines...)
echo "<div><a href='remove.php?id=<?php echo ".$row['id'].";?>'>remove record</a></div>";
and I want to pass id variable by GET to my remove.php
<?php
include '../db.php';
$id = $_GET['id'];
$con = mysqli_connect($host, $user, $pass, $db);
mysqli_query($con, 'DELETE FROM zapisy WHERE id='$id';')
mysqli_close($con);
?>
but everytime I click on my link it redirects to
remove.php?id=<?php%20echo%2042;?> // <?php echo 42;?> without all that mess, id is good
leaving that php tags, that I think should not be here, and not passing my variable to another page - first problem; second - php returns
Parse error: syntax error, unexpected T_VARIABLE in .../admin-panel/remove.php on line 5
which tells me something is wrong with the mysql spelling nearby variable, but I was looking for answer and nothing. I changed ' ' to " " and opposite, and nothing. Or is it because variable is not getting value passed and its empty?
I would be very thankful if someone could help me or point me the right path, because I'm stuck at this point and I am just beginning with coding.
Change your code to :
if(mysqli_num_rows($result) > 0)
{
{
while($row = mysqli_fetch_array($result)){
(some not important lines...)
?>
<div><a href='remove.php?id=<?php echo $row['id'];?>'>remove record</a></div>
<?php }
}
OR
if(mysqli_num_rows($result) > 0)
{
{
while($row = mysqli_fetch_array($result)){
(some not important lines...)
echo "<div><a href='remove.php?id=".$row['id']."'>remove record</a></div>"
You are messing up with HTML and PHP.
Modifications required:
1) You have added echo
twice. It no need as you are already printing the string.
2) Excessive <?php
and ?>
tags.
echo "<div><a href='remove.php?id=".$row['id']."'>remove record</a></div>";
Don't use again <?php
when you are already in PHP echo
function.
Change this:
echo "<div><a href='remove.php?id=<?php echo ".$row['id'].";?>'>remove record</a></div>";
to:
echo "<div><a href='remove.php?id=".$row['id']."'>remove record</a></div>";
And also you are missing 2x .
(because there are single quotes, not double quotes) and ;
, change this:
mysqli_query($con, 'DELETE FROM zapisy WHERE id='$id';')
to:
mysqli_query($con, 'DELETE FROM zapisy WHERE id='.$id.';');
try this
echo "<div><a href='remove.php?id=".$row['id']."'>remove record</a></div>";
Your first error is because you shouldn't open php tags inside already opened php tags.
It should be like this:
echo "<div><a href='remove.php?id=".$row['id']."'>remove record</a></div>";
The second error is because you're missing a ;
in your code and mixing up your quotes. Change your query to this.
mysqli_query($con, "DELETE FROM zapisy WHERE id='$id'");
Side note: Since you're using mysqli, you should be using prepared statements.
Here's a quick example:
$stmt = mysqli_prepare($con, "DELETE FROM zapisy WHERE id=?");
mysqli_stmt_bind_param($stmt, 'i', $id);
mysqli_stmt_execute($stmt);