Why is this MySQL INSERT INTO running twice?

Posted by stuboo on Stack Overflow See other posts from Stack Overflow or by stuboo
Published on 2010-04-19T20:23:15Z Indexed on 2010/04/19 20:33 UTC
Read the original article Hit count: 192

Filed under:
|

I'm attempting to use the mysql insert statement below to add information to a database table. When I execute the script, however, the insert statement is run twice.

Here's the URL mysite.com/save.php?Body=p220,c180

Thanks in advance.

<?php
//tipping fees application
require('base.inc.php');
require('functions.inc.php');

// connect to the database & save this message there

try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);


//$number = formatPhone($_REQUEST['From']);
//if($number != 'xxx-xxx-xxxx'){die('SMS from unknown number');} // kill this if from anyone but mike

$message = $_REQUEST['Body'];
//$Sid = $_REQUEST['SmsSid'];
$now = time();
echo $message;

$message = explode(",",$message);
echo '<pre>';
print_r($message);
echo 'message count = '.count($message);
echo '</pre>';

$i = 0;
$j = count($message);

while($i<$j){
    $quantity =$message[$i];
    $material = substr($quantity, 0, 1);
    $amount = substr($quantity, 1);

    switch ($material) {
        case 'p':
            $m = "paper";
            break;
        case 'c':
            $m = "containers";
            break;
        default:
            $m = "other";
        }

        $count = $dbh->exec("INSERT INTO tippingtotals(sid,time,material,weight) VALUES('$i+$j','$now','$m','$amount')");
        echo $count;
        echo '<br />';

    $i++;
    }



//close the database connection 
    $dbh = null;
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql