PHP Drivint me mad.
Page 1 of 1
MamaKin




Posts: 31

PostPosted: Fri, 3rd Feb 2006 20:47    Post subject: PHP Drivint me mad.
Basicall here's the code that doesn't see to want to work and for the hell of me i can't understand why..

Code:
if($_POST['submit']){

$read = mysql_query("select * from site_products");
while($r=mysql_fetch_array($read))
{   
$ptitle = $r['title'];
$pdesc = $r['desc'];
$pid = $r['id'];
$title = $_POST['title'.$pid];
$desc = $_POST['desc'.$pid];
$sql = "UPDATE ".site_products." SET title = '$title', desc = '$desc' WHERE id = '$pid' ";
$result = mysql_query($sql);
}


The database won't update. $result returns as false yet the $sql is all there....

Here's the rest of the script so you can get some idea of the context it's used in...

Code:
case "products":


if($_POST['submit']){

$read = mysql_query("select * from site_products");
while($r=mysql_fetch_array($read))
{   
$ptitle = $r['title'];
$pdesc = $r['desc'];
$pid = $r['id'];
$title = $_POST['title'.$pid];
$desc = $_POST['desc'.$pid];
$sql = "UPDATE ".site_products." SET title = '$title', desc = '$desc' WHERE id = '$pid' ";
$result = mysql_query($sql);
}
print('updated'.$result.$sql);
}

print('
          <form method="POST" action="admin.php?m=products">
       
            <tr>
      <td bgcolor="#f8f8f8"><font size="2" face="Century Gothic"><b>Edit Current Products</b></font></td>
    </tr>');
 
$rform = mysql_query("select * from site_products");
while($rf=mysql_fetch_array($rform))
{   
$ptitle = $rf['title'];
$pdesc = $rf['desc'];
$pid = $rf['id'];
print('
   <tr>
      <td bgcolor="#FFFFFF"><input type="text" name="title'.$pid.'" size="20" value="'.$ptitle.'"></td>
    </tr>
   <tr>
      <td bgcolor="#FFFFFF"><textarea rows="8" name="desc'.$pid.'" cols="50">'.$pdesc.'</textarea></td>
<tr>

');


Any help is greatly appreciated.
Thansk alot,
Craig Confused
Back to top
Avenger_




Posts: 658
Location: Norway
PostPosted: Sat, 4th Feb 2006 02:22    Post subject:
try to call mysql_error() after the mysql_query call to see why it failed, this might give you more info about the problem: http://no.php.net/manual/en/function.mysql-error.php
Back to top
sTo0z
[Moderator] Babysitter



Posts: 7449
Location: USA
PostPosted: Sat, 4th Feb 2006 02:37    Post subject:
Eh it's been awhile, but what is that comma doing there in your $sql variable?

EDIT: Oh, guess that's proper UPDATE syntax... my bad. Wink

Will look closer.


Back to top
MamaKin




Posts: 31

PostPosted: Sat, 4th Feb 2006 03:05    Post subject:
Cheers for the replies so far guys it's appreciated.

Avenger_ wrote:
try to call mysql_error() after the mysql_query call to see why it failed, this might give you more info about the problem: http://no.php.net/manual/en/function.mysql-error.php


mysql_error() retured the following
Code:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'title field, desc = description field


Seems strange to me....
Back to top
sTo0z
[Moderator] Babysitter



Posts: 7449
Location: USA
PostPosted: Sat, 4th Feb 2006 06:08    Post subject:
I don't think you need to do ".site_products." if the name of the table you are in is just site_products.

So just try "UPDATE site_products SET blah blah

And if your IDs are integers, I'm not sure if you need to surround them with single quotes.

So try WHERE id = $pid


Back to top
MamaKin




Posts: 31

PostPosted: Sat, 4th Feb 2006 12:31    Post subject:
sTo0z wrote:
I don't think you need to do ".site_products." if the name of the table you are in is just site_products.

So just try "UPDATE site_products SET blah blah

And if your IDs are integers, I'm not sure if you need to surround them with single quotes.

So try WHERE id = $pid


I get the same mysql error with
Code:
$sql = "UPDATE site_products SET title = $title, desc = $desc WHERE id = $pid";
aswell as
Code:
$sql = "UPDATE site_products SET title = ".$title.", desc = ".$desc." WHERE id = ".$pid;
Laughing
Back to top
sTo0z
[Moderator] Babysitter



Posts: 7449
Location: USA
PostPosted: Sun, 5th Feb 2006 10:45    Post subject:
Did you try:
Code:

$sql = "UPDATE site_products SET title = '$title', desc = '$desc' WHERE id = $pid";


?


Back to top
MamaKin




Posts: 31

PostPosted: Sun, 5th Feb 2006 14:56    Post subject:
I get the same error with that too Sad
Back to top
sTo0z
[Moderator] Babysitter



Posts: 7449
Location: USA
PostPosted: Sun, 5th Feb 2006 21:09    Post subject:
Alright, I'd start printing out all your variables to make sure their data looks good to you.

Like before the UPDATE statement, try like

print $title;

and stuff, and make sure all the variables look like they have good data, and I know it's silly to ask, but make sure your database is correct... Razz


Back to top
MamaKin




Posts: 31

PostPosted: Sun, 5th Feb 2006 23:04    Post subject:
Right. It looks like the problem was with closing the loops and if braceletts in the wrong places and not the SQL syntax. The entire script is llarger than that posted above. Sorted now though.

Thanks for your help Avenger_ and Sto0z Wink
Back to top
sTo0z
[Moderator] Babysitter



Posts: 7449
Location: USA
PostPosted: Mon, 6th Feb 2006 05:36    Post subject:
Ah, sorry I couldn't pinpoint the problem, lol. Smile

Glad you fixed it though! Good luck!


Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group