Jump to content

PHP Update Mysql


larclem

Recommended Posts

Hello,

 

I have a script that grabs data from a website and enters it into mysql.  It grabs almost like a raw txt file.  Here is an example of my code and then I'll explain what I want to do.

 

<form action="" method="GET">
       <small>Please enter a username:</small> <br>
    <input type="text" name="user" /> <br>
    <input type="submit" value="Search" name="submit" /> <br>
</form>

<?php
function showInfo() {
    $user = $_GET['user'];
//then connect as user
   //change user and password to your mySQL name and password
  mysql_connect("localhost","username","password"); 

//select which database you want to edit
mysql_select_db("table");

    $file = @file_get_contents('http://getstat.mysite.com/index_lite.ws?player=' . $user);
    $explode = explode("\n", $file);
    $implode = implode(",", $explode);
    $stats = explode(",", $implode);
  	






echo '<b>' . $user . '</b>' . '<br>';
    echo 'Rank: ' . $stats[0] . '<br>';
    echo 'Total Level: ' . $stats[1] . '<br>';
   

}

 

There are various other information that I am grabbing, but just wanted to list a low down on what I am doing.  I have another part that adds this info to mysql once you search for a certain user.  What I would like to do since I have about 200 users or more that I need to update, is have a query where I can do the example above but update if the user values if they exist in the database.  I have tried several methods.  I think I may be doing it wrong.

 

$sql = "UPDATE stats SET total='$stats[1]', overall='$stats[0]' where username = '$user'
";

      $result = mysql_query($sql);

Link to comment
https://forums.phpfreaks.com/topic/200605-php-update-mysql/
Share on other sites

for this to work:

$sql = "UPDATE stats SET total='$stats[1]', overall='$stats[0]' where username = '$user'

you will need to connect the variables to the statement using "." eg.

$sql = "UPDATE stats SET total='.$stats[1].', overall='.$stats[0].' where username = \''.$user'\'';

I also quoted your  $user variable as it looks as though it is a sting.

if you can run the above query using the following:

mysql_query($query) or die (mysql_error();

and let us know what, if any, errors come back it would help a lot.

Link to comment
https://forums.phpfreaks.com/topic/200605-php-update-mysql/#findComment-1052695
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.