SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Nothing is recorded.

    I have tried specifying all the columns of the table. However, nothing is being captured in the table when I hit the "copy" button.

    Here is my code.

    demotable.php (here is where all the data will be displayed after a form submission)
    Code:
    <?php 
    require('connect.php');
    $query = "SELECT * FROM trade_history1 "; //You don't need a ; like you do in SQL
    $result = mysql_query($query);
    
    echo "<table border = '1px'>"; // start a table tag in the HTML
    echo "<tr><td>" . "ID" . "</td><td>" . "Date" . "</td><td>" . "Type" . "</td><td>" . "Size" . "</td><td>" . "Currency Pair" . "</td><td>" . "Entry" . "</td><td>" . "Stoploss" . "</td><td>". "Take Profit" . "</td><td>" . "Date Close" . "</td><td>" ."Close" . "</td><td>" ."Profit/Loss"."</td><td>" ."IDKWHAISTHIS"."</td><td>" ."Copy"."</td></tr>" ;  //$row['index'] the index here is a field name
    
    while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
    echo "<tr><td>" . $row['id'] . "</td><td>" . $row['date'] . "</td><td>" . $row['type'] . "</td><td>" . $row['size'] ."</td><td>" . $row['type'] ."</td><td>" . $row['currency_pair'] ."</td><td>" . $row['entry'] ."</td><td>" . $row['stoploss'] ."</td><td>" . $row['takeprofit'] ."</td><td>" . $row['dateclose'] ."</td><td>" . $row['close'] ."</td><td>" . $row['profitloss'] . "</td></tr>";  //$row['index'] the index here is a field name
    }
    
    echo "</table>"; //Close the table in HTML
    
    mysql_close(); //Make sure to close out the database connection
    ?>
    
    <html>
    <form method = "GET" action = "copytrade.php"><input type = "submit" name = "copy" value = "copy"/></form>
    </html>
    copytrade.php (Here is where I try to retrieve the entered data and duplicate it into the same table)

    Code:
    <?php
    
    require ('connect.php');
    
    $data = "INSERT INTO trade_history1 (size, date, type, id, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss) 
                            SELECT size, date, type, id, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss
                            FROM trade_history1
                               WHERE id = ''";
    
    $db = mysql_query($data);
    
    while($row = mysql_fetch_array($db)){   //Creates a loop to loop through results
    echo "<tr><td>" . $row['id'] . "</td><td>" . $row['date'] . "</td><td>" . $row['type'] . "</td><td>" . $row['size'] ."</td><td>" . $row['type'] ."</td><td>" . $row['currency_pair'] ."</td><td>" . $row['entry'] ."</td><td>" . $row['stoploss'] ."</td><td>" . $row['takeprofit'] ."</td><td>" . $row['dateclose'] ."</td><td>" . $row['close'] ."</td><td>" . $row['profitloss'] . "</td></tr>";  //$row['index'] the index here is a field name
    }
    
    mysql_close();
    ?>
    Much help is needed as I am really new to php and mysql. Thanks.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2012
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When someone clicks on the copy button on the demotable.php, they are sent to the copytrade.php page, but the url would look like this: copytrade.php?copy=copy because that's the data being sent. In order to send the data to retrieve, you must create a form entry for each field you want. Then on the copytrade.php page, use the $_GET variable to access the data.
    Also, in order to insert data into the database with a query, it goes like this:
    Code:
    $data = "INSERT INTO trade_history1 (size, date, type, id, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss) VALUES ($_GET['size'], $_GET['date'], etc.)";

  3. #3
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for your reply

    May I know what do you mean by "create a form entry for each field"?? Do you mean like this?

    demoform.php

    PHP Code:
    <?php
    require('connect.php');

    $size          $_GET['size'];//store variable
    $date          $_GET['date'];//store variable
    $type          $_GET['type'];//store variable
    $id           $_GET['id'];
    $currencypair $_GET['currency_pair'];//store variable
    $entry          $_GET['entry'];//store variable
    $stoploss     $_GET['stoploss'];//store variable
    $takeprofit   $_GET['takeprofit'];//store variable
    $dateclose    $_GET['dateclose'];//store variable
    $close        $_GET['close'];//store variable
    $profitloss   $_GET['profitloss'];//store variable


    $sql "INSERT INTO trade_history1 (size, date, type, id, currency_pair, entry, stoploss, takeprofit, dateclose, close, profitloss) VALUES ('$size','$date','$type','$id','$currencypair','$entry','$stoploss','$takeprofit','$dateclose','$close', '$profitloss')";//store valuessss

    if (!mysql_query($sql)) {
        die(
    'Error: ' mysql_error());//
    }


    mysql_close();
    ?>
    And I have tried the INSERT INTO ... VALUES statement. However, there is an error. Do you mean that if I have the INSERT INTO... VALUES statement, I can remove my SELECT ... FROM statement?

    Thanks!
    Last edited by SpacePhoenix; Aug 6, 2013 at 07:51. Reason: placed php tags around php code

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    No, he means you will need to create a form of some description that sends the values you have from the first query - actually you just need 1; the ID number of the record you are copying.
    Although I do have to question WHY you are inserting another identical row instead of updating the existing row.

    Could you explain the logic behind it and what is happening?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would you mind giving me an example? I have very little knowledge on php and mysql. The purpose of inserting another identical row is because I want to allow my users to "copy trades". So, say User A executed a trade and User B wants to follow, User B will just have to simply click on "copy".

  6. #6
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,028
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xxx12345 View Post
    Hi, thanks for your reply

    May I know what do you mean by "create a form entry for each field"?? Do you mean like this?

    demoform.php

    PHP Code:
    <?php
    require('connect.php');

    $size          $_GET['size'];//store variable
    $date          $_GET['date'];//store variable
    $type          $_GET['type'];//store variable
    $id           $_GET['id'];
    $currencypair $_GET['currency_pair'];//store variable
    $entry          $_GET['entry'];//store variable
    $stoploss     $_GET['stoploss'];//store variable
    $takeprofit   $_GET['takeprofit'];//store variable
    $dateclose    $_GET['dateclose'];//store variable
    $close        $_GET['close'];//store variable
    $profitloss   $_GET['profitloss'];//store variable


    $sql "INSERT INTO trade_history1 (size, date, type, id, currency_pair, entry, stoploss, takeprofit, dateclose, close, profitloss) VALUES ('$size','$date','$type','$id','$currencypair','$entry','$stoploss','$takeprofit','$dateclose','$close', '$profitloss')";//store valuessss

    if (!mysql_query($sql)) {
        die(
    'Error: ' mysql_error());//
    }


    mysql_close();
    ?>
    And I have tried the INSERT INTO ... VALUES statement. However, there is an error. Do you mean that if I have the INSERT INTO... VALUES statement, I can remove my SELECT ... FROM statement?

    Thanks!
    That script is vulnerable to SQL Injection, any data submitted by the user in any way must always be considered to be dangerours until it has been sanitized. Also the mysql_* extension is deprecated as of version 5.5.x of PHP you should be migrating over to using either the mysqli_* extension or to PDO (in either case you should make use of prepared statements).
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  7. #7
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean like this?

    Code:
    $stmt = $mysqli->prepare("INSERT INTO trade_history1 (size, date, type, id, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss) 
                                                            SELECT size, date, type, id, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss
                                                            FROM trade_history1
                                                            WHERE id = '?'"); 
    		
    		$stmt->bind_param("i", $id); // 1 ? so we use i. we use i because  id is INT
    		
    		
    		$successfullyCopied = $stmt->execute(); 
    		
    	
    		$stmt->close();


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •