Jump to content


Photo

Hi, I Need Help with a function thanks im new in php.


  • Please log in to reply
10 replies to this topic

#1 zeppis

zeppis
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 October 2006 - 01:03 AM

Hi there I have a problem with a function( i think is the function) don't pay attention to the noob code everything works except last part I'M NOT ABLE TO WRITE DATBASE (connected succesfully)     this is nothing real is homework but I been stuck in this part for 3 days, thanks for your help look at the last lines in bold that's what i think is wrong.

<?php
require_once ('db_connect.php');


if (isset ($_POST[submit])){


if (strlen ($_POST[name])>0) {$name = stripslashes ($_POST[name]);}
else {$name = NULL;
echo '<p><b>Your forgot to enter your name!!</b></p>' ;}

if (strlen ($_POST[email])>0) {$email = stripslashes ($_POST[email]);}
else {$email = NULL;
echo '<p><b>Your forgot to enter eMail!!</b></p>' ;}

if(strlen ($_POST[new_bid])>0) {$new_bid =($_POST[new_bid]);}
else {$new_bid = NULL;
echo '<p><b> Please place your bid!</b></p>';}

if (($_POST[name]) && ($_POST[email] )&& ($_POST[new_bid])) ;
{
echo "Thank  you, <b>{$_POST[name]}</b> for bidding, we'll contact you at <b>{$_POST[email]}</b> if you win this item";}





}
else{ //displays form


?>
<title>PLE1</title>





<body bgcolor="#CCCCCC"><form action=" <?php echo $_SERVER['PHP_SELF']; ?>" method="post">
       <fieldset style="border-color:#1A50B8 "><legend><font color="#1A50B8">Enter your information</font></legend>
<p><b>Your name:</b> <input type="text" name="name" maxlength="40"></p>
<p><b>Email:</b> <input type="text" name="email"  maxlength="25"></p>

<b>Your Bid:</b>
    <select name = "new_bid">
      <option value="">Increase By</option>
      <option value="10">US$10</option>
      <option value="20">US$20</option>
      <option value="30">US$30</option>
      <option value="40">US$40</option>
      <option value="50">US$50 </option>
    </select>
    <input type="submit" value="PlaceBid" name="submit">




</fieldset /form>
<?php
}

?>
<table cellpadding="2" cellspacing="1" border="2" bordercolor="#000000">
<tr>
<td bgcolor="666666"<b>Image</b></td>
<td bgcolor="666666"<b>Description</b></td>
<td bgcolor="666666"<b>Current Bid</b></td>
<td bgcolor="666666"<b>Highest Bidder</b></td>
<td bgcolor="666666"<b>Select</b></td>

</tr>

<?php
$sql = 'select * from plepictures order by bid desc';
$result = @mysql_query($sql);
while ($row = @mysql_fetch_array($result, MYSQL_ASSOC))
{
echo   '<tr><td><img src = "'. $row['filename']. '"></td>' //file name working
. '<td bgcolor="cccccc">'. $row['description'].'</td>'                // description working
. '<td bgcolor="999999">US$'. $row['bid'].'</td>' //bid working
. '<td bgcolor="666666">'. $row['name'].'</td>' //name working
.'<td align = "center" bgcolor="000000"><input type = "radio" name = "image_id" value = '. $row['id'].'"></td></tr>';
}
?>
</table>





<?php  //EVERYTHING UP TO HERE WORKS FINE DATABASE IS CONNECTED ETC.
//THIS IS THE FUNCTION THAT's PROBABLY IS WRONG

function increaseBid() {   

$sql = "select bidfrom plepictures WHERE id=$image_id";
$result = @mysql_query($sql);
$row = @mysql_fetch_array($result,MYSQL_ASSOC);
$bid = $row[0] + $new_bid;

mysql_query("UPDATE plepictures  SET bid='$bid', name='$name', email='$email', WHERE id='$image_id'");

}



function getYear() {
  $year = date("Y");
  return $year;
}


