Does code run after "mysql_query() or die()" or does it wait
Posted: Thu Oct 15, 2009 11:42 am
Hi - I'm struggling to tell what's failing so I realised I don't know enough about how php executes mysql queries.
Will everything happen in order, or could the javascript at the bottom render/fire without the mysql being successful?
Additionally - what's the best way to fire/react to a timeout (does mysql_error() provide that?)
thanks
Will everything happen in order, or could the javascript at the bottom render/fire without the mysql being successful?
Additionally - what's the best way to fire/react to a timeout (does mysql_error() provide that?)
thanks
Code: Select all
<?php
include "includes/test_login.php";
include "includes/db_connect.php";
$action = escapeStr($_POST['action']);
$fsRef = escapeStr($_POST['fsRef']);
$tagIndex = escapeStr($_POST['tagIndex']);
$owner = escapeStr($_POST['owner']);
//Compile query based on action
$query = "";
//action = add ----------------------------------------------------------------------------
if($action=="add"){
$query .= "INSERT INTO `tags` (";
$i = 0;
foreach ($_POST as $field => $value){
$field = escapeStr($field);
if($field!="action"){
if($i++>0)$query .= ",";//only prepend comma after first time
$query .= "`$field`";
}
}
$query .= ") VALUES (";
$i = 0;
foreach ($_POST as $field => $value){
$value = escapeStr($value);
if($field!="action"){
if($i++>0)$query .= ",";//only prepend comma after first time
$query .= "'$value'";
}
}
$query .= ")";
}
//action = update ----------------------------------------------------------------------------
if($action=="update"){
$query .= "UPDATE `tags` SET ";
$i = 0;
foreach ($_POST as $field => $value){
$field = escapeStr($field);
$value = escapeStr($value);
if($field!="action" && $field!="fsRef" && $field!="tagIndex" && $field!="owner"){
if($i++>0)$query .= ",";//only prepend comma after first time
$query .= "`$field`='$value'";
}
}
$query .= " WHERE `tagIndex`='$tagIndex' AND `fsRef`='$fsRef' AND `owner`='$owner'";
}
//action = delete ----------------------------------------------------------------------------
if($action=="delete"){
$query .= "DELETE FROM `tags` WHERE `fsRef`='$fsRef' AND `tagIndex`='$tagIndex' AND `owner`='$owner'";
}
//Run compiled query -------------------------------------------------------------------------
mysql_query($query) or die(mysql_error());
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Saved </title>
</head>
<body>
<script type="text/javascript">
// Run js having completed DB update
this.parent["<?php echo $fsRef ?>leaveEditMode"]("<?php echo $action ?>");
// Send status message
var returnData = '{"fsRef":"<?php echo $fsRef ?>","text":"Database updated: <?php echo $action ?> tag"}';
this.parent["<?php echo $fsRef ?>cbDisplayStatus"](returnData);
document.location = "../iframes/loading.html";
</script>
</body>
</html>