Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Trying to create a simple cumulative addition script in PHP (or JS):

Just trying to create a very simple cumulative addition script in PHP (or JS):

1)  enter any integer(4 digits or less), click submit,  number entered is displayed and saved on the same web page

2)  enter another number, click submit,  number entered is added to previous number and total is saved and displayed on the web page Repeat …….

Example:      the mantra counter at garchen.net

Thanks much, Mark

Can you post what you have so far? We need a place to start.

5 Answers

html
<form method="post" action= "process-mantra-form-ami.php" >
    <p><strong>Amitabha Million Mantra Accumulation: </strong><br></p>
    <div style="margin-left: 20px;">
        <p>OM AMI DEWA HRI</p>
        <input type="text" name="accumulation" size="10" maxlength="6">
        <input type="submit" value="Submit Your Mantra" name="B1"><br>
        <span id="mani">Amitabha Mantra Count: <?php echo $newValue; ?> </span>
        <p></p>
    </div>
</form>
php
// Create connection
$con=mysqli_connect("localhost:8888","root","root","my_db");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$accumulation = mysqli_real_escape_string($con, $_POST['accumulation']);

// Create database
$sql="CREATE DATABASE my_db";
if (mysqli_query($con,$sql)) {
  echo "Database my_db created successfully";
} else {
  echo "Error creating database: " . mysqli_error($con);
}

// Create table "Mantras" with one column 'Num'
$sql="CREATE TABLE Mantras (Num INT)";
if (mysqli_query($con,$sql)) {
  echo "Table mantras created successfully";
} else {
  echo "Error creating table: " . mysqli_error($con);
}

// Insert form data into table
$sql="INSERT INTO Mantras (Num INT)
VALUES ('$num')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}

// update database
mysqli_query($con,"UPDATE Mantra SET Num = num + 1");

}

mysqli_close($con);

Ok. Above on top, is my index.php form code, and below is my process-mantra-form-ami.php code. I know it is not complete or correct yet. Anyone care to offer some assistance? Thanks much, Mark

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hey Mark,

Is the total something that could be tracked based on numbers in DB or does it need to happen live on the page and is reset when you refresh the page?

Thanks much much for your response Zac. It's exactly like the example at the top of the page here: garchen.net

It happens live, on the page, as soon as the 'submit' button is clicked.