if (isset ($_POST[submit])){


increaseBid();//<======================CALLED THE FUCTION

printf ( mysql_affected_rows());

print("This year is: ".getYear()."\n");

ALL THIS IS SUPPOSED TO WORK BUT IT JUST WON'T CHANGE ANY VALUES IN MY PAGE



}


?>
NOW IF I CHANGE A VALUE IN THE DATABASE IS SHOWN IN THE PAGE, BUT I CAN'T MAKE IT WRITE THE DATABASE AND RETURN THE VALUES TO MY PAGE.

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 12 October 2006 - 01:17 AM

Try displaying your queries in the browser, to check that they are what you think they should be.  Eg

$sql = "UPDATE ... ";
echo "<pre> $sql </pre>"

Also, check the return values on all your mysql queries.

$result = mysql_query($sql);
if ($result === false) die("Query $sql failed with error " . mysql_error());

And don't use @ .. that means you ignore errors instead of fixing them :)

#3 zeppis

zeppis
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 October 2006 - 01:36 AM

Thanks for your help, I took out the @'s, now I inserted the $result = mysql_query($sql);
        if ($result === false) die("Query $sql failed with error " . mysql_error());
into the code, and came up with this error wich is within the function : Query select bidfrom plepictures where id= failed with error 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 'where id=' at line 1

and this is the page so you get an Idea http://www.okdesigns.org


thanks for your help.

#4 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 12 October 2006 - 01:42 AM

you have a trailing comma in your UPDATE statement.  remove the comma after email='$email'.

#5 zeppis

zeppis
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 October 2006 - 01:49 AM

alright these errors are happening within the function I had double quotes in function increaseBid() {   

$sql = 'select bid from plepictures where id=$image_id'; 
SO I CHANGED THEM TO SINGLE QUOTES AND GOT A NEW ERROR MESSAGE  Query select bid from plepictures where id=$image_id failed with error Unknown column '$image_id' in 'where clause'

is that a prob? I removed the comma also.

#6 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 12 October 2006 - 01:51 AM

actually, the problem wasn't with your double quotes, it was the fact that you were missing a space between bid and from in the original query.  either change the quotes back to doubles in order to have $image_id interpolated into its value, or exit the single quotes:

$sql = 'select bid from plepictures where id='.$image_id;


#7 zeppis

zeppis
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 October 2006 - 02:02 AM

Ok I changed this is how it looks like function increaseBid() {   

$sql = "select bid from plepictures where id='$image_id'";
$result = mysql_query($sql);
        if ($result === false) die("Query $sql failed with error " . mysql_error());
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$bid = $row[0] + $new_bid;

mysql_query("UPDATE plepictures  SET bid='$bid', name='$name', email='$email' WHERE id='$image_id'");

}

Im not getting any errors now but my printf ( mysql_affected_rows()); IS SHOWING 0, so the database
isn't getting nothing
http://www.okdesigns.org/  this is the site, now description, pics etc are coming from database so is connected.
thanks all

#8 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 12 October 2006 - 02:07 AM

where are $image_id, $new_bid, $name and $email coming from?  the reason is ask is that, if they're from the form, in order to access them from the function you need to use $_POST['image_id'] instead, like so:

$sql = "select bid from plepictures where id='{$_POST['image_id']}'";

i use the braces there so that the parser doesn't choke on the single quotes used in the index name.

if you want to use them as local variables, you need to pass them to the function as arguments.  if you don't know how to do that, read a tutorial on writing functions.

#9 zeppis

zeppis
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 October 2006 - 02:58 AM

Ok, I changed all variables to globals within the function and came up with new error Query select bid from plepictures where id=(2\") failed with error 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 '\")' at line 1

that means i have to strip slashes or something?

#10 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 12 October 2006 - 05:59 AM

i don't know where that \" is coming from.  what does the query look like in the code?

#11 zeppis

zeppis
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 October 2006 - 12:52 PM

looks like this $sql = "select bid from plepictures where id={$_POST[image_id]}";




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users