Jump to content


Photo

php help send/update/display


  • Please log in to reply
29 replies to this topic

#21 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 July 2006 - 03:11 PM

1st problam was a typo the second is the echo of key value of all the research database ok.


page 1
<?

echo"<a href='wahtever.php?&cmd=update'>update</a>";

$db=mysql_connect("localhost","name","password");

mysql_select_db("database_name",$db);

$query="SELECT * from research where userid='$userid";

$result=mysql_query($query);

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

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

}

?>

page 2

<?
$db=mysql_connect("localhost","name","password");

mysql_select_db("database_name",$db);

if($_GET['cmd']=='update') {

$query="UPDATE research SET points=$points+1 where userid='$userid'";

$result=mysql_query($query);

echo"Database Updated";

}else{

echo"Sorry no update";

}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#22 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 July 2006 - 03:19 PM

thx for update..

new error comes up

Warning: Variable passed to each() is not an array or object in XXX on line 23

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

maybe this is to do with my not having a key in the research table i only have 1 field which is points.  also i dont have userid since i dont have members area.  im just testing basic so i always delete

where userid='$userid

thx for help so far.  if only it will display then that will be great.

#23 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 July 2006 - 03:27 PM

edited sorry i missed a while loop lol....................


<?

echo"<a href='wahtever.php?&cmd=update'>update</a>";

$db=mysql_connect("localhost","name","password");

mysql_select_db("database_name",$db);

$query="SELECT * from research";

$result=mysql_query($query);

while($record=mysql_fetch_assoc($result){

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

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

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#24 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 July 2006 - 03:55 PM

Parse error: parse error, unexpected '{'

for

while($record=mysql_fetch_assoc($result) {

wish i understood this last bit i might be able to work out problem myself.  cant wait for my php book to get here lol.  i wonder if ill get this example working before it does =).

who said php was easy ^^

#25 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 09 July 2006 - 03:58 PM

while($record=mysql_fetch_assoc($result) {

You're just missing a parentheses:
while($record=mysql_fetch_assoc($result)) {
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#26 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 July 2006 - 04:09 PM

well page loads with no errors now and when i click to research it says database updated but still doesnt show current value on main page.



#27 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 July 2006 - 05:59 PM

do you actually have a select query on your main page?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#28 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 July 2006 - 06:51 PM

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 :)

<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>

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.

#29 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 10 July 2006 - 04:27 AM

okay what exactly are you trying to do here: that is, what is the bigger 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:
<?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 ****
   // ***********************************
?>	

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#30 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 10 July 2006 - 09:32 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users