1

I have a page with a dynamic table, filled with data from mysql database. The table isn't just an ordinary table, one column contains a checkbox with a value of the id (primary key) of the database. But the data from mysql that i want to retrieve has spaces in between them.

Example:

id|name
1 |Chuck Norris
2 |Bruce Lee
3 |Jackie Chan

From the column name, I was able to get Chuck, Bruce and Jackie, but not Norris, Lee, and Chan.

i have to pages namely: delete.php and checkIt.php

a portion of delete.php contains `

    $query = "SELECT * FROM account";
    $result = mysql_query($query) or die ("Query failed");
    $num = mysql_num_rows($result);

    echo "<form method='post' action='checkIt.php'><table>";
    if($num>0)
    {

        echo '<tr>
                <td></td>
                <td>ID</td>
                <td>NAME</td>';
        echo '</tr>';

        while($row = mysql_fetch_array($result))
        {   
            echo "<tr>
                    <td><input type='checkbox' name='names[]' value=".$row['name']."/></td>
                    <td>".$row['id']."</td>
                    <td>".$row['name']."</td>";
            echo "</tr>";
        }
    }
    echo "</table><br><input type='submit' value='Delete' /></form>";
?>`

and a portion of checkIt.php contains `

for ($i=0; $i<count($_POST['names']); $i++)
{ 
    $name = $_POST['names'][$i]; 
    echo " ".$name;
    $sql="DELETE from account WHERE name='$name';";
    $result=mysql_query($sql);
}
?>`

$name from checkIt.php only gets one word. I would like to get the complete data from the mysql so that the DELETE will execute properly. Any suggestions? Thanks in advance!

5
  • 1
    a table can't be ofcourse ordinary, if it has Chuck Norris and other great beings!! Commented Aug 6, 2012 at 17:11
  • Check the data that gets to checkIt.php (echo it!). Maybe you need to treat the data you are sending (encode it, decode it or something). Also, couldn't it be the ID the value you set in the option's value on delete.php? Commented Aug 6, 2012 at 17:12
  • well, this isn't the actual project i'm working on, but the logic is same with this one. Let's just say my primary key in mysql is a varchar and has spaces in between. I need to get the whole data including the spaces. What I got is just the first word Commented Aug 6, 2012 at 17:15
  • Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Aug 6, 2012 at 17:17
  • 2
    Also, as a side note, Chuck Norris cannot be contained in a table. Tables are contained within Chuck Norris. Commented Aug 6, 2012 at 17:17

1 Answer 1

4

You need to enclose the value in your checkbox input in quotes.

Right now it comes out as:

<input type='checkbox' name='names[]' value=chuck norris/>

Changing your echo to:

echo "<tr>
          <td><input type='checkbox' name='names[]' value='".$row['name']."' /></td>
          <td>".$row['id']."</td>
          <td>".$row['name']."</td>";

should solve the problem.

Also, be sure to escape the $_POST parameters you are using in your query or else you are vulnerable to SQL Injection.

Sign up to request clarification or add additional context in comments.

2 Comments

@joannamañez: If this answer solved your problem, consider upvoting and accepting it, by clicking on the large green tick mark under the answer's score.
Something so simple can cause a migraine :) Good find drew010!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.