Jump to content

[SOLVED] updating mutiple rows, mysql


brem13

Recommended Posts

i have a script that shows pictures from a database along with their comments, when i try to update the comments, it doesnt do anything except put comment[11] in the comment for the last picture. code is below, any help is appreciated :)

 

<?php

if ($_POST['submit'])

{

include("../../../../configpic.php");

$username = $_COOKIE['loggedin'];

$userlow = strtolower($username);

$username1 = basename(dirname(__FILE__)) ;

 

$cwd = getcwd();

$cwd = explode("/", $cwd);

$pwd = $cwd[(count($cwd)-2)];

$userdir = strtolower($pwd);

$comment = htmlspecialchars($_POST['comment[]']);

$picname = htmlspecialchars($_POST['pic']);

 

mysql_connect($server, $db_user, $db_pass) or die (mysql_error());

 

mysql_select_db($database) or die ("Could not select database because ".mysql_error());

 

$transaction = mysql_db_query($database, "select * from $userdir WHERE album = '$username1'") or die (mysql_error());

$p = 1;

while($trans = @mysql_fetch_array($transaction)){

 

mysql_query("UPDATE $userlow SET comment='comment[".$p."]' WHERE picture='$picname'") or die(mysql_error());

$p++;

}

}

 

 

?>

<?php

  include("../../../../configpic.php");

      $username1 = basename(dirname(__FILE__)) ;

     

      $cwd = getcwd();

$cwd = explode("/", $cwd);

$pwd = $cwd[(count($cwd)-2)];

 

$userdir = strtolower($pwd);

     

$username2 = strtolower($username1);

 

          mysql_connect($server, $db_user, $db_pass) or die (mysql_error());

          $result = mysql_db_query($database, "select * from $userdir WHERE album = '$username1'") or die (mysql_error());

$folder = "galleries/members/".$username1;

 

  $i = 1;

  $p = 1;

 

while ($qry = mysql_fetch_array($result)) {

    echo '<a href="'.$qry[picture].'"><img src="'.'thumbs/'.$qry[picture].'"></a>';

    $pic = $qry[picture];

    echo "<input type='hidden' name='pic' value=".$pic.">";

    echo "<form name='edit' action='editgall.php' method='POST'><input type='text' name='comment[".$p."]' length=50 value=".$qry[comment].">";

    $p++;

    if($i == 4)

    {

        echo '<br />';

        $i = 0; // reset counter

    }

    $i++;

 

  }

 

  echo "<input type='submit' name='submit' value='Update'></form>";

    ?>

Link to comment
Share on other sites

hi, a quick view over your code shows a simple problem, not tested though just a quick thought

 

this line: $comment = htmlspecialchars($_POST['comment[]']); is your problem, the value in the database showing is an array value.

 

The name for your HTML element is comment[1], comment[2], etc. . When retrieving this value from the POST array it will return as an array.

 

so, the solution:

 

$aComments = $_POST['comment'];
// uncomment the following line to see the contents of the array
//print_r($aComments); 
$comment = htmlspecialchars($aComments[1]);

 

This code will retrieve your comment, the value "1" is an example, this depends on your application

 

Cheers Daanoz

 

Link to comment
Share on other sites

code looks like this now

 

<?php

if ($_POST['submit'])

{

include("../../../../configpic.php");

$username = $_COOKIE['loggedin'];

$userlow = strtolower($username);

$username1 = basename(dirname(__FILE__)) ;

$cwd = getcwd();

$cwd = explode("/", $cwd);

$pwd = $cwd[(count($cwd)-2)];

 

$userdir = strtolower($pwd);

//$comment = htmlspecialchars($_POST['comment[]']);

$aComments = $_POST['comment'];

// uncomment the following line to see the contents of the array

//print_r($aComments);

$comment = htmlspecialchars($aComments[1]);

$picname = htmlspecialchars($_POST['pic']);

mysql_connect($server, $db_user, $db_pass) or die (mysql_error());

 

mysql_select_db($database) or die ("Could not select database because ".mysql_error());

 

$transaction = mysql_db_query($database, "select * from $userdir WHERE album = '$username1'") or die (mysql_error());

 

while($trans = @mysql_fetch_array($transaction)){

 

mysql_query("UPDATE $userlow SET comment='$comment' WHERE picture='$picname'") or die(mysql_error());

 

}

}

 

?>

 

<?php

  include("../../../../configpic.php");

      $username1 = basename(dirname(__FILE__)) ;

     

      $cwd = getcwd();

$cwd = explode("/", $cwd);

$pwd = $cwd[(count($cwd)-2)];

 

$userdir = strtolower($pwd);

     

$username2 = strtolower($username1);

 

          mysql_connect($server, $db_user, $db_pass) or die (mysql_error());

          $result = mysql_db_query($database, "select * from $userdir WHERE album = '$username1'") or die (mysql_error());

$folder = "galleries/members/".$username1;

echo "<p align=center>";

  $i = 1;

 

while ($qry = mysql_fetch_array($result)) {

    echo '<a href="'.$qry[picture].'"><img src="'.'thumbs/'.$qry[picture].'"></a>';

    $pic = $qry[picture];

    echo "<input type='hidden' name='pic' value=".$pic.">";

    echo "<form name='edit' action='editgall.php' method='POST'><input type='text' name='comment' length=50 value=".$qry[comment].">";

   

    if($i == 4)

    {

        echo '<br />';

        $i = 0; // reset counter

    }

    $i++;

 

  }

  echo "</p>";

  echo "<input type='submit' name='submit' value='Update'></form>";

    ?>

 

 

 

Link to comment
Share on other sites

