0

Hey, Been writing my own php snippet but I'm having a few issues. I have a drop down which is populated by the mysql db however when I click delete it runs an action and throws an error.

Form:

<form action="deleteuserform.php">
                <select>

                    <?php 
                    $sql="SELECT id,customer_name FROM Customers";

                    $result =mysql_query($sql);

                    while ($data=mysql_fetch_assoc($result)){

                    ?>

                    <option value ="<?php echo $data['id'] ?>" ><?php echo $data['customer_name'] ?></option>

                    <?php } ?>

                </select>

                    <input type="submit" value="Delete User">
        </form>

deleteuserform.php:

<?php

mysql_query("Delete FROM Customers WHERE id = $data['id']");

?>

Error I am recieving:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/xtrapsp/public_html/Rhino/deleteuserform.php on line 3

4
Contributors
11
Replies
15
Views
4 Years
Discussion Span
Last Post by remunance
Featured Replies
  • 2

    I am not sure whether I understood your question correctly but here we go: Setting an action attribute to another page is not a bad practice and I do not think it causes a lot of workload for the server. But you can have the action attribute set to the … Read More

  • header('location:page.php'); you forget to put semicolon online number 16 and also give method='post' in form tag. Read More

  • and chek your table name in delete query you give Customer and in select query you give Customers both name should be same Read More

0

Enclose $data['id'] in curly braces since it is a complex variable so PHP knows what variable to parse from this string.

mysql_query("Delete FROM Customers WHERE id = {$data['id']}");

See this link.

Edited by broj1

0

Ahh! Silly me! :D

Thanks Broj1

Another question if I may, I have these two pages setup now however they don't seem to work? For example I click delete after selecting them from the drop down. It runs the script (I presume) and then redirects back to deleteuser.php

However the field isn't actually being deleted.

Is it possible to run the function without having to action another php document? As it is causing un-needed work loads.

Thanks

2

I am not sure whether I understood your question correctly but here we go: Setting an action attribute to another page is not a bad practice and I do not think it causes a lot of workload for the server. But you can have the action attribute set to the same page if you wish. In that case you have to check if the form was submitted on the beginning of the page. If yes, then you have to act appropriately (delete the record, redirect or display a message or whatever). But in your current case You have to check whether the query is OK so I suggest you do a little debugging. Change the deleteuserform.php page sligtly like this:

<?php
// this line is only for debugging, you will remove it later
die("Delete FROM Customers WHERE id = {$data['id']}");
mysql_query("Delete FROM Customers WHERE id = {$data['id']}");
?>

Now when you select an option from the drop down you will be redirected to the second page but the query will only get displayed and not executed. You can examine it and copy it to phpmyadmin and test it there.

I also suggest you state the form method explicitly to POST which is more appropriate for deleting.

<form action="deleteuserform.php" method="post">
Votes + Comments
Great Help!
0
<?php 
$host = "localhost";
$username="root";
$password="carina";
$database= "test";

$connection = mysql_connect($host,$username,$password);
$db= mysql_select_db($database);




 ?>

<html>
            <head>
                <title></title>
            </head>
            <body>
                <form method="post" action="deleteuserform.php">
                <select name='id'>
                    <?php 
                    $sql="SELECT id,name FROM customer";
                    $result =mysql_query($sql);
                    while ($data=mysql_fetch_assoc($result)){
                    ?>
                    <option value ="<?php echo $data['id'] ?>" ><?php echo $data['customer_name'] ?></option>
                    <?php } ?>
                </select>
                    <input type="submit" value="Delete User" name='submit'>
        </form>
            </body>
            </html>

deleteuserform.php:

<?php 

    $host = "localhost";
$username="root";
$password="carina";
$database= "test";

$connection = mysql_connect($host,$username,$password);
$db= mysql_select_db($database);
$name="";
        if(isset($_POST['submit']))
        {

            $id=$_REQUEST['id'];
            $sql=mysql_query("Delete FROM customer WHERE id = '$id'");
            if($sql)
            {
            echo "<script>alert('data deleted')</script>";
            header('location:page.php')
        }

    }

i try this and run perfectly,you have to change database name with your database name and table name with your tablne name remainning things are same adn i hope this thing exactaly what you want run the code with required changes

0

I don't think I'm doing this right, I tried both methods and still fail to delete a row from the mysql table...

@broj1

When I click delete this comes up:

Delete FROM Customers WHERE id =

I can only assume it's not taking data from the drop down?

@arti,

I tried your method and had a few problems with the script snippet you sent?

The page which has the drop down:

        <form action="deleteuserform.php">
                    <select>
                        <?php 
                        $sql="SELECT id,customer_name FROM Customers";
                        $result =mysql_query($sql);
                        while ($data=mysql_fetch_assoc($result)){
                        ?>
                        <option value ="<?php echo $data['id'] ?>" ><?php echo $data['customer_name'] ?></option>
                        <?php } ?>
                    </select>
                        <input type="submit" value="Delete User">
            </form>

The action it runs:

<?php 
$host = "localhost";
$username="xtrapsp_custo";
$password="REMOVED";
$database= "xtrapsp_Rhino";
$connection = mysql_connect($host,$username,$password);
$db= mysql_select_db($database);
$name="";
        if(isset($_POST['submit']))
        {
            $id=$_REQUEST['id'];
            $sql=mysql_query("Delete FROM Customer WHERE id = '$id'");
            if($sql)
            {
            echo "<script>alert('data deleted')</script>";
            header('location:page.php')
        }  << LINE 17
    }
?>

The error:

Parse error: syntax error, unexpected '}' in /home/xtrapsp/public_html/Rhino/deleteuserform.php on line 17

I marked line 17 above.

Edited by bradly.spicer

1
header('location:page.php');

you forget to put semicolon online number 16 and also give method='post' in form tag.

Edited by AARTI SHRIVAS

Votes + Comments
Very useful!
1

and chek your table name in delete query you give Customer and in select query you give Customers both name should be same

0

I HAVE IT :D

Thanks very much, it was an error on my behalf. 10am my brain is just dead haha.

Thanks very much to both of you that helped explain it to me and put up with me :)

This is why I love Daniweb

Edited by bradly.spicer

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.