Hi there,
I was hoping for a little advice please 🙂
I have 2 seperate form (both working independantly) ad i am trying to integrate them into 1 form with 2 buttons, one for insertion of record and other for deletion of record/s.
Im have a form that has a drop down and a user selects an option and clicks submits a record is inserted into a database and the inserted record is echoed to show whats in the table.
Accompaying each record that is echoed back is a check box. There are two submit buttons, one for inserting a record and the other for deleting a record (if a check box has been ticked by user next to echoed record).
Both the inserting query and delete query work independantly to each other and im am trying to integrate them...
Heres the basic bit to the delete query:
if(isset($_POST['perform'])) {
foreach($_POST['id'] as $id) { // This will loop through the checked checkboxes
$query_test="DELETE FROM animalfears WHERE AnimalFearID='$id'"; // Change yourtable and id.
$test = mysql_query($query_test) or die("Problem with the query: $query_test<br />" . mysql_error());
}
}
?>
<?php do { ?>
<tr>
<td align="center"><span class="style12"><?php echo $row_test['FearID']; ?></span></td><td><label>
<input type="checkbox" name="id[<?php echo $row_test['AnimalFearID']; ?>]" id="id_<?php echo $row_test['AnimalFearID']; ?>" value="<?php echo $row_test['AnimalFearID']; ?>" />
</label></td>
</tr><?php } while ($row_test = mysql_fetch_assoc($test)); ?>
// checkbox code above
and heres the basic bit of code for the insert:
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO animalfears (AnimalFearID, AnimalID, FearID) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['animalfearid'], "int"),
GetSQLValueString($_POST['animalid'], "int"),
GetSQLValueString($_POST['select'], "text"));
mysql_select_db($database_woodside, $woodside);
$Result1 = mysql_query($insertSQL, $woodside) or die(mysql_error());
$insertGoTo = "add_animal_fear.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
<?php do { ?>
<tr>
<td align="center"><span class="style12"><?php echo $row_test['FearID']; ?></span></td><td><label>
<input type="checkbox" name="id[<?php echo $row_test['AnimalFearID']; ?>]" id="id_<?php echo $row_test['AnimalFearID']; ?>" value="<?php echo $row_test['AnimalFearID']; ?>" />
</label></td>
</tr><?php } while ($row_test = mysql_fetch_assoc($test)); ?>
The form kind of works..?? Well it works to insert a record fine and it is echoed back with the checkbox.. What doesnt work properly is to delete a record. It doesnt come up with any errors but all it does is just INSERT another record???????
I tried to add the Delete code form to the insert form so theres only 1 form (originally from INSERT query) for both insert and delete query....
Im thinking perhaps an if else statement of some kind may be the right way to go .. lke:
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
else
if(isset($_POST['perform'])) {
foreach($_POST['id'] as $id) { // This will loop through the checked checkboxes
$query_test="DELETE FROM animalfears WHERE AnimalFearID='$id'";
$test = mysql_query($query_test) or die("Problem with the query: $query_test<br />" . mysql_error());
}
}
I want to make it so when either the insert or delete button is clicked that function is carried out....
How can i do this please? If any body could help that would be ace :
Thanks
Heres full code:
<?php require_once('Connections/woodside.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO animalfears (AnimalFearID, AnimalID, FearID) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['animalfearid'], "int"),
GetSQLValueString($_POST['animalid'], "int"),
GetSQLValueString($_POST['select'], "text"));
mysql_select_db($database_woodside, $woodside);
$Result1 = mysql_query($insertSQL, $woodside) or die(mysql_error());
$insertGoTo = "add_animal_fear.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
$colname_animal = "-1";
if (isset($_GET['recordID'])) {
$colname_animal = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);
}
mysql_select_db($database_woodside, $woodside);
$query_animal = sprintf("SELECT * FROM animal WHERE AnimalID = %s", $colname_animal);
$animal = mysql_query($query_animal, $woodside) or die(mysql_error());
$row_animal = mysql_fetch_assoc($animal);
$totalRows_animal = mysql_num_rows($animal);
mysql_select_db($database_woodside, $woodside);
$query_fears = "SELECT * FROM fears ORDER BY FearID ASC";
$fears = mysql_query($query_fears, $woodside) or die(mysql_error());
$row_fears = mysql_fetch_assoc($fears);
$totalRows_fears = mysql_num_rows($fears);
mysql_select_db($database_woodside, $woodside);
$query_test = "SELECT * FROM animalfears WHERE AnimalID='" . $row_animal['AnimalID'] . "'";
$test = mysql_query($query_test, $woodside) or die(mysql_error());
$row_test = mysql_fetch_assoc($test);
$totalRows_test = mysql_num_rows($test);
mysql_select_db($database_woodside, $woodside);
$query_fearslist = "SELECT * FROM fears WHERE FearID NOT IN (SELECT FearID FROM animalfears WHERE AnimalID='" . $row_test['AnimalID'] . "')";
$fearslist = mysql_query($query_fearslist, $woodside) or die(mysql_error());
$row_fearslist = mysql_fetch_assoc($fearslist);
$totalRows_fearslist = mysql_num_rows($fearslist);
if(isset($_POST['perform'])) {
foreach($_POST['id'] as $id) { // This will loop through the checked checkboxes
$query_test="DELETE FROM animalfears WHERE AnimalFearID='$id'"; // Change yourtable and id.
$test = mysql_query($query_test) or die("Problem with the query: $query_test<br />" . mysql_error());
}
}
<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<table width="362" border="0">
<tr>
<td width="109"><span class="style11">Add New Fear:</span></td>
<td width="108" align="center"> <label>
<select name="select">
<?php
do {
?>
<option value="<?php echo $row_fearslist['FearID']?>"><?php echo $row_fearslist['FearID']?></option>
<?php
} while ($row_fearslist = mysql_fetch_assoc($fearslist));
$rows = mysql_num_rows($fearslist);
if($rows > 0) {
mysql_data_seek($fearslist, 0);
$row_fearslist = mysql_fetch_assoc($fearslist);
}
?>
</select>
</label></td>
<td width="123" align="center"><input type="submit" name="Submit" value="Submit" /></td>
<td width="109 "align="center"><table width="109" border="0" class="newbord">
<tr><td align="center"><span class="style7 style13"><strong>This Animals Fears:</strong></span></td>
</tr>
<?php do { ?>
<tr>
<td align="center"><span class="style12"><?php echo $row_test['FearID']; ?></span></td><td><label>
<input type="checkbox" name="id[<?php echo $row_test['AnimalFearID']; ?>]" id="id_<?php echo $row_test['AnimalFearID']; ?>" value="<?php echo $row_test['AnimalFearID']; ?>" />
</label></td>
</tr><?php } while ($row_test = mysql_fetch_assoc($test)); ?>
</table></td>
<td><label>
<input type="submit" name="perform" id="perform" value="Delete" />
</label></td>
</tr>
<tr>
<td align="center"><a href="add_animal2.php?recordID=<?php echo $row_animal['AnimalID']; ?>" class="style7">Continue</a></td>
<td> </td>
</tr>
</table> <p>
<input name="animalfearid" type="hidden" id="animalfearid" value="<?php echo $totalRows_animalfears +1 ?>" />
<input name="animalid" type="hidden" id="animalid" value="<?php echo $row_animal['AnimalID']; ?>" />
</p>
<p>
<label>
</label>
</p>
<input type="hidden" name="MM_insert" value="form1">
</form>
<?php