Jump to content

Recommended Posts

i have a script here

for a form.

<?php
require_once('../connect.php');

$query = mysql_query("SELECT * FROM layout") or die(mysql_error());

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
<title>E Layout</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
</head>
<body>
<a href=\"eenglish.htm\">English<a> > <a href=\"eeditlayout.php\">E Layout<a>
<hr>
<form action=\"esubmiteditlayout.php\" method=\"post\">
<table width=\"100%\"  border=\"0\">";
while($row=mysql_fetch_object($query))
{
$id=$row->id;
$name=$row->name;
$source=$row->source;
echo "
  <tr>
    <td valign=\"top\" width=\"10\"><input type=\"checkbox\" name=\"checkbox";
echo $id;
echo "\"></td>
    <td valign=\"top\" width=\"100\">Button Name:</td>
    <td valign=\"top\" width=\"200\"><input type=\"text\" name=\"button[]\" value=\"";
echo $name;
echo "\"/></td>
    <td valign=\"top\" width=\"150\">Button Source:</td>
    <td valign=\"top\" width=\"200\"><input type=\"text\" name=\"buttonsrc[]\" value=\"";
echo $source;
echo "\"/> 
    <td valign=\"top\" align=\"left\">ID:<input type=\"text\" readonly size=\"3\" name=\"id[]\" value=\"";
echo $id;
echo "\"></td>
  </tr>";
}
echo "
</table>
<input type=\"submit\" name=\"edit\" value=\"Edit\">
<input type=\"submit\" name=\"insert\" value=\"Insert\">
<input type=\"submit\" name=\"del\" value=\"Delete\" onclick=\"return deldel()\">
<script language=\"javascript\">
function deldel()
{
   var agree=confirm(\"Are you sure you want to delete the checked buttons from the Side Bar?\");
   if(agree)
      return true;
   else
      return false;
}
</script>
</form>
</body>
</html>";

mysql_close($link);
?>

the form has checkboxes for each row

now how can i make a submit script so that pressing the delete button deletes the row that is checked?

Link to comment
https://forums.phpfreaks.com/topic/59595-solved-deleting-checked-row/
Share on other sites

Do something like this:

<td valign=\"top\" width=\"10\"><input type=\"checkbox\" name=\"checkbox" .$id. "\" value=\"1\" /></td>

 

then on your processor:

$query = mysql_query("SELECT * FROM layout") or die(mysql_error());
while($row=mysql_fetch_object($query))
{
$id=$row->id;
if($_POST['checkbox'.$id] == 1){
//Delete $id from database
}
}

If you name your checkboxes using an array... eg;

 

echo "<td valign=\"top\" width=\"10\"><input type=\"checkbox\" name=\"checkbox[]\" value=\"$id\" /></td>

 

A simple....

 

<?php

  $sql = "DELETE FROM tbl WHERE id IN(" implode(',',$_POST['checkbox']) . ")";

?>

 

In your process page should suffice.

ok when i do what cmgmyr suggested, i just get a blank page with no error but it doesnt work

 

when i do what thorpe suggested, i get a Parse error: parse error, unexpected T_STRING in the following line:

$sql = "DELETE FROM layout WHERE id IN(" implode(',',$_POST['checkbox']) . ")";

can someone help plz

thx

To answer, the question, no, there does not need to be an id='something' statement. We are using a list of possible values, and the IN clause. It is effectively like writing id='something' or id='somethingelse' or id='somethingmore' etc, but much easier to work with.

 

Whenever you have a query that's not working try some debugging:

 

<?php
$sql = "DELETE FROM layout WHERE id IN(".implode(',',$_POST['checkbox']).")";
mysql_query($sql) or die(mysql_error().'<br />query'.$sql);
echo '<br />.$sql.'<br />';
?>

Kind of a double check here, since we dont know if the query is failing or not as yet. If it does, we will get the mysql error and the contents of your query, if it doesn't we will just get exactly whats being passed into the query.

this is the form code

<?php
require_once('../connect.php');

$query = mysql_query("SELECT * FROM layout") or die(mysql_error());

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
<title>E Layout</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
</head>
<body>
<a href=\"eenglish.htm\">English<a> > <a href=\"eeditlayout.php\">E Layout<a>
<hr>
<form action=\"esubmiteditlayout.php\" method=\"post\">
<table width=\"100%\"  border=\"0\">";
while($row=mysql_fetch_object($query))
{
$id=$row->id;
$name=$row->name;
$source=$row->source;
echo "
  <tr>
    <td valign=\"top\" width=\"10\"><input type=\"checkbox\" name=\"checkbox[]\" value=\"";
echo $id;
echo "\"></td>
    <td valign=\"top\" width=\"100\">Button Name:</td>
    <td valign=\"top\" width=\"200\"><input type=\"text\" name=\"button[]\" value=\"";
echo $name;
echo "\"/></td>
    <td valign=\"top\" width=\"150\">Button Source:</td>
    <td valign=\"top\" width=\"200\"><input type=\"text\" name=\"buttonsrc[]\" value=\"";
echo $source;
echo "\"/> 
    <td valign=\"top\" align=\"left\">ID:<input type=\"text\" readonly size=\"3\" name=\"id[]\" value=\"";
echo $id;
echo "\"></td>
  </tr>";
}
echo "
</table>
<input type=\"submit\" name=\"edit\" value=\"Edit\">
<input type=\"submit\" name=\"insert\" value=\"Insert\">
<input type=\"submit\" name=\"del\" value=\"Delete\" onclick=\"return deldel()\">
<script language=\"javascript\">
function deldel()
{
   var agree=confirm(\"Are you sure you want to delete the checked buttons from the Side Bar?\");
   if(agree)
      return true;
   else
      return false;
}
</script>
</form>
</body>
</html>";

mysql_close($link);
?>

and this is the submit code

<?php
require_once('../connect.php');

$edit=$_POST[edit];
$insert=$_POST[insert];
$delete=$_POST[del];

if (isset($delete))
{
$sql = "DELETE FROM layout WHERE id IN(".implode(',',$_POST['checkbox']).")";
}
else if (isset($insert))
{
mysql_query("INSERT INTO layout (id, name, source)
VALUES ('', '', '')");

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
<meta http-equiv=\"Refresh\" content=\"1; url=eeditlayout.php\"/>
</head>
<body>
New Button has been Inserted.<br>
Thank You
</body>
</html>
";
}
else if (isset($edit))
{
$i = 0;
$info = array();
foreach ($_POST['button'] as $namekey => $namevalue) 
  {
   $info[$i++]['name'] = $namevalue;
  
  }
  $i=0;
  foreach ($_POST['buttonsrc'] as $sourcekey => $sourcevalue)
  {
  $info[$i++]['source'] = $sourcevalue;
  
  }
  $i=0;
  foreach ($_POST['id'] as $idkey => $idvalue)
  {
   $info[$i++]['id'] = $idvalue;
  
  }
for($i =0;$i<count($info);$i++)
{
mysql_query("UPDATE layout SET name='".$info[$i]['name']."', source='".$info[$i]['source']."'
WHERE id='".$info[$i]['id']."'");
}
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
<meta http-equiv=\"Refresh\" content=\"3; url=eeditlayout.php\"/>
</head>
<body>
The layout has been updated.<br>
You will be redirected back to the E Layour page in 3 seconds.<br>
Thank You
</body>
</html>
";
}
mysql_close($link)
?>

thx

All these...

 

if (isset($delete))

 

statements will evaluate to true because you set these variables at the top of your page. Not the problem, but something else that will cause issues.

 

 

Thats not actually true is it? Tested with:

 

<?php
$submit = $_POST['submit'];
if(isset($submit)){
echo 'form has been submitted';
}else{
echo 'form has not been submited';
}
?>
<form action="phpfreaks.php" method="post">
<input type="submit" name="submit" />
</form>

 

The first time you load the page, you get the next 'form has not been submitted'. Once you submit, it displays 'form has been submitted'.

i thought just typing it executes it

 

Oh man.. this is something you really need to get your head around. All this...

$sql = "DELETE FROM layout WHERE id IN(".implode(',',$_POST['checkbox']).")";

does is assigns a string to $sql. Exactly the same as...

 

$a = 'this is foo';

 

its that string (an sql statement) that needs to be passed to mysql_query() to actually execute the query..

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.