Jump to content

php help send/update/display


Newbiephper

Recommended Posts

my code is what u see above.  so yeah i think so.

i tried it ur way, btw thx for your pm (im sorry about that if u like i will copy ur work over to forum for other people to view and learn as it was very informative).

anyways based on a 1 page idea with submit button
have a look at this and see how many problems i got :)

[code]
<html>
<head>
<title>Research Test</title>
</head>

<body>
<h1>Carry out More Research</h1>

<?php

$db=mysql_connect("hostname","username","password");

mysql_select_db("dbname",$db);

  if ($_POST['submit']) {

$query="UPDATE research SET points='$points+1'";
$result=mysql_query($query);
  }
?>

<form action = '<?php $_SERVER['PHP_SELF']; ?>' method = 'post'>
  <input type = 'submit' value='research' name='research'>
</form>

<p></p>
<h2>Current Research level</h2>

$query2="SELECT * from research";

$result=mysql_query($query2);

while($record=mysql_fetch_assoc($result)) {

while(list($key,$value)=each($record)) {

echo "<br> $key : $value <br>";
}
}
?>

</body>
</html>
[/code]

thx to any1 thats helped out on this problem.  i feel if i can get one workable example i can learn and modify it to fulfil many ideas i have.
Link to comment
Share on other sites

okay what exactly are you trying to do here: that is, what is the [i]bigger[/i] picture. 

here is 2 basic scenarios:

a) Do you want a page that shows the current number, and all i'm gonna do is click a button and it will automatically increment the database number by one, and show the new number on the page? OR...

b) are you wanting to have a form that the user will be inputing some other information, and in addition to whatever you are going to do with the user's input, you wish for it to update this number by one and show the current number?

if you are shooting for option a), then you should go with redarrow's method of using a link to pass the variable.  if you are wanting the user to input other information of some kind, then go for the form method. I assume that you are wanting to eventually have some kind of login system and a different row in your database for each person. 

I am going to show you a step-by-step example of redarrow's method, along with nifty comments and everything, because you are saying you just want to get something "working" and this is easier. I know this code looks long, but it's mostly comments, for your benefit.

example of using a link link:
[code]
<?php
  //connect to database
  $server = 'localhost'; //your host name here. localhost usually works
  $username = 'username'; //db username here
  $password = 'password'; //db password here
  $databasename = 'databasename'; //name of db here
  $conn = mysql_connect($server,$username,$password) or die(mysql_error());
  mysql_select_db($databasename) or die(mysql_error());

  // prevent sql injection example:
  // i am including this as an example of
  // sanitizing your variables, as i am sure
  // your ultimate goal is to base your
  // query on individual people
  function clean_var($value){
      if (get_magic_quotes_gpc()) { stripslashes($value); }
      if (!is_numeric($value)) { mysql_real_escape_string($value); }   
      return $value;
  }

  // *******************************************
  // **** this part will only happen if the ****
  // **** user clicks the linkie            ****

  // check to see if there is an id 
  // if there is an id..that is, if the
  // user clicked the linkie, then we will
  // increment the number in the database
  if (isset($_GET['id']) && trim($_GET['id']) != '') {
      // sanitize the variable by passing it
      // to the clean_var function
      $id = clean_var($id);
   
      // make your query string. this string assumes
      // that your table name is 'research' and
      // the column you want incremented by one is called
      // 'points'. you must change it to the correct names
      // if these are not the right names!
      $sql = "update research set points = points + 1 ";
      // eventually when you have individual users, you will
      // want to add something like this to the end of $sql:
      // $sql.="where id='$id'";

      // execute your query string
      mysql_query($sql) or die(mysql_error());
  } // end the if $_GET['id']
  // **** end click the linkie part ****
  // ***********************************

  // ******************************************************
  // **** this part is going to be displayed          ****
  // **** no matter what, each time the page is viewed ****
 
  // build the query that will get the current number in points
  $sql = "select points from research ";
  // again, you will eventually want to be adding something
  // like this to this query:
  // $sql.='where id='$id'";
     
  // execute your query string. save the result
  // source in a variable so we can retrieve the
  // information
  $result = mysql_query($sql) or die(mysql_error());

  // if the query returned a result..
  if ($result) {
      // let's get the info and put it into $points
      // as it stands, if you have more than one row
      // in your table, there will would technically
      // be able to do a loop to fetch each row, but
      // we are only going to get the first row, as
      // theoretically all the rows should have the
      // same number in them...
      $rs = mysql_fetch_array($result);
      $points = $rs['points'];
  }   

  // example of echoing the current points and
  // a link to 'refresh' this page.
  echo "current points: $points <br><br>";
  // at this point in time, the script will increment
  // no matter what id equals, as long as it equals
  // something. eventually you will want to do something
  // like check the id against the user's id
  echo "<a href='{$_SERVER['PHP_SELF']}?id=blah'>click me!</a>";
  // **** end of part that will always display ****
  // ***********************************
?>
[/code]

I have tested this code to make sure it works, and it does. All you have to do is enter in your correct database information, including table/column names.
Link to comment
Share on other sites

thx a million Crayon Violent.  this has provided me with tonnes of information and topics to read over.  eternally grateful.  the comments are a great help.  its an amazing thing to see it working.  next few days ill experiment with it and read over some things that came up in topic discussiona nd hopefully i'll be that little wiser for it.

thx again to you and redarrow.
Link to comment
Share on other sites

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.