Jump to content

assignment deadline in 24 hrs please help !!!


andrew_biggart

Recommended Posts

can anyone please find a solition to this my assignment deadline is in 24 hours :(

 

<?php

$host="localhost"; // Host name

$username=""; // Mysql username

$password=""; // Mysql password

$db_name="test"; // Database name

$tbl_name="test_mysql"; // Table name

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

$sql="SELECT * FROM $tbl_name";

$result=mysql_query($sql);

 

$count=mysql_num_rows($result);

 

?>

<table width="400" border="0" cellspacing="1" cellpadding="0">

<tr>

<td><form name="form1" method="post" action="">

<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">

<tr>

<td bgcolor="#FFFFFF"> </td>

<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>

</tr>

<tr>

<td align="center" bgcolor="#FFFFFF">#</td>

<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>

<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>

<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>

<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>

</tr>

<?php

while($rows=mysql_fetch_array($result)){

?>

<tr>

<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>

<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>

<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>

</tr>

<?php

}

?>

<tr>

<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>

</tr>

<?

// Check if delete button active, start this

if($delete){

for($i=0;$i<$count;$i++){

$del_id = $checkbox[$i];

$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";

$result = mysql_query($sql);

}

 

// if successful redirect to delete_multiple.php

if($result){

echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";

}

}

mysql_close();

?>

</table>

</form>

</td>

</tr>

</table>

I'm feeling generous (I usually tell them to go study):

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_assoc($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>

Oh, and he wasn't using php outside of php.

After revewing his code further, I notice he doesn't have variables set, so it still won't work.

lol sorry everyone just to clarify i have taken this staright from a tutorial website and changed the database connection to check it worked first so i havnt even written this code !!! blame the author lol

 

can anyone please offer some advice as to what path to follow to do this and i will write my own code !!! POSSIBLY lol

 

i know the delete variable is undefined and i have tried changing it to    if $_POST('delete'); and things like that but ive been staring at php staright for about a week my head is wedged between my arse and i cant make heads or tails of anything anyone that can point me in the right direction it would much appreciated

 

thanks in advance

Unless globals are on, you are going to get error with your code. what you need to do is check to see if the form hase been submitted first then run your code. also there is a better way to loop through your checkboxes.

 

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
$conn = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// check to see if form has been submitted
if(!isset($_POST['delete'])){
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
  <tr>
    <td>
    <form name="form1" method="post" action="">
      <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
        <tr>
          <td bgcolor="#FFFFFF"> </td>
          <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
        </tr>
        <tr>
          <td align="center" bgcolor="#FFFFFF">#</td>
          <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
          <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
          <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
          <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
        </tr>
        <?php
        while($rows=mysql_fetch_array($result)){
        ?>
        <tr>
          <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
          <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
          <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
          <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
          <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
        </tr>
        <?php
        }
        ?>
        <tr>
          <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
        </tr>
      </table>
      </form>
    </td>
  </tr>
</table>
<?php
} else {
$result = 1;
// Check if delete button active, start this
  foreach($_POST['checkbox'] as $id){
    $del_id = $id;
    $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
    $res = mysql_query($sql);
    if(!$res){
    // set result to 0 if failure
    $result = 0;
    }
  }
  // if successful redirect to delete_multiple.php
  if($result){
  echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
  }
}
mysql_close();
?>

aghhhhh this is still not workin :?(

 

when i press delete it returns a blanc screen this is what im using

 

<?php

include("config.php");

if(!isset($_POST['delete'])){

$sql="SELECT * FROM biggart_members";

$result=mysql_query($sql);

 

$count=mysql_num_rows($result);

?><table width="400" border="0" cellspacing="1" cellpadding="0">

<tr>

<td><form name="form1" method="post" action="">

<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">

<tr>

<td bgcolor="#FFFFFF"> </td>

<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>

</tr>

<tr>

<td align="center" bgcolor="#FFFFFF">#</td>

<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>

<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>

<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>

<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>

</tr>

<?php

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

?>

<tr>

<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['user_id']; ?>"></td>

<td bgcolor="#FFFFFF"><?php echo $rows['user_id']; ?></td>

<td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td>

<td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>

<td bgcolor="#FFFFFF"><?php echo $rows['email']; ?></td>

</tr>

<?php

}

?>

<tr>

<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>

</tr>

<?php

} else {

$result = 1;

// Check if delete button active, start this

  foreach($_POST['checkbox'] as $id){

    $del_id = $id;

    $sql = "DELETE FROM biggart_members WHERE id='$del_id'";

    $res = mysql_query($sql);

    if(!$res){

    // set result to 0 if failure

    $result = 0;

    }

  }

  // if successful redirect to delete_multiple.php

  if($result){

  echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";

  }

}

mysql_close();

?></table>

</form>

</td>

</tr>

</table>

</div>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.