homepage Welcome to WebmasterWorld Guest from 54.205.106.111
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
mysql problem wont update?
mysql database wont update
Kaldi




msg:3889476
 12:21 am on Apr 10, 2009 (gmt 0)

i have a script that wont update the mysql database
the values that are posted from the form are getting posted but for some reason it does not update the database.
here is the script that updates the database
----------------------------------------------------------
<?
$hostname="localhost";
$username="username";
$password="password";
$database="database";

// Connect to the database
mysql_connect($hostname, $username, $password) OR DIE("Unable to connect");
mysql_select_db("$database") or die("Unable to select database");

$number=$_POST['howmanynav1'];
$order=$_POST['itemOrder'];
$order = explode(";", $order);
$I=0;
$Z=1;
$number=$number;
while($I<$number){
$name=$order[$I];
$ordernav13="SELECT * FROM addons where name='$name'";
$ordernav12=mysql_query($ordernav13) or die("It died");
$ordernav1=mysql_fetch_array($ordernav12);
$ordernav1num=$ordernav1[nav1];
echo "$ordernav1num";
echo "$name<br>";
echo "$Z<br>";
$query="UPDATE addons SET nav1 = '$Z' WHERE name = '".$order[$I]."'";
echo $query;
echo "<br>";
$checkresult = mysql_query($query);
if ($checkresult) echo 'update query succeeded';
else echo 'update query failed';
echo "<br>";
$I=$I+1;
$Z=$Z+1;
}
?>
----------------------------------------------------------
and the results are
----------------------------------------------------------
Link Panel
1
UPDATE addons SET nav1 = '1' WHERE name = 'Link Panel'
update query succeeded
Login System
2
UPDATE addons SET nav1 = '2' WHERE name = 'Login System'
update query succeeded
Search
3
UPDATE addons SET nav1 = '3' WHERE name = 'Search'
update query succeeded
Highest Rated Articles
4
UPDATE addons SET nav1 = '4' WHERE name = 'Highest Rated Articles'
update query succeeded
Most Read Articles
5
UPDATE addons SET nav1 = '5' WHERE name = 'Most Read Articles'
update query succeeded
Veiw Active Users
6
UPDATE addons SET nav1 = '6' WHERE name = 'Veiw Active Users'
update query succeeded

----------------------------------------------------------

but i cant update mysql and i can pull data from it ether. yes the username and password and database is all correct.

I'm completely puzzled cause i have another script that updates the database but this one won't.
any ideas?

[edited by: coopster at 10:12 am (utc) on April 10, 2009]
[edit reason] masked userid and passwords [/edit]

 

dublinmike




msg:3889812
 11:16 am on Apr 10, 2009 (gmt 0)

Hi there, your check means, was the query well formed which I assume it is, pass in your database link to mysql_affected_rows to see if the database was actually updated, i.e. instead of


$checkresult = mysql_query($query);
if ($checkresult) echo 'update query succeeded';

try...

mysql_query($query);
if (mysql_affected_rows($db_link)) echo 'update query succeeded';

Kaldi




msg:3889910
 2:57 pm on Apr 10, 2009 (gmt 0)

i made that change and this is what i have

----------------------------------------------------------

$query="UPDATE addons SET nav1 = '$Z' WHERE name = '".$order[$I]."'";
echo $query;
echo "<br>";
mysql_query($query);

if (mysql_affected_rows()) echo 'update query succeeded';
else echo 'update query failed';
----------------------------------------------------------

and it displays this

----------------------------------------------------------
Link Panel
1
UPDATE addons SET nav1 = '1' WHERE name = 'Link Panel'
update query failed
----------------------------------------------------------

i also have this

----------------------------------------------------------

$ordernav13="SELECT * FROM addons where name='$name'";
$ordernav12=mysql_query($ordernav13) or die("It died");
$ordernav1=mysql_fetch_array($ordernav12);
$ordernav1num=$ordernav1[nav1];
echo "$ordernav1num";

----------------------------------------------------------

which should display a number from the database of it's current position. and $Z is displayed witch is what it should be updated to. i don't understand why it wont pull data from or pose data to the database and not give an error message. any ideas?

[edited by: Kaldi at 3:34 pm (utc) on April 10, 2009]

dublinmike




msg:3890470
 10:12 am on Apr 11, 2009 (gmt 0)

Just print out exactly what's in your table, if the query isn't 'updating' the row, it means the value is probably already set to what you're setting it to. What does the following print?


$sql = "SELECT nav1, name FROM addons";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
print_r($row);
}

In your last snippet of code, I think you're missing some quotes i.e. $ordernav1[nav1] should be $ordernav1['nav1']. In fact, if that snippet is in your original code, that might be the problem...

Kaldi




msg:3890556
 2:03 pm on Apr 11, 2009 (gmt 0)

$sql = "SELECT nav1, name FROM addons";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
print_r($row);
}

prints this

Array ( [nav1] => 0 [name] => Forum ) Array ( [nav1] => 2 [name] => Login System ) Array ( [nav1] => 3 [name] => Search ) Array ( [nav1] => 4 [name] => Highest Rated Articles ) Array ( [nav1] => 1 [name] => Link Panel ) Array ( [nav1] => 0 [name] => News ) Array ( [nav1] => 0 [name] => Veiw Active Users ) Array ( [nav1] =>
0 [name] => Admin ) Array ( [nav1] => 5 [name] => Most Read Articles )

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved