brem13 Posted March 7, 2009 Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/ Share on other sites More sharing options...
daanoz Posted March 7, 2009 Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779033 Share on other sites More sharing options...
brem13 Posted March 7, 2009 Author Share Posted March 7, 2009 thank you, kindof helped a bit, i changed my code, and now, if i change the comment of the first pic, it updates the database to the comment of the last pic??? Link to comment https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779037 Share on other sites More sharing options...
brem13 Posted March 7, 2009 Author Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779055 Share on other sites More sharing options...
daanoz Posted March 7, 2009 Share Posted March 7, 2009 can you give us a bit more info on how the table layout works? Link to comment https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779057 Share on other sites More sharing options...
brem13 Posted March 7, 2009 Author Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779059 Share on other sites More sharing options...
daanoz Posted March 7, 2009 Share Posted March 7, 2009 well, for example in this sql: "UPDATE $userlow SET comment='$comment' WHERE picture='$picname'" $userlow should indicate the table name, but instead it looks like it is the username? Link to comment https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779067 Share on other sites More sharing options...
brem13 Posted March 7, 2009 Author Share Posted March 7, 2009 ya, it takes the username and makes it all lowercase because there is a different table for every user Link to comment https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779069 Share on other sites More sharing options...
daanoz Posted March 7, 2009 Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779078 Share on other sites More sharing options...
brem13 Posted March 7, 2009 Author Share Posted March 7, 2009 below is a pic of my phpadmin, and my site with the comments [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779086 Share on other sites More sharing options...
brem13 Posted March 7, 2009 Author Share Posted March 7, 2009 its not putting the whole comment in the field but ill try n fix that later, im just trying to have the comments update Link to comment https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779090 Share on other sites More sharing options...
daanoz Posted March 7, 2009 Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779096 Share on other sites More sharing options...
brem13 Posted March 7, 2009 Author Share Posted March 7, 2009 Warning: Invalid argument supplied for foreach() ...BREMINEM/RANDOM/editgall.php on line 26 Line 26: foreach($aPics as $PicKey => $PicName) { Link to comment https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779099 Share on other sites More sharing options...
brem13 Posted March 7, 2009 Author Share Posted March 7, 2009 i fixed that, but now it doesnt do anything, nothing changes or updates Link to comment https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779103 Share on other sites More sharing options...
daanoz Posted March 7, 2009 Share Posted March 7, 2009 what does a print_r of $aComments and $aPics give you? Link to comment https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779105 Share on other sites More sharing options...
brem13 Posted March 7, 2009 Author Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779109 Share on other sites More sharing options...
brem13 Posted March 7, 2009 Author Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779112 Share on other sites More sharing options...
daanoz Posted March 7, 2009 Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779114 Share on other sites More sharing options...
brem13 Posted March 7, 2009 Author Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779115 Share on other sites More sharing options...
daanoz Posted March 7, 2009 Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779128 Share on other sites More sharing options...
brem13 Posted March 7, 2009 Author Share Posted March 7, 2009 changed it, same thing, only first word displays Link to comment https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779136 Share on other sites More sharing options...
redarrow Posted March 7, 2009 Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779149 Share on other sites More sharing options...
daanoz Posted March 7, 2009 Share Posted March 7, 2009 ok, i'm back, lets try this : while ($qry = mysql_fetch_array($result, MYSQL_ASSOC )) { [/]code] Link to comment https://forums.phpfreaks.com/topic/148379-solved-updating-mutiple-rows-mysql/#findComment-779172 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.