5 Replies - 506 Views - Last Post: 11 October 2013 - 07:15 AM Rate Topic: -----

#1 IxGRpPafwang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-January 13

Get values out of database, update them and put them back with php

Posted 10 October 2013 - 02:59 PM

Probably an easy question but I'm having lots of trouble with PHP. Currently im working on something where I need to get the values out of different rows with Mysql. Lets call these rows Berlin, Paris and London. I then want to update the population of Berlin with 100, Paris 60 and London with 20 using PHP and then put these new values back in the database with Mysql. How would I do this?

I know how to do simple things like getting values out of a table and putting values back but I just dont know how to do anything with it in PHP. This is what I currently have:
<?php

	session_start();
	include 'dbconnect.php';
	
    $json = array('cities'=>array());

	$query 	= 'SELECT * FROM cities';
	$result = mysqli_query($link, $query);
    
    while ($obj = mysqli_fetch_object($result)) {
        array_push($json['cities'], array('name'=>$obj->Name, 'population'=>intval($obj->Population);
	}
	 
	mysqli_query($link,'UPDATE cities SET Population=Population+1');
	
	mysqli_free_result($result);
					
    echo json_encode($json);

    mysqli_close($link);
?>



Is This A Good Question/Topic? 0
  • +

Replies To: Get values out of database, update them and put them back with php

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Get values out of database, update them and put them back with php

Posted 10 October 2013 - 03:10 PM

If you want to update a row where a certain condition is met, you need to add a WHERE clause to the UPDATE query that specifies those conditions.

What you need to do is learn the basics of how the four main SQL commands work. It just so happens I've written a tutorial on the subject here on DIC. I suggest you read it, or find something similar elsewhere on the same subject if you prefer. You really can't go forward with PHP/MySQL development before you get to know these basic concepts.
Was This Post Helpful? 1
  • +
  • -

#3 IxGRpPafwang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-January 13

Re: Get values out of database, update them and put them back with php

Posted 10 October 2013 - 03:58 PM

Sorry for not mentioning this but the reason I want to use PHP is because I want to do some more complex things with the values like using "if" statements and some of the added values might be dependent on other values, for example from another table. Would these things be doable with Mysql?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Get values out of database, update them and put them back with php

Posted 10 October 2013 - 04:29 PM

Atli gave you a perfectly cromulent tutorial link. You should go read it.
Was This Post Helpful? 1
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3227
  • View blog
  • Posts: 10,837
  • Joined: 12-December 12

Re: Get values out of database, update them and put them back with php

Posted 10 October 2013 - 04:34 PM

The OP might embiggen his knowledge by following the link.
Was This Post Helpful? 0
  • +
  • -

#6 IxGRpPafwang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-January 13

Re: Get values out of database, update them and put them back with php

Posted 11 October 2013 - 07:15 AM

I get the logic behind it now. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1