1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php script does not add data to the mysql database

Discussion in 'PHP' started by adelevr, Jan 17, 2009.

  1. #1
    I'm new to php and have searched extensively but need some help.

    My script should add a new record to the database, but it only increments the id and leaves the rest blank.
    I have also checked permissions and am sure that they are correct. I know can work with the db with php scripts because I created the tables using a script.

    oh, this is the same script that I used before I had to re-install (suse 11.1) and the only thing that has changed is that I had to recreate the database. It worked before.

    here is the code:

    <?php
    include ('dbconnect.php');

    if (isset($_REQUEST['Submit']))
    {
    # THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE
    $sql = "INSERT INTO $db_table(fname,lname) values ('$fname','$lname')";
    if($result = mysql_query($sql ,$db))
    {
    echo "Thank you, a new author has been entered into our database<br><br>";
    }
    else
    {
    echo "ERROR: ".mysql_errno() . ':' . mysql_error();
    }

    }
    else

    ?>
    <form method="post" action="">
    First Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="text" name="fname">
    <br><br>
    Last Name: &nbsp;&nbsp;&nbsp;&nbsp;
    <input type="text" name="lname">
    <br><br>
    <input type="submit" name="Submit" value="Submit">
    </form>
     
    adelevr, Jan 17, 2009 IP
  2. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try :
    
    include ('dbconnect.php');
    $lname=$_REQUEST['lname'];
    $fname=$_REQUEST['fname'];
    ...
    
    PHP:
    The stuff you post with the form is stored in the $_REQUEST array, under the forms field name. If you want access to the posted data you have to retrieve it from that $_REQUEST array.

    http://programmingtutorials.wordpress.com/2008/03/04/php-_request/

    If that don't work, would you mind posting the dbconnect.php file code (without the password etc. ;) ), we can't see what's in there ?
     
    juust, Jan 17, 2009 IP
  3. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #3
    yea, try above or use something like this:

    if(isset($_POST['Submit'])) {
    $fname = $_POST['fname'];
    $lname = $_POST['lname '];
    .........
     
    khan11, Jan 17, 2009 IP
  4. adelevr

    adelevr Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Excellent! That did the trick, thanx.
     
    adelevr, Jan 17, 2009 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    Danltn, Jan 17, 2009 IP
  6. immortal_king

    immortal_king Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi,

    Smilarly I have the same problem where the Id gets incremented but does not updte the Databse and only creates a row. Wherever VARCHAR given fills with fields name.

    Input fields are created by javascript if enrtry rate and exit rates are provided. rest of the fields are to completed by the user.


    TABLE STRUCTUE
    ===============

    -- phpMyAdmin SQL Dump
    -- version 3.2.0.1
    -- http://www.phpmyadmin.net
    --
    -- Host: localhost
    -- Generation Time: Feb 06, 2011 at 11:38 AM
    -- Server version: 5.1.36
    -- PHP Version: 5.3.0

    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

    --
    -- Database: `onlyprofits`
    --

    -- --------------------------------------------------------

    --
    -- Table structure for table `2010_nifty`
    --

    CREATE TABLE IF NOT EXISTS `2010_nifty` (
    `trade_id` int(10) NOT NULL AUTO_INCREMENT,
    `contract` varchar(10) NOT NULL,
    `lot` int(10) NOT NULL,
    `trade_size` int(3) NOT NULL,
    `capital` decimal(10,2) NOT NULL,
    `trade` varchar(20) NOT NULL,
    `entry_date` date NOT NULL,
    `entry_rate` decimal(10,2) NOT NULL,
    `exit_date` date NOT NULL,
    `exit_rate` decimal(10,2) NOT NULL,
    `points` decimal(10,2) NOT NULL,
    `profit_loss` decimal(10,2) NOT NULL,
    `percentage` decimal(10,2) NOT NULL,
    `trade_cost` decimal(10,2) NOT NULL,
    `net_pl` decimal(10,2) NOT NULL,
    `net_percent` decimal(10,2) NOT NULL,
    `result` varchar(20) NOT NULL,
    PRIMARY KEY (`trade_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

    --
    -- Dumping data for table `2010_nifty`
    --

    INSERT INTO `2010_nifty` (`trade_id`, `contract`, `lot`, `trade_size`, `capital`, `trade`, `entry_date`, `entry_rate`, `exit_date`, `exit_rate`, `points`, `profit_loss`, `percentage`, `trade_cost`, `net_pl`, `net_percent`, `result`) VALUES
    (1, 'JAN', 50, 0, '0.00', 'BOUGHT', '2011-01-02', '5700.00', '2011-01-08', '5750.00', '50.00', '5000.00', '7.02', '0.00', '0.00', '0.00', ''),
    (2, 'contract', 0, 0, '0.00', 'trade', '0000-00-00', '0.00', '0000-00-00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', 'result'),
    (3, 'contract', 0, 0, '0.00', 'trade', '0000-00-00', '0.00', '0000-00-00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', 'result'),
    (4, 'contract', 0, 0, '0.00', 'trade', '0000-00-00', '0.00', '0000-00-00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', 'result'),
    (5, 'contract', 0, 0, '0.00', 'trade', '0000-00-00', '0.00', '0000-00-00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', 'result'),
    (6, 'contract', 0, 0, '0.00', 'trade', '0000-00-00', '0.00', '0000-00-00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', 'result');


    ===============
    HTML PAGE:
    ===============

    <?php

    mysql_connect("localhost", "onlyprofits", "10111953JB") or die(mysql_error());

    mysql_select_db("onlyprofits") or die(mysql_error());





    if(isset($_POST['Submit'])) {
    $contract=$_REQUEST['contract'];
    $trade=$_REQUEST['trade'];
    $lot=$_REQUEST['lot'];
    $trade_size=$_REQUEST['trade_size'];
    $entry_date=$_REQUEST['entry_date'];
    $entry_rate=$_REQUEST['entry_rate'];
    $exit_date=$_REQUEST['exit_date'];
    $exit_rate=$_REQUEST['exit_rate'];
    $points=$_REQUEST['points'];
    $capital=$_REQUEST['capital'];
    $profit_loss=$_REQUEST['profit_loss'];
    $percentage=$_REQUEST['percentage'];
    $trade_cost=$_REQUEST['trade_cost'];
    $net_pl=$_REQUEST['net_pl'];
    $net_percent=$_REQUEST['net_percent'];
    $result=$_REQUEST['result'];

    # THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE
    mysql_query("INSERT INTO 2010_nifty (contract, trade, lot, trade_size, entry_date, entry_rate, exit_date, exit_rate, points, capital, profit_loss, percentage, trade_cost, net_pl, net_percent, result) VALUES ('contract', 'trade', 'lot', 'trade_size', 'entry_date', 'entry_rate', 'exit_date', 'exit_rate', 'points', 'capital', 'profit_loss', 'percentage', 'trade_cost', 'net_pl', 'net_percent', 'result'
    )");


    }
    ?>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>BOUGHT</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Page-Enter" content="blendTrans(Duration=2.0)">
    <script type="text/javascript" src="../includes/oodomimagerollover.js"></script>
    <link href="styles.css" rel="stylesheet" type="text/css">
    <link href="../includes/old_style.css" rel="stylesheet" type="text/css">
    <link href="../includes/layout.css" rel="stylesheet" type="text/css" />
    <link href="../includes/old_style.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
    body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
    }
    </style>
    </head>

    <body>


    <div align="center"><form action="2010_nifty_bought-forum.php" method="post" name="myform" id="myform"><table border="0">
    <tr>
    <td><div align="center">Contract</div></td>
    <td><div align="center">Trade</div></td>
    <td><div align="center">Lot</div></td>
    <td><div align="center">Lots Bought</div></td>

    </tr>
    <tr>
    <td><div align="center"><input name="contract" type="text" id="contract" class="textarea"></div></td>
    <td><div align="center"><input name="trade" type="text" id="trade" class="textarea" value="BOUGHT"></div></td>
    <td><div align="center"><input name="lot" type="text" id="lot" class="textarea" value="50"></div></td>
    <td><div align="center"><input name="trade_size" type="text" id="trade_size" class="textarea" value="1" onFocus="this.value=''"></div></td>
    </tr>
    <tr>


    <td><div align="center">Entry Date</div></td>
    <td><div align="center">Entry Rate</div></td>
    <td><div align="center">Exit Date</div></td>
    <td><div align="center">Exit Rate</div></td>
    </tr>
    <tr>

    <td><div align="center"><input name="entry_date" type="text" id="entry_date" class="textarea"></div></td>
    <td><div align="center"><input name="entry_rate" type="text" id="entry_rate" class="textarea"></div></td>
    <td><div align="center"><input name="exit_date" type="text" id="exit_date" class="textarea"></div></td> <td><div align="center"><input name="exit_rate" type="text" id="exit_rate" class="textarea"></div></td>
    </tr>
    <tr>


    <td><div align="center">Points</div></td>
    <td><div align="center">Capital</div></td>
    <td><div align="center">Profits/Loss</div></td>
    <td><div align="center">Percentage</div></td>
    </tr>
    <tr>


    <td><div align="center"><input name="points" type="text" id="points" class="textarea"></div></td>
    <td><div align="center"><input name="capital" type="text" id="capital" class="textarea"></div></td>
    <td><div align="center"><input name="profit_loss" type="text" id="profit_loss" class="textarea"></div></td>
    <td><div align="center"><input name="percentage" type="text" id="percentage" class="textarea"></div></td>
    </tr>
    <tr>

    </tr>

    <tr>
    <td><div align="center">Trade Cost</div></td>
    <td><div align="center">Net Profit / Loss</div></td>
    <td><div align="center">Net Percentage</div></td>
    <td><div align="center">Type</div></td>
    </tr>
    <tr>
    <td><div align="center"><input name="trade_cost" type="text" id="trade_cost" class="textarea"></div></td>
    <td><div align="center"><input name="net_pl" type="text" id="net_pl" class="textarea" /></div></td>
    <td><div align="center"><input name="net_percent" type="text" id="net_percent" class="textarea" /></div></td>
    <td><div align="center"><input name="result" type="text" id="result" class="textarea" value="Profit" onFocus="this.value=''"></div></td>
    </tr>
    <tr>

    <td colspan="5"><div align="center">&nbsp;</div></td>
    </tr>
    <tr>

    <td colspan="5"><div align="center">
    <input type="submit" name="Submit" value="Submit">
    </div></td>
    </tr>
    </table>
    </form>
    <script type="text/javascript">
    function addEvent(els, type, func) {
    if(!(els instanceof Array)) els = [els];
    for(var i=0, n=els.length; i<n; ++i) {
    if(els.addEventListener) els.addEventListener(type, func, false);
    else els.attachEvent("on"+type, func);
    }
    }

    String.prototype.trim = function() {
    return this.replace(/^\s+/, "").replace(/\s+$/, "");
    };
    var trade = document.getElementById("trade");
    var trade_size = document.getElementById("trade_size");
    var lot = document.getElementById("lot");
    var capital = document.getElementById("capital");
    var entry_rate = document.getElementById("entry_rate");
    var exit_rate = document.getElementById("exit_rate");
    var points = document.getElementById("points");
    var profit_loss = document.getElementById("profit_loss");
    var percentage = document.getElementById("percentage");
    var trade_cost = document.getElementById("trade_cost");
    var net_pl = document.getElementById("net_pl");
    var net_percent = document.getElementById("net_percent");

    addEvent(
    [trade, entry_rate, exit_rate],
    "keyup",
    function(e) {
    var vtrade = trade.value.trim();
    var vtrade_size = trade_size.value.trim()
    var ventry_rate = entry_rate.value.trim();
    var vexit_rate = exit_rate.value.trim()
    var vlot = lot.value.trim()
    var vcapital = capital.value.trim()
    var nentry_rate = ventry_rate*1;
    var nexit_rate = vexit_rate*1;
    if(ventry_rate=="" || vexit_rate=="" || isNaN(nentry_rate) || isNaN(nexit_rate)) {
    points.value = percentage.value = "";
    return;
    }


    (points.value = nexit_rate - nentry_rate);
    capital.value = (nentry_rate * vlot) * 25 / 100;
    profit_loss.value = ((points.value) * vlot) * vtrade_size;
    percentage.value = (profit_loss.value) * 100 / vcapital;
    trade_cost.value = ((((nentry_rate * vlot) * 0.00004) + 9.9324) + (((nexit_rate * vlot) * 0.00004) + ((nexit_rate * vlot) * 0.00017) + 9.9324)) * vtrade_size;
    net_pl.value = (profit_loss.value) - (trade_cost.value);
    net_percent.value = (net_pl.value) * 100 / vcapital;
    }
    );
    </script></div>

    </body>
    </html>

    ============
     
    immortal_king, Feb 6, 2011 IP
  7. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #7
    drop isset but do it this way

    if(!empty($_POST['submit']))
     
    spaceman12, Feb 6, 2011 IP
  8. thewantedhost

    thewantedhost Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If I were you, I would take your database details out of that post!?
     
    thewantedhost, Feb 6, 2011 IP
  9. immortal_king

    immortal_king Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hi spaceman,

    Thank you for your suggestion ... but this too did not help me. I neither incremented a row nor posted the data in database.

    please help
     
    immortal_king, Feb 9, 2011 IP