SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    update table fields with array

    HI All,

    i'm not very good with arrays so I have to ask You guys....
    I have a variable number of rows for updating some entries in a mysql database

    PHP Code:
    <?
    while($rows mysql_fetch_array($result_rows)) {

    echo 
    "<h4>Image source : <input type=\"text\" name=\"image_url_1\" size=\"50\" value=\"".$rows['image_url_1']."\"/><br />
    Content :<br /><textarea name=\"content_rows_1\" cols=\"100\" rows=\"10\">"
    $rows['content_1']."</textarea></h4>";
    }
    ?>
    as you can see, the number of these 2 input fields depends on the mysql_fetch_array result. Later on I have to insert these 'words' (user inputs) into a database but I can't figure out how to propagate the the multiple 'image_url_1' variables.... I know it has to be an array of 'image_url_1[0]', 'image_url_1[1]', 'image_url_1[2]', etc. but how should I 'forward' them to the next page AND then insert them into the DBase..... ????
    Hope you'll now what I meant cause I'm getting confused...

    Full time ADMIN - art community
    Part time coder - dsign

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post the structure for your MySQL table in here?

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If i came to understand what you are actually wanting, simply create the array of the fields and take it as an array in the php too.
    PHP Code:
    while($rows mysql_fetch_array($result_rows)) {
        echo 
    "<h4>Image source : <input type=\"text\" name=\"image_url_1[]\" size=\"50\" value=\"" $rows['image_url_1'] . "\"/><br />
            Content :<br /><textarea name=\"content_rows_1[]\" cols=\"100\" rows=\"10\">"
    $rows['content_1']."</textarea></h4>";

    And in the posting script do something like this:
    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        
    $image_url $_POST['image_url_1']; # this will now be an array
        
    $content $_POST['content_rows_1']; 
        
    # check if the array count is at least of 1 or greater
        
    if(count($image_url) >= && count($content) == count($image_url)){
            for(
    $i 0$i count($image_url); $i++){
                
    $sql "INSERT INTO tblename(field1,field2) VALUES('" $image_url[$i] . "', '" $content[$i] . "')";
                
    mysql_query($sql) or die(mysql_error());
            }
        }

    Hope this will help you out there.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you rajug for your suggestion! Your solution wasn't functioning in my instance BUT it helped me to implement my own!!!! Thanks a lot.

    BTW here's the solution :

    at first page we have to output existing data from the database :
    PHP Code:
    <?
    $query   
    "SELECT COUNT(id) AS numrows FROM table_rows WHERE page = 'book'";
    $result  mysql_query($query) or die('Error, query failed. ' mysql_error());
    $row     mysql_fetch_array($resultMYSQL_ASSOC);
    $numrows $row['numrows'];

    for (
    $i=1$i<=$numrows$i++) {

    $query1   "SELECT * FROM table_rows WHERE page = 'book' AND id = '$i'";
    $result1  mysql_query($query1) or die('Error, query failed. ' mysql_error());
    $rows     mysql_fetch_array($result1MYSQL_ASSOC);

    echo 
    "<h4>Row : <br /><img src=\"../"$rows['image_url_1']. "\"><br /><br />Image source : <input type=\"text\" name=\"image_url_1[{$i}]\" size=\"50\" value=\"".$rows['image_url_1']."\"/><br />Content :<br /><textarea name=\"content_rows_1[{$i}]\" cols=\"100\" rows=\"10\">"$rows['content_1']."</textarea></h4>";
    }

    $_SESSION['nr_book_rows'] = $numrows;

    ?>
    and then the page to Update table rows :
    PHP Code:
    session_start();

             include 
    '../library/config.php';
             include 
    '../library/opendb.php';

    $id $_GET['id'];
    $page $_GET['page'];

    $nr_book_rows $_SESSION['nr_book_rows'];

    $title $_POST['title'];
    $content_1 $_POST['content_1'];
    $title_right $_POST['title_right'];
    $content_right_1 $_POST['content_right_1'];
    $label $_POST['label'];
    $link $_POST['link'];


    switch (
    $page) {

        case 
    "home":
        
    $query "UPDATE table_home SET
                                         title = '
    $title', 
                                        content_1 = '
    $content_1',
                                        title_right = '
    $title_right',
                                        content_right_1 = '
    $content_right_1'
                                        WHERE id = '
    $id' ";
        break;
        
        case 
    "book":
        
    $query "UPDATE table_book SET
                                         title = '
    $title', 
                                        content_1 = '
    $content_1',
                                        title_right = '
    $title_right',
                                        content_right_1 = '
    $content_right_1'
                                        WHERE id = '
    $id' ";
                                        
    foreach(
    $_POST['image_url_1'] as $key => $image_url_1){
      
        
    $content_rows_1 $_POST['content_rows_1'][$key];

                
    $sql "UPDATE table_rows SET
                                                content_1 = '
    $content_rows_1',
                                                image_url_1 = '
    $image_url_1'
                                                WHERE id = "
    .$key .  " AND page = 'book'";
                
    mysql_query($sql) or die(mysql_error());
        }
        break;
        
        }

    mysql_query($query) or die('Error, query failed. ' mysql_error());
    ?> 
    hope it will help others too.

    Regards

    Full time ADMIN - art community
    Part time coder - dsign

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Feel the little difference here:
    Code php:
    $result = "SELECT * FROM table_rows WHERE page='book'";
    if(mysql_num_rows($result) >= 1){
    	while($rows = mysql_fetch_assoc($result)){
    		echo "<h4>Row : </h4><br />
    				<img src=\"../" . $rows['image_url_1'] . "\"><br /><br />
    				Image source : <input type=\"text\" name=\"image_url_1[" . $rows['id'] . "]\" size=\"50\" value=\"" . $rows['image_url_1'] . "\"/><br />
    				Content :<br /><textarea name=\"content_rows_1[" . $rows['id'] . "]\" cols=\"100\" rows=\"10\">" . $rows['content_1']."</textarea>";
    	}
    }
    else{
    	echo 'No books found!';
    }

    And script:
    Code php:
    switch($page){
    	case 'home':
    		break;
    	case 'book':
    		$query = "UPDATE 
    						table_book 
    					SET
    						title='" . $title . "', 
    						content_1='" . $content_1 . "',
    						title_right='" . $title_right . "',
    						content_right_1='" . $content_right_1 . "'
    					WHERE 
    						id='" . $id . "'";
    		foreach($_POST['image_url_1'] as $id=>$image_url){
    			$content = $_POST['content_rows_1'][$id];
    			$sql = "UPDATE 
    						table_rows 
    					SET
    						content_1='" . $content . "',
    						image_url_1='" . $image_url . "'
    					WHERE 
    						id=" . $id . " 
    						AND page='book'";
    			mysql_query($sql) or die(mysql_error());
    		}
    		break;
    	default:
    		break;
    }
    mysql_query($query) or die('Error, query failed. ' . mysql_error());
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


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
  •