May 9, 201510 yr 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
May 9, 201510 yr //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 May 9, 201510 yr by Finland
May 9, 201510 yr Author 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? Edited May 9, 201510 yr by Tom
May 9, 201510 yr 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 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); }
May 9, 201510 yr Author Sorry 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?
May 9, 201510 yr 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.
May 9, 201510 yr Author 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.
May 9, 201510 yr 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 '";
May 9, 201510 yr Author 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 Edited May 9, 201510 yr by Tom
May 9, 201510 yr You can set a Cronjob to run the queries automaticly on specific time. http://www.thesitewizard.com/general/set-cron-job.shtml
May 9, 201510 yr I'll create a script later for everyone to use that will correctly insert/update and view data from a NoSQL or MySQL database using an API. Doing it right; do it once.
May 9, 201510 yr Author Tom I found this Found wat Im stuck trying to return an int from a column, been like 6 hours
May 9, 201510 yr Found wat Im stuck trying to return an int from a column, been like 6 hours Opps http://osbot.org/forum/topic/70713-dynamic-signature-php-snippet/ Edited May 9, 201510 yr by josedpay
Create an account or sign in to comment