13 Replies - 354 Views - Last Post: 15 January 2013 - 06:46 PM Rate Topic: -----

#1 Jheroll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-January 13

Problem deleting in table

Posted 15 January 2013 - 04:59 PM

Hi, i have a problem with deleting data from a table.
First problem is that the data doesnt show in the table.
second the delete doesnt work.

here is the code:

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="people"; // Database name 
$tbl_name="usersinfo"; // 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">&nbsp;</td>

</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>username</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>firstname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>MI</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Company</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>position</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>department</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['USERNAME']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['LASTNAME']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['FIRSTNAME']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['MI']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['COMPANY']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['POSITION']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['DEPARTMENT']; ?></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=deletesam.php\">";
}
}
mysql_close();
?>

</table>
</form>
</td>
</tr>
</table>



an error is also shown saying: UNDEFINED VARIABLE ON LINE 66 ..
Please help. Thanks in advance. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Problem deleting in table

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10445
  • View blog
  • Posts: 38,682
  • Joined: 27-December 08

Re: Problem deleting in table

Posted 15 January 2013 - 05:11 PM

The $delete variable wasn't declared anywhere, hence the error. By your comment: Check if delete button active, start this , there seems to be a clear misunderstanding of how PHP works. What happens is the following:
-The client sends a request to the server (HTML is a client-side language)
-The server processes the request (PHP is a server-side tool)
-The server sends the result back to the client
-The client displays the results

So you should always separate your HTML/CSS from your PHP.

Now when your HTML form is submitted, the button is submitted as either a $_GET or $_POST value, depending on the submission method. It is important to note that if the user presses enter rather than submitting the button, the button value may not be sent to the server.

When testing your queries, you should check to see what the mysql_query() function returns. If it returns FALSE, you should print out the result of the mysql_error() call. Also, it is important to note that the mysql_*() family of functions is deprecated and will be removed in future versions of PHP. You should really be using Prepared Statements like PDO or MySQLi.

More reading:
Separating Your HTML and PHP
PDO
Was This Post Helpful? 1
  • +
  • -

#3 Jheroll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-January 13

Re: Problem deleting in table

Posted 15 January 2013 - 05:39 PM

HI, I have a problem with deleting data in table..

main.php(codes)
<?php 

$conn=mysql_connect("localhost","root","");
	if(!$conn)
	{
	die('could not connect!' . mysql_error());
	}

	mysql_select_db("people", $conn);
$result = mysql_query("SELECT * FROM usersinfo"); ?>

<table>
  <tr>
    <td>Recent Posts</td>
  </tr>
  <?php while($row = mysql_fetch_array($result)) : ?>
  <tr>
    <td><?php echo $row['USERNAME']; ?></td>
    <td><?php echo $row['LASTNAME']; ?></td>
	    <td><?php echo $row['FIRSTNAME']; ?></td>
    <td><?php echo $row['MI']; ?></td>
	    <td><?php echo $row['COMPANY']; ?></td>
    <td><?php echo $row['POSITION']; ?></td>
	  <td><?php echo $row['DEPARTMENT']; ?></td>
    <!-- and so on -->
    <td>
      <form action="delete.php" method="post">
        <input type="hidden" name="delete_id" value="<?php echo $row['ID']; ?>" />
        <input type="submit" value="Delete" />
      </form>
    </td>
  </tr>
  <?php endwhile; ?>
</table>



delete.php (codes)

<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id'])) {
  $delete_id = mysql_real_escape_string($_POST['delete_id']);
  mysql_query("DELETE FROM usersinfo WHERE `ID`=".$delete_id);
  header('Location: main.php');
}
?>


when I click the delete button I get: Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')' in C:\wamp\www\delete\delete.php on line 2

does anyone know how to fix this .. help please .. thanks in advance :)
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10445
  • View blog
  • Posts: 38,682
  • Joined: 27-December 08

Re: Problem deleting in table

Posted 15 January 2013 - 05:43 PM

Duplicate threads merged. Please avoid duplicate posting.

See the isset() function call in your if statement isset($_POST['delete_id']. You forgot the end parenthesis.
Was This Post Helpful? 1
  • +
  • -

#5 Jheroll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-January 13

Re: Problem deleting in table

Posted 15 January 2013 - 05:49 PM

View Postmacosxnerd101, on 15 January 2013 - 05:43 PM, said:

Duplicate threads merged. Please avoid duplicate posting.

See the isset() function call in your if statement isset($_POST['delete_id']. You forgot the end parenthesis.


<?php
if (isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
  $delete_id = mysql_real_escape_string($_POST['delete_id']);
  mysql_query("DELETE FROM usersinfo WHERE `ID`=".$delete_id);
  header('Location: main.php');
}
?>


I still get the same error..
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10445
  • View blog
  • Posts: 38,682
  • Joined: 27-December 08

Re: Problem deleting in table

Posted 15 January 2013 - 05:51 PM

You still didn't make the change. You didn't close the isset() function with a parenthesis. Instead you added a parenthesis to the end of the if statement.
Was This Post Helpful? 1
  • +
  • -

#7 Jheroll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-January 13

Re: Problem deleting in table

Posted 15 January 2013 - 05:52 PM

<?php
if (isset($_POST['delete_id']) && !empty($_POST['delete_id'])) {
  $delete_id = mysql_real_escape_string($_POST['delete_id']);
  mysql_query("DELETE FROM usersinfo WHERE `ID`=".$delete_id);
  header('Location: main.php');
}
?>


I still get the same error..
[/quote]

nothings happening..
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10445
  • View blog
  • Posts: 38,682
  • Joined: 27-December 08

Re: Problem deleting in table

Posted 15 January 2013 - 05:59 PM

Do some debugging. Check if the values are set before the if statement, and print them out to see what you are getting. Note that this might cause problems with the header() function regarding a headers already sent error message, but we can remove the print statements after doing a little debugging.

Also, you really should be moving away from the mysql_*() family of functions.
Was This Post Helpful? 1
  • +
  • -

#9 Jheroll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-January 13

Re: Problem deleting in table

Posted 15 January 2013 - 06:00 PM

View PostJheroll, on 15 January 2013 - 05:52 PM, said:

<?php
if (isset($_POST['delete_id']) && !empty($_POST['delete_id'])) {
  $delete_id = mysql_real_escape_string($_POST['delete_id']);
  mysql_query("DELETE FROM usersinfo WHERE `ID`=".$delete_id);
  header('Location: main.php');
}
?>


I still get the same error..


nothings happening..
[/quote]

Now the problem is when I click the delete button nothing happens :(
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10445
  • View blog
  • Posts: 38,682
  • Joined: 27-December 08

Re: Problem deleting in table

Posted 15 January 2013 - 06:03 PM

Stop being helpless and try to do some debugging. I'm trying to point you in the direction you'll need to do some debugging. Programming requires effort and initiative, not just someone on the other end of a forum telling you exactly what to do.

By the way- "it's not working" is not a clear description. Do a little debugging to find out why things aren't working.
Was This Post Helpful? 1
  • +
  • -

#11 Jheroll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-January 13

Re: Problem deleting in table

Posted 15 January 2013 - 06:29 PM

View Postmacosxnerd101, on 15 January 2013 - 06:03 PM, said:

Stop being helpless and try to do some debugging. I'm trying to point you in the direction you'll need to do some debugging. Programming requires effort and initiative, not just someone on the other end of a forum telling you exactly what to do.

By the way- "it's not working" is not a clear description. Do a little debugging to find out why things aren't working.


Sorry that was childish of me.

The value of the ID that was selected from the main form was caught and posted in the delete form.
And Ive change the code to this
<?php
$del = ($_POST['delete_id']);
echo $del

?>

<?php

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("people", $con);
mysql_query("DELETE FROM usersinfo WHERE ID='delete_id'");
mysql_close($con);
?>


Still the selected Item cant be deleted.
Is there a problem with the code ? :)
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10445
  • View blog
  • Posts: 38,682
  • Joined: 27-December 08

Re: Problem deleting in table

Posted 15 January 2013 - 06:32 PM

You're missing a semi-colon on your delete statement. Also, delete_id in your query isn't a variable. Remember- all variables start with a dollar-sign in PHP. Lastly, always remember to print the result of mysql_error() if FALSE was returned from the mysql_query() function. That will give you more feedback on what went wrong with your query.
Was This Post Helpful? 1
  • +
  • -

#13 Jheroll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-January 13

Re: Problem deleting in table

Posted 15 January 2013 - 06:44 PM

View Postmacosxnerd101, on 15 January 2013 - 06:32 PM, said:

You're missing a semi-colon on your delete statement. Also, delete_id in your query isn't a variable. Remember- all variables start with a dollar-sign in PHP. Lastly, always remember to print the result of mysql_error() if FALSE was returned from the mysql_query() function. That will give you more feedback on what went wrong with your query.


Thank You for guiding and helping me. Its now working. Your great! thank you so much!!! :)))
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10445
  • View blog
  • Posts: 38,682
  • Joined: 27-December 08

Re: Problem deleting in table

Posted 15 January 2013 - 06:46 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1