Jump to content

Recommended Posts

I'm only a beginner at php and i don't know alot yet so what i'm asking is probably shockingly simple so please bear with me. i'm making a text based game and what i'm stuck on is making a button click change a value in a variable ive tried looking at tutorials but i cant find anything helpful if you could explain it to me or give me an example that would be great  or  a link to a tutorial that you think will help 

 

 

Consider the following code:

 

$myVar = 'sword';
echo "<a href=\"mypage.php?myvar=axe\">Change my var</a>";
if(isset($_GET['myvar']) and $_GET['myvar'] == 'axe'){
     $myVar = mysql_real_escape_string($_GET['axe']);
     echo $myVar; //it will print 'axe'
}

 

What im doing with that code is initialize $myVar with the value 'sword'. The "<a href ..." part prints a link with a url variable which then i check if it is set. If so $myVar will become as the url variable, 'axe'. The $_GET is the key to capture url variables. Hope its clear.

I understand everything except the mysql_real_escape_string  what is that doing exactly? ??? 

 

is it possible to put this in a buttton? i know it has a onclick="" feature  but im not sure how to put anything inside can i just put it in as code or do i need to make a function and call it?

From php manual:

 

Escapes special characters in the unescaped_string, taking into account the current character set of the connection so that it is safe to place it in a mysql_query(). If binary data is to be inserted, this function must be used.

 

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.

 

This function must always (with few exceptions) be used to make data safe before sending a query to MySQL.

 

The onclick event is used to call javacript code or functions. Make an html link as in the sample code and when u click it, it sends to the url the variable which can be captured with $_GET. Hope its clear now.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.