PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
amit_n
Forum Newbie
Posts: 9 Joined: Sat Mar 07, 2009 12:19 am
Post
by amit_n » Sat Mar 07, 2009 12:42 am
Hi,
I am new to this forum and beginner in PHP.
I have following code. I am able show values in dropdown list but i am not able to select the value and delete it from DB.
Please suggest me what is wrong into this.
Thanks
Amit
Code: Select all
<?
// Connect database
mysql_connect("localhost","root","");
mysql_select_db("ItcPolicy");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form id="form1" name="form1" method="POST" action="delete.php">
<? echo "List Of Policies:<br>";?>
<select name="select">
<option value="">--- Select ---</option>
<?
$q1=mysql_query("SELECT PolicyName, FileName FROM listofpolicies;");
// Show records by while loop.
while($q1arr = mysql_fetch_assoc($q1)){
?>
<option value="<? echo $q1arr['FileName']; ?>" <? if($q1arr['FileName']==$select){ echo "selected"; } ?>><? echo $q1arr['PolicyName']; ?></option>
<?
// End while loop.
}
?>
</select>
<input type="submit" name="Submit" value="Select" />
</form>
<p>
<?
// If you have selected from list box.
if(isset($select)&&$select!=""){
$select=$_POST['Select'];
echo $select;
}
if(isset($select) && $select!=""){
// Get records from database (table "name_list").
$q2=mysql_query("DELETE * FROM listofpolicies where FileName='Catamaran.pdf';");
//$q1arr=mysql_fetch_assoc($q2);
?>
<? echo $q1arr['PolicyName']; ?>
<?
// End if statement.
}
// Close database connection.
mysql_close();
?>
</p>
</body>
</html>
Last edited by
amit_n on Sat Mar 07, 2009 5:02 am, edited 1 time in total.
BomBas
Forum Commoner
Posts: 41 Joined: Wed Mar 04, 2009 1:04 pm
Post
by BomBas » Sat Mar 07, 2009 2:46 am
Get rid of the ; here:
Code: Select all
SELECT PolicyName, FileName FROM listofpolicies;
Otherwise, do you get any problem? Because your code seems right to me.(expect what I said above)
amit_n
Forum Newbie
Posts: 9 Joined: Sat Mar 07, 2009 12:19 am
Post
by amit_n » Sat Mar 07, 2009 4:58 am
Hi,
Thanks for reply.
It was not that problem. I made following changes
1) $q2=mysql_query("DELETE * FROM listofpolicies where FileName='Catamaran.pdf';");
I removed "$q2=" and * from this line
2) $select=$_POST['Select'];
I have changed it to $select=$_POST['select'];
I know this is not logical but it is working fine.
This is my new code
Code: Select all
<?
// Connect database
mysql_connect("localhost","root","");
mysql_select_db("ItcPolicy");
$select=$_GET['select'];
if($select){
echo $select;
mysql_query("DELETE FROM listofpolicies where FileName='$select';");
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form id="form1" name="form1" method="GET" action="delete.php">
<? echo "List Of Policies:<br>";?>
<select name="select">
<option value="">--- Select ---</option>
<?
$q1=mysql_query("SELECT PolicyName, FileName FROM listofpolicies;");
// Show records by while loop.
while($q1arr = mysql_fetch_assoc($q1)){
?>
<option value="<? echo $q1arr['FileName']; ?>" <? if($q1arr['FileName'] == $select){ echo "selected"; } ?>><? echo $q1arr['PolicyName']; ?></option>
<?
// End while loop.
}
// Close database connection.
mysql_close();
?>
</select>
<input type="submit" name="Submit" value="Delete" />
</form>
</body>
</html>
Thanks
Amit
BomBas
Forum Commoner
Posts: 41 Joined: Wed Mar 04, 2009 1:04 pm
Post
by BomBas » Sat Mar 07, 2009 5:10 am
1) $q2=mysql_query("DELETE * FROM listofpolicies where FileName='Catamaran.pdf';");
I removed "$q2=" and * from this line
Oh sorry , I didn't see your DELETE query... My bad.
And yeah. for deleting all of the column's values you have to get rid of *.
I know this is not logical but it is working fine.
It is logical, PHP is
case sensitive !