Jump to content

Looking for PHP snippet for updating MYSQL data


Tom

Recommended Posts

Setting up a dynamic signature and know literally no PHP, was wondering if somebody had a php file that is correctly formatted and will

automatically add new entered data, etc.

 

E.g. Fish Caught was 30, next session person catches 3000 fish so it will add 3000 to 30, rather than just putting the new value there.

 

I've already got the signature making set up, just looking for this part now.

 

Any help is appreciated, thanks

  • Like 1
Link to comment
Share on other sites

//Simple insert-into database via mysqli by Kosti Wessman
<?php
//Connection details
$servername = "hostlelocal";
$username = "nameleuser";
$password = "wordlepass";
$dbname = "baseledata";

//Variable conn is used to determine the new mysql connection for above data
$conn = new mysqli($servername, $username, $password, $dbname);
//Checking if the connection is valid, if not, killing the process and throwing a error message
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
//Query for inserting the wanted data into the mysql - Where DynSig is the table name
$sql = "INSERT INTO DynSig (xp_gained, time_run, fish_caught)
VALUES ('value1', 'value2', 'value3')"; 
//Values 1, 2 and 3 are in order where xp_gained, time_run, fish_caught

//Closing the connection.. :P
$conn->close();
?>

This should do the trick. Just create variables for the value1, value2 and value3 to make it autoupdate from your script.

//Query for inserting the wanted data into the mysql - Where DynSig is the table name
$sql = "UPDATE DynSig SET value_name='valuehere' WHERE id='userid'";
//For the update you requested. misread first time
Edited by Finland
Link to comment
Share on other sites

Oh sweet man, thanks for this!

 

Doesnt UPDATE do the same as insert? Like you cant have 2 of the same primary key right?

 

Also, wouldnt I need to include 

query($sql)

Somewhere?

 

Sorry :D I was too fuzzy but here you go. Add this before the mysqli_close

if (mysqli_query($conn, $sql)) {
    echo "Record added to the Database";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
Link to comment
Share on other sites

 

Sorry biggrin.png I was too fuzzy but here you go. Add this before the mysqli_close

if (mysqli_query($conn, $sql)) {
    echo "Record added to the Database";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

 

As for calculating and adding differences e.g. fish caught will need to be incremented, that isnt automatic I'm assuming.

 

Would I have to read the database fields into variables, do the math then update?

Link to comment
Share on other sites

As for calculating and adding differences e.g. fish caught will need to be incremented, that isnt automatic I'm assuming.

 

Would I have to read the database fields into variables, do the math then update?

 

You could do something like this

 

  • View what $current value is
  • Inject $new_value
  • Sum up $current and $new_value
  • Insert $result in to the database

Let me know how this goes. 

<?php
$current = "30";
$new_value = "3000";
$result = $current + $new_value;

$sql = "INSERT INTO DynSig ('$result')
?>

Something like this would do the trick. Injecting the variables from your script will be left up to you.

Link to comment
Share on other sites

You could do something like this

 

  • View what $current value is
  • Inject $new_value
  • Sum up $current and $new_value
  • Insert $result in to the database

Let me know how this goes. 

<?php
$current = "30";
$new_value = "3000";
$result = $current + $new_value;

$sql = "INSERT INTO DynSig ('$result')
?>

Something like this would do the trick. Injecting the variables from your script will be left up to you.

 

The only problem I'm coming across now is actually getting the currrent value from the database, setting it shouldnt be an issue

 

Was thinking something like $current = SELECT xpgained IN Data WHERE id='user' obviously I don't know all the queries, and thats probably wrong.

Link to comment
Share on other sites

The only problem I'm coming across now is actually getting the currrent value from the database, setting it shouldnt be an issue

 

Was thinking something like $current = SELECT xpgained IN Data WHERE id='user' obviously I don't know all the queries, and thats probably wrong.

"SELECT * FROM table_name WHERE column_name=' value in column '"; 
Link to comment
Share on other sites

 

The only problem I'm coming across now is actually getting the currrent value from the database, setting it shouldnt be an issue

 

Was thinking something like $current = SELECT xpgained IN Data WHERE id='user' obviously I don't know all the queries, and thats probably wrong.

"SELECT * FROM table_name WHERE column_name=' value in column '"; 

 

 

Isnt *  the wildcard for ALL in SQL?

 

I still need to get the value from a field based on the users ID,

So would to assign it to a value, id have to run the query right

 

Something like

hGVmI.png

Edited by Tom
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...