开发者

How do I have more than 1 MySQL UPDATE queries on the same page?

I have a problem with a page I'm building. I have 2 buttons, each with the aim to UPDATE 'filelocation' field in my MySQL table, onClick.

$setloc = mysql_query("UPDATE users SET filelocation='location1' WHERE username='$username'");
$setloc2 = mysql_query("UPDATE users SET filelocation='location2' WHERE username='$username'");

So the above are my queries to UPDATE filelocation to either 'location1' or 'location2'; and below are the buttons which should execute either $setloc or $setloc2 onClick():

<div id='targetdiv'>
    <input class='destination' type='button' name='button1' onClick='<?php $setloc; ?>' value='Click here 1' /><br />
    <input class='destination' type='button' name='button2' onClick='<?php $setloc2; ?>' value='click here 2' /><br />
</div>

The problem开发者_如何学运维 is, you don't need to click any of the buttons to set the MySQL table's filelocation field. It automatically sets filelocation to $setloc2

How do I make it so that you have to hit a button in order to execute either $setloc or $setloc2; without it automatically setting filelocation to $setloc2.

Thanks a lot in advance, hiroki


You're doing it totally wrong. You php runs on the server. Your javascript runs on the client.

Basically the PHP output is sent to the client, so queries are run, and their output (which is nothing in ur case) is returned to the client on the PHP tags u opened on javascript events. Check your HTML source to understand.

I suggest you read about HTTP's Request/Response nature to understand the difference between HTTP and Desktop Applications.

The right way to do it would be:

<?php 
if ($_GET['input']==1) 
   $setloc = mysql_query("UPDATE users SET filelocation='location1' WHERE username='$username'");
elseif ($_GET['input']==2)
   $setloc2 = mysql_query("UPDATE users SET filelocation='location2' WHERE username='$username'");
else 
{
?>
<form method='get'>
<input type='submit' name='input' value='1' />
<input type='submit' name='input' value='2' />
</form>
<?php
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