its supposed to go through the database and display pictures for an album ex. random, with every picture displayed it is supposed to put an text input tag next to the picture with the comment from the database for that picture, what i want, is when i change the comment in the text field, i want it to update that comment in the database

Link to comment
Share on other sites

aah k,

 

so, in your table you have multiple rows containing the same picture name? if this is the case, it should work. you can test this by performing a select instead of an update to retrieve all rows eligible for an update.

 

so something like:

 

"SELECT * FROM $userlow WHERE picture='$picname'"

 

Another thing which mite cause a problem is the $picname = htmlspecialchars($_POST['pic']); line, the htmlspecialchars might alternate your picture name in such a way that it doesn't match the records...

 

Hope this helps a bit

Link to comment
Share on other sites

Right, lets give this a shot, not sure this is what you want though

 

<?php
if ($_POST['submit'])
{
  include("../../../../configpic.php");
  $username = $_COOKIE['loggedin'];
  $userlow = strtolower($username);
  $username1 = basename(dirname(__FILE__)) ;
  $cwd = getcwd();
  $cwd = explode("/", $cwd);
  $pwd = $cwd[(count($cwd)-2)];

  $userdir = strtolower($pwd);
  //$comment = htmlspecialchars($_POST['comment[]']);
  $aComments = $_POST['comment'];
  $aPics = $_POST['pic'];


  $comment = htmlspecialchars($aComments[1]);
  $picname = htmlspecialchars($_POST['pic']);
  mysql_connect($server, $db_user, $db_pass) or die (mysql_error());

  mysql_select_db($database) or die ("Could not select database because ".mysql_error());

  $transaction = mysql_db_query($database, "select * from $userdir WHERE album = '$username1'") or die (mysql_error());

  foreach($aPics as $PicKey => $PicName) {
     if(array_key_exists($PicKey, $aComments)) {
        $comment = htmlspecialchars($aComments[$PicKey]);    
        $picname = htmlspecialchars($PicName);   
        mysql_query("UPDATE $userlow SET comment='$comment' WHERE picture='$picname'") or die(mysql_error());
    }
  }
}

?>

<?php
  include("../../../../configpic.php");
      $username1 = basename(dirname(__FILE__)) ;
     
      $cwd = getcwd();
$cwd = explode("/", $cwd);
$pwd = $cwd[(count($cwd)-2)];

$userdir = strtolower($pwd);
     
$username2 = strtolower($username1);

          mysql_connect($server, $db_user, $db_pass) or die (mysql_error());
          $result = mysql_db_query($database, "select * from $userdir WHERE album = '$username1'") or die (mysql_error());
$folder = "galleries/members/".$username1;
echo "<p align=center>";
  $i = 1;
$counter = 0;
echo "<form name='edit' action='editgall.php' method='POST'>";
while ($qry = mysql_fetch_array($result)) {
    echo '<a href="'.$qry[picture].'"><img src="'.'thumbs/'.$qry[picture].'"></a>';
    $pic = $qry[picture];
    echo "<input type='hidden' name='pic[".$counter."]' value=".$pic."><input type='text' name='comment[".$counter."]' length=50 value=".$qry[comment].">";
   
    if($i == 4)
    {
        echo '<br />';
        $i = 0; // reset counter
    }
    $i++;
$counter++;
   }
   echo "</p>";
   echo "<input type='submit' name='submit' value='Update'></form>";
    ?>

Link to comment
Share on other sites

Array ( [0] => dfg [1] => waterskiing [2] => me [3] => me [4] => up [5] => me [6] => on [7] => up [8] => me [9] => skiing [10] => ) Array ( [0] => 1234924386.jpg [1] => 1234920795.jpg [2] => 1234894096.jpg [3] => 1234907747.jpg [4] => 1234930824.jpg [5] => 1234963429.jpg [6] => 1234981460.jpg [7] => 1234887327.jpg [8] => 1234886722.jpg [9] => 1234915683.jpg [10] => 1234885926.jpg )

Link to comment
Share on other sites

ok, it works, lol, u had the picname as PicName, i changed it and it finally works, thank you sooooooo much for all your help, just one more thing if u can please? see how it only puts the first word from the comment in the input tag, what could be causing that??

Link to comment
Share on other sites

ok, np, sry for the slow reponses, but the forum seems to be loadin pretty slow.

 

hmm, the input thing is a bit wierd though...

 

try this:

 

        $comment = mysql_real_escape_string(htmlspecialchars($aComments[$PicKey]));   

        $picname = mysql_real_escape_string(htmlspecialchars($PicName)); 

 

my guess it that a character might be breaking up the SQL

 

 

Link to comment
Share on other sites

ya, it is loading really slow, i thought it was my connection, lol, umm, but i checked and when i update it, it puts everything in the database like "at home" but when it displays in the textbox, it only shows the first word, but it puts all the words in the database, its just not showing them all

Link to comment
Share on other sites

ok, i would start by putting quotes surround the indexes

    echo '<a href="'.$qry['picture'].'"><img src="'.'thumbs/'.$qry['picture'].'"></a>';
    $pic = $qry['picture'];
    echo "<input type='hidden' name='pic[".$counter."]' value=".$pic."><input type='text' name='comment[".$counter."]' length=50 value=".$qry['comment'].">";

Link to comment
Share on other sites

but all your arrays look very wired sorry.

 

example

 

$data['name'] <<< correct.

 

but

 

$data[name] <<<< incorrect.

 

also i no it ok but i suggest using the while loop method like

 

while($data=mysql_fetch_assoc($query_result)){ <<< start the loop.

 

// let say i wanted all the messages .

 

$messages=" ".$data['messages']."\n <br>";

 

echo $messages;

 

} <<< end the loop.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.