How To Store Only 100 data to Database and How to updated data into database??

Discussion in 'PHP' started by Mac Weng, Jan 19, 2006.

  1. #1
    Hiiii...i need help about array and updated data int db using php and pear..could onyone help me...and also how to related to table with 1 id..please..

    
    <?php
    
    require 'db_connect.php';
    
    // require above script
    // change the path to match wherever you put it.
    
    
    $table = "CREATE TABLE users (
    id int UNSIGNED NOT NULL AUTO_INCREMENT, 
    username varchar(50),
    password varchar(50), 
    regdate varchar(20),
    email varchar(150),
    last_login varchar(20),
    PRIMARY KEY(id)
    )";
    
    $table = "CREATE TABLE info (
    id int UNSIGNED NOT NULL AUTO_INCREMENT  REFERENCES users(id),
    coord_x int(5),
    coord_y int(5),
    width_x int(5),
    height_y int(5),
    username varchar(50),
    PRIMARY KEY(id)
    )";
    
    
    $create = $db_object->query($table);	//perform query
    
    if(DB::isError($create)) {
    	die($create->getMessage());
    } else {
    	echo 'Table created successfully.';
    
    }
    
    $db_object->disconnect();
    
    ?>
    
    PHP:
    below is how i insert data....

    
    <?php
    
    // database connect script.
    
    require 'db_connect.php';
    
    ?>
    
    <?php 
    
        $insert = "INSERT INTO info (
                id,
    			coord_x, 
    			coord_y,
    			width_x,
    			height_y,
    			username)
    			VALUES (
    			'".$_GET['id']."',
    			'".$_GET['xMousePos']."', 
    			'".$_GET['yMousePos']."', 
    			'".$_GET['xMousePosMax']."',
    			'".$_GET['yMousePosMax']."',
    			'".$_SESSION['username']."')";
    			
    
    	$add_member = $db_object->query($insert);
    
    	if (DB::isError($add_member)) {
    		die($add_member->getMessage());
    	}
    
    	$db_object->disconnect();
    ?>
    
    
    PHP:
    how can i store only 100 data from the insert coding above and how can i updated the data..please give me some example or help me..with code..
     
    Mac Weng, Jan 19, 2006 IP
  2. DavidAusman

    DavidAusman Peon

    Messages:
    399
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    To update a DB using PHP, you can use UPDATE what() SET(what). For example you are going to Update coord_x in table info. Then your sql will be:

    UPDATE coord_x SET info WHERE id = 'which row do you want to update'

    And for the relational DB, you can select multiple field from different table like

    SELECT table1.field1, table2.field2 FROM table1, table2 WHERE id = table2.id
     
    DavidAusman, Jan 19, 2006 IP
  3. Mac Weng

    Mac Weng Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3

    ??...how david....one more think...can i just put updated and insert same place...without any loop or anything...:confused:
     
    Mac Weng, Jan 19, 2006 IP
  4. DavidAusman

    DavidAusman Peon

    Messages:
    399
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Im not very clear! What do you mean by update and insert at the same place.

    If you dont loops, then you will have to click on the submit button for 100 times to insert those 100 data into DB. Use loop will save you time!
     
    DavidAusman, Jan 20, 2006 IP
  5. Triexa

    Triexa Active Member

    Messages:
    580
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #5
    Ya I don't quite follow either... could you please explain a bit clearer?
     
    Triexa, Jan 21, 2006 IP
  6. Mac Weng

    Mac Weng Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    below is the capture coordinate that will get value from insert coding above.

    
    <html> 
    <body> 
    
    <script type=text/javascript> 
    // Set Netscape up to run the "captureMousePosition" function whenever
    // the mouse is moved. For Internet Explorer and Netscape 6, you can capture
    // the movement a little easier.
    if (document.layers) { // Netscape
        document.captureEvents(Event.MOUSEMOVE);
        document.onmousemove = captureMousePosition;
    } else if (document.all) { // Internet Explorer
        document.onmousemove = captureMousePosition;
    } else if (document.getElementById) { // Netcsape 6
        document.onmousemove = captureMousePosition;
    }
    // Global variables
    xMousePos = 0; // Horizontal position of the mouse on the screen
    yMousePos = 0; // Vertical position of the mouse on the screen
    xMousePosMax = 0; // Width of the page
    yMousePosMax = 0; // Height of the page
    
    function captureMousePosition(e) {
        if (document.layers) {
            // When the page scrolls in Netscape, the event's mouse position
            // reflects the absolute position on the screen. innerHight/Width
            // is the position from the top/left of the screen that the user is
            // looking at. pageX/YOffset is the amount that the user has 
            // scrolled into the page. So the values will be in relation to
            // each other as the total offsets into the page, no matter if
            // the user has scrolled or not.
            xMousePos = e.pageX;
            yMousePos = e.pageY;
            xMousePosMax = window.innerWidth+window.pageXOffset;
            yMousePosMax = window.innerHeight+window.pageYOffset;
        } else if (document.all) {
            // When the page scrolls in IE, the event's mouse position 
            // reflects the position from the top/left of the screen the 
            // user is looking at. scrollLeft/Top is the amount the user
            // has scrolled into the page. clientWidth/Height is the height/
            // width of the current page the user is looking at. So, to be
            // consistent with Netscape (above), add the scroll offsets to
            // both so we end up with an absolute value on the page, no 
            // matter if the user has scrolled or not.
            xMousePos = window.event.x+document.body.scrollLeft;
            yMousePos = window.event.y+document.body.scrollTop;
            xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
            yMousePosMax = document.body.clientHeight+document.body.scrollTop;
        } else if (document.getElementById) {
            // Netscape 6 behaves the same as Netscape 4 in this regard 
            xMousePos = e.pageX;
            yMousePos = e.pageY;
            xMousePosMax = window.innerWidth+window.pageXOffset;
            yMousePosMax = window.innerHeight+window.pageYOffset;
        }
        top.leftFrame.location='simpan.php?xMousePos=' + xMousePos + ' ,yMousePos=' + yMousePos + ', xMousePosMax=' + xMousePosMax + ' ,yMousePosMax=' + yMousePosMax;
    
        return true; 
    
    }
    
    </script> 
    </body> 
    </html>
    
    PHP:

    what i need is...when i use the script above..it will capture as many as the mouse is moving..and it will insert to the DB....i need maybe to store only 100 data or less in db without modified the captured script...if it posible to use array and looping at the insert coding above?? and also now when it store the coordinate...it will store new data for same user many time...i mean if USER1 move the mouse it will store USER1 movement and if USER2 move it will store USER2 movement....but when one more timeUSER1 move the mouse...it will store the value below the USER2 value...not updated the value...

    above i will include also my DB screen shot...
    [​IMG]
    [​IMG]
     
    Mac Weng, Jan 22, 2006 IP
  7. DavidAusman

    DavidAusman Peon

    Messages:
    399
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    ho ho ho ... Javascript :)
    Sorry I know nothing much about JS :(

    But I know what you are trying to do.

    First, you will need to know whether USER1 had already exist in DB. If it does exist, your script will update. But if it does not exist, it will create a new row.
    This is just for example:
    
    <?php
    ///your connection
    $conn = mysql_connect("localhost", "username", "password");
                mysql_select_db("dbName");
    $sql = mysql_query("select * from tableNameOfYourUser where user = '".$user."'");
    $row = mysql_num_rows($sql);
    if($row > 0) {
         $sql = mysql_query("UPDATE tableNameOfYourUser SET fieldName = '".$coordinate."' where user = '".$user."'");
    }
    else {
         $values = "VALUES ('".$coordinate."')";
         $sql = mysql_query("INSERT INTO tableNameOfYourUser(coordinate) ". $values);
    }
    ?>
    
    PHP:
    But first you should use JavaScript to pass the variable to php.
     
    DavidAusman, Jan 25, 2006 IP
  8. stuw

    stuw Peon

    Messages:
    702
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #8
    as an aside you should try and clean your data before it goes into your sql query.

    If your expecting an integer for a field use something like
    $clean['mouse_x']=intval($_GET['mouse_x']);
    Then use If your expecting an integer for a field use something like
    $clean['mouse_x'] in your sql instead of $_GET['mouse_x']

    If your expecting something thats not an integer use
    $clean['mouse_x']=mysql_real_escape_string($_GET['mouse_x']);

    Otherwise someone could call your script with ?mouse_x='sql injection attack' and really ruin your day
     
    stuw, Jan 25, 2006 IP
  9. DavidAusman

    DavidAusman Peon

    Messages:
    399
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #9
    stuw: could you explain more about mysql injection. Im longing to understand more about this!

    How could someone query your DB by the method you explain above?
    Thanks
     
    DavidAusman, Jan 25, 2006 IP
  10. Mac Weng

    Mac Weng Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    emm...that i think i understand..but how can i past data for example 100 data when inserting and updated..because now it will store as many as i move the mouse and will stop when i am logout the page...and about the mysql injection...what is that and why i need use clean??:(

    one more thing b4 i forget.. i am using pear...how about that?? and also i have 2 table...1. users table and 2. info table..
     
    Mac Weng, Jan 26, 2006 IP
  11. DavidAusman

    DavidAusman Peon

    Messages:
    399
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #11
    DavidAusman, Jan 26, 2006 IP