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



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

trying create simple cumulative add-on script in php (or js):

1) come in integer(4 digits or less), click submit, number entered displayed , saved on same web page

2) come in number, click submit, number entered added previous number , total saved , displayed on web page

repeat …….

example: mantra counter @ garchen.net

below code have far in index.php:

<form method="post" action= "process-mantra-form-ami.php" > <p><strong>amitabha 1000000 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 mantra" name="b1"><br> <span id="mani">amitabha mantra count: <?php echo $newvalue; ?> </span> <p></p> </div> </form>

i getting confused form processing php. im attempting utilize local mamp server db. create connection, create database, , table, insert form info table, , retrieve info index.php @ same time in process-mantra-form-ami.php file?

you guys made seem easy in lastly post, there seems lot it. know code below incomplete , not quite correct. help!

process-mantra-form-ami.php code below

<?php // create connection $con=mysqli_connect("localhost:8888","root","root","my_db"); // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } // escape variables 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" 1 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 info table $sql="insert 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); ?> <div> <h2>thank <?php echo $num; ?> amitabha mantras!</h2> <p>remember dedicate merit.</p> <p><a href="index.php">return main site</a></p> </div>

try out... (sorry, bored tonight)

http://php.net/manual/en/book.mysqli.php http://php.net/manual/en/mysqli.quickstart.prepared-statements.php $conn->query($sql) $conn->prepare($sql) $conn->error http://php.net/manual/en/class.mysqli-stmt.php $stmt->bind_param('ss',$val1,$val2)

$stmt->bind_result($res1,$res2)

http://php.net/manual/en/mysqli.construct.php

<?php $host = 'localhost'; // localhost:8888 $user = 'root'; $pass = ''; // root $dbnm = 'test'; $conn = mysqli_connect($host,$user,$pass,$dbnm) or die('error ' . $conn->connect_error); // testing.... can run code on , on 1 time again , not // errors things existing , stuff run_statement($conn,"drop database if exists `my_db`;",'cleared old db'); run_statement($conn,"drop table if exists `mantras`;",'cleared old table'); run_statement($conn,"drop table if exists `two_col_table`;",'cleared old table'); // create database $sql = 'create database my_db'; $err = run_statement($conn,$sql,'database creation'); if (!$err) $conn->select_db('my_db'); // create table "mantras" 1 column 'num' $sql = 'create table mantras (num int)'; $err = run_statement($conn,$sql,'table mantras'); if (!$err) { $sql = 'insert mantras (num) values ( ? )'; $stmt = $conn->prepare($sql); $stmt->bind_param('d',$num); // d digit s (string) work $num = 1; $stmt->execute(); $num = 2; $stmt->execute(); $stmt->close(); echo ($conn->error) ? "insert errored: {$conn->error}" : 'insert ran succesfully'; // update database $sql = 'update mantras set num = num + 1'; run_statement($conn,$sql,'update database'); } // create table "test" 2 columns $sql = 'create table two_col_tbl (num int, txt varchar(10))'; $err = run_statement($conn,$sql,'table two_col_tbl'); if (!$err) { // demonstrating how bind multiple values $sql = 'insert two_col_tbl values ( ?, ? )'; $stmt = $conn->prepare($sql); $stmt->bind_param('ds',$num,$txt); $num = 1; $txt = 'hello'; $stmt->execute(); $num = 2; $txt = 'world'; $stmt->execute(); $stmt->close(); // select statement $sql = 'select num, txt two_col_tbl'; $stmt = $conn->prepare($sql); $stmt->bind_result($db_num, $db_txt); $stmt->execute(); print '<table><tr><th colspan=2>two_col_tbl</tr><tr><th>num</th><th>txt</th></tr>'; while ($stmt->fetch()) { print "<tr><td>$db_num</td><td>$db_txt</td></tr>"; } print '<table>'; $stmt->close(); } $conn->close(); function run_statement($conn,$statement,$descr) { if ($conn->query($statement)) echo "$descr ran successfully"; else echo "$descr failed: {$conn->error}"; homecoming $conn->error; } ?> <div> <h2>thank <?php echo $num; ?> amitabha mantras!</h2> <p>remember dedicate merit.</p> <p><a href="index.php">return main site</a></p> </div>

php sql cumulative-sum

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -