Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    cxn
    cxn is offline
    New Coder
    Join Date
    Aug 2009
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Adding array items to database

    Hi all

    I'm pretty new to php/mysql, which causes me to get stuck .. everywhere.

    I have a form to input a string. I used the explode function to cut the string in parts (array items). Each item is a username from a site I'm active on.

    My database table has 2 columns, "username" and "last checked".

    What I want is that each username gets put in a new row of the table.

    I'm assuming a foreach loop around the array would do the trick, but I have no success this far. I've been googling a lot on how to put an array into 1 column of a database, but found practically nothing that helped.

    Thanks for your time.

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,386
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    Show us the PHP script you have so far ... with the explode stuff and
    what you do with connecting to MySQL (but delete your password so we can't see it).

    I'm also guessing that each username gets a new row, but not if the name
    already exists? In that case, the username is not given a new row?

    So there's some decisions to make along with populating your table.

    We're interested to see what you've already done.

  • #3
    Regular Coder
    Join Date
    Jul 2003
    Posts
    117
    Thanks
    0
    Thanked 17 Times in 17 Posts
    First you need to learn
    * Database basics
    * SQL
    * PHP

    Then go through some tutorials:
    registration form using PHP

    login form using PHP

  • #4
    cxn
    cxn is offline
    New Coder
    Join Date
    Aug 2009
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Show us the PHP script you have so far ... with the explode stuff and
    what you do with connecting to MySQL (but delete your password so we can't see it).

    I'm also guessing that each username gets a new row, but not if the name
    already exists? In that case, the username is not given a new row?

    So there's some decisions to make along with populating your table.

    We're interested to see what you've already done.
    Hi, here's the code;
    Code:
    $inputarray = explode(" - ", $inputcut);
    
    $inputarray = array_diff($inputarray, array(array_pop($inputarray)));
    
    
    foreach ($inputarray as $value) {
    echo $value . "<br />"; }
    
    
    $con = mysql_connect('localhost','user','password')
    or die('Connecting to database failed.');
    mysql_select_db('cxn_project',$con);
    
    
    $data = '';
    foreach ($inputarray as $value) {
        $data .= "('$value'),";
    }
    $data = rtrim($data, ',');
    if (mysql_query("INSERT INTO online (Username) VALUES $data")) {
        echo "Data inserted!";
    } 
    
    
    
    $q2 = mysql_query($query);
    
    if (!$q2)
      {
      echo ('Q2 failed: ' . mysql_error());
      }
     else {echo 'success Q2';}

    I got it to work! Query works and it get put in my table :-)

    I doubt this is the most efficient way to code it. Though currently it works.

    You're right that I want to overwrite existing username rows, but new ones should be added. That's wherer I'm stuck now

  • #5
    cxn
    cxn is offline
    New Coder
    Join Date
    Aug 2009
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by cxn View Post
    I got it to work! Query works and it get put in my table :-)

    I doubt this is the most efficient way to code it. Though currently it works.

    You're right that I want to overwrite existing username rows, but new ones should be added. That's wherer I'm stuck now


    I guess I can use the UPDATE command? Though I think I need to loop through the rows first.. or not?

  • #6
    cxn
    cxn is offline
    New Coder
    Join Date
    Aug 2009
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Is what I want even possible? Thanks

  • #7
    New to the CF scene
    Join Date
    Aug 2009
    Location
    Colorado
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if you use the UPDATE statement in your SQL query the database will not add a new row, IE your table will not grow. UPDATE will only update a existing database row and has a different query structure.

    exactly what kind of input are you putting into the database? do you want every word to be a new line (row) of the database or do you want the database to take the input as an entire row? I can't wrap my head around why you would break up the input.

    you can use GETDATE() to plug the current timestamp into your database then use date() to form it in anyway you see fit.

  • #8
    New Coder
    Join Date
    Aug 2009
    Posts
    26
    Thanks
    1
    Thanked 3 Times in 3 Posts
    try doing this

    PHP Code:
    if(!mysql_query($UPDATE_sql_statement)){
    mysql_query($INSERT_sql_statement);


  • #9
    cxn
    cxn is offline
    New Coder
    Join Date
    Aug 2009
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by takenreality View Post
    if you use the UPDATE statement in your SQL query the database will not add a new row, IE your table will not grow. UPDATE will only update a existing database row and has a different query structure.

    exactly what kind of input are you putting into the database? do you want every word to be a new line (row) of the database or do you want the database to take the input as an entire row? I can't wrap my head around why you would break up the input.

    you can use GETDATE() to plug the current timestamp into your database then use date() to form it in anyway you see fit.

    I got a string of users on line seperated by " - " (space, -, space). Eg Person1 - Person2 - Persosn3 ... That's why I want to seperate them. Each new db line has 1. the username, 2. the date I submitted them into the db.

    Thanks for the help, I'll be looking into it tonight and post my progress :-)


    @Tailer, thanks, seems so simple :-)

  • #10
    cxn
    cxn is offline
    New Coder
    Join Date
    Aug 2009
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tailender1 View Post
    try doing this

    PHP Code:
    if(!mysql_query($UPDATE_sql_statement)){
    mysql_query($INSERT_sql_statement);

    Hmm, do I have to insert a loop somewhere as well?

  • #11
    New Coder
    Join Date
    Aug 2009
    Posts
    26
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by cxn View Post
    Hmm, do I have to insert a loop somewhere as well?
    well the logic is to see whether update query is a success or not and if it is not successful then try to insert ..
    i think this is sufficient but there is no error handling implemented which you have to do it yourself...

  • #12
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php

    $inputarray 
    explode(" - "$inputcut);
    $inputarray array_diff($inputarray, array(array_pop($inputarray)));

    foreach (
    $inputarray as $value
    {
        echo 
    $value "<br />"; }
        
        
    $con mysql_connect('localhost','user','password') or die('Connecting to server failed.');
        
    mysql_select_db('cxn_project',$con) or die('Connecting to database failed.');
        
        
    $data '';
        foreach (
    $inputarray as $value
        {
            
    $data .= $value;
        }
        
        
    $data rtrim($data',');
        
    $query mysql_query("SELECT * FROM `online` WHERE Username = '$data'");
        if(
    mysql_num_rows($query)!=0)
        {
            echo 
    "User: ".$data." already exisits";
        }
        else
        {
            if (
    mysql_query("INSERT INTO `online` (Username) VALUES ('$data')")) 
            {
                echo 
    "Data inserted!";
            } 
            
            
    $q2 mysql_query($query);
            if (!
    $q2)
            {
                echo (
    'Q2 failed: ' mysql_error());
            }
            else
            {
                echo 
    'success Q2';}
            }
        }
    }
    ?>

  • #13
    cxn
    cxn is offline
    New Coder
    Join Date
    Aug 2009
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Phil Jackson View Post
    PHP Code:
    <?php

    $inputarray 
    explode(" - "$inputcut);
    $inputarray array_diff($inputarray, array(array_pop($inputarray)));

    foreach (
    $inputarray as $value
    {
        echo 
    $value "<br />"; }
        
        
    $con mysql_connect('localhost','user','password') or die('Connecting to server failed.');
        
    mysql_select_db('cxn_project',$con) or die('Connecting to database failed.');
        
        
    $data '';
        foreach (
    $inputarray as $value
        {
            
    $data .= $value;
        }
        
        
    $data rtrim($data',');
        
    $query mysql_query("SELECT * FROM `online` WHERE Username = '$data'");
        if(
    mysql_num_rows($query)!=0)
        {
            echo 
    "User: ".$data." already exisits";
        }
        else
        {
            if (
    mysql_query("INSERT INTO `online` (Username) VALUES ('$data')")) 
            {
                echo 
    "Data inserted!";
            } 
            
            
    $q2 mysql_query($query);
            if (!
    $q2)
            {
                echo (
    'Q2 failed: ' mysql_error());
            }
            else
            {
                echo 
    'success Q2';}
            }
        }
    }
    ?>

    Hi, thanks for the reply. I pretty much understand the coding, but it doesn't work unfortunately

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/cxn/public_html/project/usersadded.php on line 42
    Q2 failed: Query was emptyHeartBeat

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/cxn/public_html/project/usersadded.php on line 42
    Q2 failed: Query was emptyMonoxideChild

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/cxn/public_html/project/usersadded.php on line 42
    Q2 failed: Query was emptyNocturnal

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/cxn/public_html/project/usersadded.php on line 42
    Q2 failed: Query was emptyAngelsReturn

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/cxn/public_html/project/usersadded.php on line 42
    Q2 failed: Query was emptyCubest

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/cxn/public_html/project/usersadded.php on line 42
    Q2 failed: Query was emptyGReddy

    Those are the usernames I wanted to insert in my empty table.

    I did alter your code a bit cause it gave an "unexpected" error.

    This is what I have:

    PHP Code:
    <html>
    <style type="text/css">

    body{
    font-family:  Arial, Verdana,Helvetica, sans-serif;

    }

    </style> 
    </html>


    <?php 

    echo(date("l dS \of F Y h:i:s A") . "<br /><br />");

    $input $_POST['input'];

    $inputcut substr($input2);


     

    $inputarray explode(" - "$inputcut);
    $inputarray array_diff($inputarray, array(array_pop($inputarray)));

    foreach (
    $inputarray as $value
    {
        echo 
    $value "<br />"
        
        
    $con mysql_connect('localhost',' useer ',' pw ') or die('Connecting to server failed.');
        
    mysql_select_db('cxn_project',$con) or die('Connecting to database failed.');
        
        
    $data '';
        foreach (
    $inputarray as $value
        {
            
    $data .= $value;
        }
        
        
    $data rtrim($data',');
        
    $query mysql_query("SELECT * FROM 'online' WHERE Username = '$data'");
        if(
    mysql_num_rows($query)!=0)
        {
            echo 
    "User: ".$data." already exisits";
        }
        else
        {
            if (
    mysql_query("INSERT INTO 'online' (Username) VALUES ('$data')")) 
            {
                echo 
    "Data inserted!";
            } 
            
            
    $q2 mysql_query($query);
            if (!
    $q2)
            {
                echo (
    'Q2 failed: ' mysql_error());
            }
            else
            {
                echo 
    'success Q2';
            }
        }
    }
    ?>

    Thanks for your help!

  • #14
    cxn
    cxn is offline
    New Coder
    Join Date
    Aug 2009
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    bump :^$

  • #15
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    Can you give me an example of the input?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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