PDA

View Full Version : Help with altered tutorial code


Ehugg
01-09-2008, 02:34 AM
Hello.

I altered the working "Part 10 - Deleting data with PHP" code. It loads fine but when I try to delete an account, all it does is refresh the page.

Here's my code:

<?php
require_once("Connections/connection.php");////Database connection.

////////////////////////////////////////
$del = $_GET['del'];
$username = $_POST['username'];
$password = $_POST['password'];
$submit = $_POST['submit'];

////////////////////////////////////////


if ($del){
/////////////////////////////////////////////////
$query = sprintf("DELETE FROM table1 WHERE username = $del ");
mysql_query($query)or die(mysql_error());
/////////////////////////////////////////////////
}


if ($submit && $password && $username){
/////////////////////////////////////////////////
$query = sprintf("INSERT into table1 (username, password) values ('$username','$password')");
mysql_query($query)or die(mysql_error());
/////////////////////////////////////////////////
}elseif($submit){
echo "Please enter your Username and Password";
}

//////////////////////////////////////////////
$query = sprintf("SELECT * FROM table1"); //
$result = @mysql_query($query); //
$row = mysql_fetch_array($result); //
//////////////////////////////////////////
?>

<?php
/*do{
echo $row['username']."<br>";
}while ($row = mysql_fetch_array($result))
*/
?>







<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action= "<?php $_SERVER['PHP_SELF']; ?>">
<table width="285" border="1">
<tr>
<td width="144">Username</td>
<td width="125">Password</td>
</tr>
<tr>
<td><label>
<input name="username" type="text" id="username" value="" />
</label></td>
<td><label>
<input type="password" name="password" id="password" />
</label></td>
</tr>
<tr>
<td height="23" colspan="2"><label>
<div align="center">
<input type="submit" name="button" id="button" value="Enter" />
<input name="submit" type="hidden" id="submit" value="1" />
</div>
</label></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
<table width="424" height="53" border="1">
<tr>
<td width="48" height="23">ID</td>
<td width="121">Username</td>
<td width="128">Password</td>
<td width="99">Date</td>
</tr>
<?php do { ?>
<tr>
<td height="23"><?php echo $row['account_number'] ?></td>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['password'] ?></td>
<td><?php echo $row['datejoined'] ?></td>
</tr>
<?php }while ($row = mysql_fetch_array($result)) ?>
</table>
<p>&nbsp;</p>
<table width="200" border="1">
<tr>
<td>Username</td>
<td>Password</td>
</tr>
<tr>
<td><form id="form2" name="form2" method="post" action="">
<label>
<input type="text" name="delete" id="deleteid" />
</label>
</form>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="47" colspan="2">

<label>
<div align="center">
<input type="button" name="button2" id="button2" value="Delete" onclick="document.location.href='accounts.php?del=<?php 'deleteid' ?>'" />
</label>

</div>
</div></td>
</tr>
</table>
<p>&nbsp;</p>
</body>
</html>



(http://www.dreamweaverclub.com/forum/../../vtm/)

davidj
01-09-2008, 05:48 AM
where you have

<input type="button" name="button2" id="button2" value="Delete" onclick="document.location.href='accounts.php?del=<?php 'deleteid' ?>'" />

needs to be....

<input type="button" name="button2" id="button2" value="Delete" onclick="document.location.href='accounts.php?del=<?php echo 'deleteid' ?>'" />

notice the echo in the onclick del value

Ehugg
01-09-2008, 03:14 PM
Now i get this "Unknown column 'deleteid' in 'where clause'"

davidj
01-09-2008, 03:36 PM
where you hae this...


<input type="button" name="button2" id="button2" value="Delete" onclick="document.location.href='accounts.php?del=<?php 'deleteid' ?>'" />


this bit...


onclick="document.location.href='accounts.php?del=<?php 'deleteid' ?>'" />


this should be an numerical id of a record not a string as you have it...

<?php 'deleteid' ?>


also you need to echo it

Ehugg
01-09-2008, 06:27 PM
Ok thanks it works now.