//The program is supposed to
//read,write,update,delete and show all records.
//only writing works...
//
//What's wrong:
//- doesn't read, and doesn't show the msg "record not found"
//- doesn't update, and shows the right msg
//- doesn't delete, and shows msg as ok.
//- doesn't show all records (and standard SELECT * FROM...)
//
//I've tested each sql statement on PHPMYADMIN
//and works fine, of course, replacing variables with
//vaues in quotes.
<?php
// THIS IS ONE WHOLE PIECE OF PHP
//==============================
//=== G L O B A L S
//==============================
$flag = -1 ;
$msg = "" ;
$tmsg = array();
$tmsg[0] = "" ;
$tmsg[8] = "server unavailable";
$tmsg[9] = "cannot find database";
$tmsg[1] = "cannot find record" ;
$tmsg[2] = "cannot create record" ;
$tmsg[3] = "cannot update record" ;
$tmsg[4] = "cannot delete record" ;
$tmsg[12] = "record created";
$tmsg[13] = "record updated" ;
$tmsg[14] = "record deleted" ;
$rows = "";
$rec_date = $POST['rec_date'];
$rec_unit = $POST['rec_unit'];
$rec_code = $POST['rec_code'];
$rec_desc = $POST['rec_desc'];
//=============================
//=== F U N C T I O N S
//=============================
function db_connect(){
$flagthis = 0;
$dbcnx = mysql_connect("http://mysql_ourinternet.us/", "lisaweb_test","test1");
if (!$dbcnx) {
$flagthis = 8;
}
if (! @mysql_select_db("lisaweb_test") ) {
$flagthis = 9;
}
return $flagthis;
}
//-----------------------------
function fmyread($opthis){
global $rows, $rec_date, $rec_unit, $rec_code, $rec_desc ;
$flagthis = 0;
$sql = "SELECT * FROM sia_test WHERE rec_date = $rec_date AND rec_unit = $rec_unit AND rec_code = $rec_code LIMIT 1";
$result = mysql_query($sql);
if (!$result){ $flagthis = $opthis;}
else { $rows = mysql_fetch_array($result) ; }
return $flagthis;
}
//--------------------------------------
function fmycreate($opthis){
global $rec_date, $rec_unit, $rec_code, $rec_desc ;
$flagthis = 0;
$sql = "INSERT INTO sia_test ( rec_date , rec_unit , rec_code , rec_desc , rec_id ) VALUES ('$rec_date', '$rec_unit', '$rec_code', '$rec_desc', '')" ;
$result = mysql_query($sql);
if (!$result) { $flagthis = $opthis; }
return $flagthis;
}
//--------------------------------------
function fmyupdate($opthis){
global $rec_date, $rec_unit, $rec_code, $rec_desc ;
$flagthis = 0;
$sql = "UPDATE sia_test SET rec_desc = $rec_desc WHERE rec_date = $rec_date AND rec_unit = $rec_unit AND rec_code = $rec_code LIMIT 1";
$result = mysql_query($sql);
if (!$result) { $flagthis = $opthis; }
return $flagthis;
}
//--------------------------------------
function fmydelete($opthis){
global $rec_date, $rec_unit, $rec_code, $rec_desc ;
$flagthis = 0;
$sql = "DELETE FROM sia_test WHERE rec_date = $rec_date AND rec_unit = $rec_unit AND rec_code = $rec_code LIMIT 1";
// $sql = "REPLACE FROM sia_table WHERE rec_date = $rec_date AND rec_unit = $rec_unit AND rec_code = $rec_code LIMIT 1";
$result = mysql_query($sql);
if (!$result) { $flagthis = $opthis; }
return $flagthis;
}
//--------------------------------------
function fmyselect($opthis){
global $rec_date, $rec_unit, $rec_code, $rec_desc ;
$flagthis = 0;
$sql = "SELECT * FROM sia_test ";
$result = mysql_query($sql);
echo(" <script> \n ");
echo(" var t = new Array() \n ");
$rows = mysql_fetch_array($result) ;
$i = 0;
while ( $rows = mysql_fetch_array($result) ) {
echo(" t[$i] = "."'". "$rows[$rec_date]" .
- "$rows[$rec_desc]" . "'" . " \n ");
$i++;
}
echo(" </script> \n ");
if (!$result) { $flagthis = $opthis; }
return $flagthis;
}
//==== EXECUTION CONTINUE HERE ===========================
//==== EXECUTION CONTINUE HERE ===========================
//==== EXECUTION CONTINUE HERE ===========================
if (isset($POST['opcode'])) {
$opcode = $POST['opcode'];
if ($opcode > 0){ $flag = 0;}
} else $opcode = 0;
if ($flag == 0){ $flag = db_connect($flag); }
if ($flag == 0){
if ($opcode == 1) { $flag = fmyread($opcode); }
if ($opcode == 2) { $flag = fmycreate($opcode); }
if ($opcode == 3) { $flag = fmyupdate($opcode); }
if ($opcode == 4) { $flag = fmydelete($opcode); }
if ($opcode == 5) { $flag = fmyselect($opcode); }
}
$msg = $tmsg[$flag];
if ($flag > 0) { $msg = $tmsg[$flag] ; }
else { if ($opcode > 1) { $msg = $tmsg[$opcode+10]; } }
//=================================================
// CREATING JAVASCRIPT FUNCTION TO GET FLAGS & DATA
// numeric vars must be without quotes
//=================================================
echo(" <script> \n ");
echo(" var flagphp = $flag \n ");
echo(" var opcode = $opcode \n ");
echo(" var msgphp = '$msg' \n ");
echo(" var t = new Array('','','','','') \n ");
echo(" function storedata(){ \n ");
echo(" t[1] = '$rec_date' \n ");
echo(" t[2] = '$rec_unit' \n ");
echo(" t[3] = '$rec_code' \n ");
echo(" t[4] = '$rec_desc' \n ");
echo(" } \n ");
echo(" </script> \n ");
?>
<!-- ##################################################### -->
<!-- ## H T M L & J S C R I P T -->
<!-- ##################################################### -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>DEMO</TITLE>
<META content=... name=description>
<META content=... name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
//==== FOR DEBUGGING PURPOSES
/
alert(" opcode: " + opcode) // for debugging
alert(" flag : " + flagphp) // for debugging
alert(" msg : " + msgphp) // for debugging
/
//===== END OF DEBUGGING
function sendreg(whattodo){
document.phptran.opcode.value = whattodo // 7=read, 8=update, 9=write
document.phptran.submit()
}
</script>
</head>
<BODY bgcolor="#003300" text="#ffc000" LINK="#00aaaa" VLINK="#00aaaa" ALINK="#00aaaa" MARGINWIDTH=0 MARGINHEIGHT=0 LEFTMARGIN=0 TOPMARGIN=0>
<form name=phptran ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
<input type=hidden name=opcode value="">
<table width=500 align=center>
<tr><td colspan=2><br><br><font size=3>Testing PHP-Mysql <a href="mailto:lisawebs@yahoo.com"> by lisawebs@yahoo.com</a><br>Database name: <strong>lisaweb_test</strong><br>Table name: <strong>sia_test</strong><br>Table Structure:<br>rec_date - VARCHAR(10)<br>rec_unit - VARCHAR(1)<br>rec_code - VARCHAR(1)<br>rec_id - INT(4) autoincrement - PRIMARY INDEX<br>rec_desc - VARCHAR(20)<BR><a href="lisawebs_demo.txt" target="_blank">CLICK TO SEE PHP CODE</A><BR><BR> </td>
</tr><tr>
<td>Date: </td><td> <input type=text name=rec_date value="" size=10></td>
</tr><tr>
<td>Unit: </td><td> <input type=text name=rec_unit value="" size=1></td>
</tr><tr>
<td>Code: </td><td> <input type=text name=rec_code value="" size=1></td>
</tr><tr>
<td>Description: </td><td> <input type=text name=rec_desc value="" size=20><br>
</tr><tr>
<td colspan=2>
<input type=reset value="clear screen">
<input type=button value=read onClick="sendreg(1)">
<input type=button value=create onClick="sendreg(2)">
<input type=button value=update onClick="sendreg(3)">
<input type=button value=delete onClick="sendreg(4)">
<input type=button value=show_all onClick="sendreg(5)">
<br><br>... ashamed about this, but I won't give up
</td></tr></table>
<br><br>
</form>
<script>
// to avoid recursive writing
document.phptran.opcode.value = ""
if (opcode > 6)
showdata()
function showdata(){
document.phptran.rec_date.value = t[1]
document.phptran.rec_unit.value = t[2]
document.phptran.rec_code.value = t[3]
document.phptran.rec_desc.value = t[4]
}
if (msgphp != "")
alert(msgphp)
if (flagphp == 0 & opcode == 1)
storedata()
if (opcode == 5){
document.write('<br>Records<br>')
for (i=0;i<t.length;i++){ document.write(t) }
}
</script>
</body>
</html>