hi, i spent a few hours trying to get this right and i give up, i just cant do it. i am making an application that inserts, updates, deletes rows in a sql table(delete using javascript code in the script where onclick deletes a row), then displays the results in the form of an html table. the way i have it coded now, i can first update a row, insert a row, and then delete a row or rows, but once i delete rows, after that, i cant insert or update any rows. any help GREATLY appreciated on this one. thank you. derek
here is the code to the entire application in php.
Code:
<?php
include("connections.php");
////////////////////////////////////////
////////////////////////////////////////
/// query db and loop through rows example
$id = $_POST['id'];
$field2 = $_POST['data2'];
$field3 = $_POST['data3'];
$field4 = $_POST['data4'];
$field5 = $_POST['data5'];
$field6 = $_POST['data6'];
$id = $_GET['id']; //gets the id value from javascript code below
/*echo "<pre>"; //displays array and variables to check whats going on
print_r($_POST);
echo "</pre>";*/
//if id is set from the onclick javascript below,
if ($id) {
mysql_query("DELETE FROM table1 WHERE record_id ='$id'");
}
if(isset($_POST['Submit'])){ //if submit has been set, or clicked.
if($id){ //has id been set?
mysql_query("UPDATE table1 SET field2='$field2',field3='$field3',field4='$field4',field5='$field5',field6='$field6' WHERE record_id ='$id'");
}else{
if(!$id) { //if id has not been set, insert data into next row.
mysql_query("INSERT INTO table1 (field2,field3,field4,field5,field6)VALUES('$field2','$field3','$field4','$field5','$field6')");
}//end if
}//end else
}//end isset
/// query db and loop through rows example
//selects everything from table and assigns to the variable $query
$query = mysql_query("SELECT * FROM table1");
$table = "<table width=\"40%\" border=\"1\">
<tr>
<td>heading1</td>
<td>heading2</td>
<td>heading3</td>
<td>heading4</td>
<td>heading5</td>
<td>heading6</td>
</tr>";
while($row = mysql_fetch_array($query)){
$table .= " <tr>
<td onclick=\"document.location.href='?id=".$row['record_id']."'\" >".$row['record_id']."</td>
<td>".$row['field2']."</td>
<td>".$row['field3']."</td>
<td>".$row['field4']."</td>
<td>".$row['field5']."</td>
<td>".$row['field6']."</td>
</tr>";
}
$table .= "</table>";
?>
<!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=iso-8859-1" />
<title>Update Statement</title>
<style type="text/css">
<!--
.style1 {color: #990000}
.style2 {color: #0066FF}
.style3 {color: #0000CC}
.style4 {color: #993399}
-->
</style>
</head>
<body>
<p><?php echo $table; ?></p>
<p><span class="style2">*If updating a row, please enter the ID of the row you want to update, and enter the data</span><br />
<br />
<span class="style3">*If adding a new row of data , just leave the ID space blank</span><br />
<br />
<span class="style4">*To delete a row of data, click on the id of the row to delete</span><br />
</p>
<form id="form1" name="form1" method="post" action="">
<table width="31%" border="0">
<tr>
<td>ID</td>
<td><input name="id" type="text" id="id" /></td>
</tr>
<tr>
<td width="32%">Data2</td>
<td width="68%"><input name="data2" type="text" id="data2" /></td>
</tr>
<tr>
<td>Data3</td>
<td><input name="data3" type="text" id="data3" /></td>
</tr>
<tr>
<td>Data4</td>
<td><input name="data4" type="text" id="data4" /></td>
</tr>
<tr>
<td>Data5</td>
<td><input name="data5" type="text" id="data5" /></td>
</tr>
<tr>
<td>Data6</td>
<td><input name="data6" type="text" id="data6" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
|