Jump to content

Archived

This topic is now archived and is closed to further replies.

zeppis

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

Recommended Posts

[b]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)[/b]     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>




[b]
<?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 [/b]


}


?>
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.

Share this post


Link to post
Share on other sites
Try displaying your queries in the browser, to check that they are what you think they should be.  Eg

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

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

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

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

Share this post


Link to post
Share on other sites
Thanks for your help, I took out the @'s, now I inserted the [i] $result = mysql_query($sql);
        if ($result === false) die("Query $sql failed with error " . mysql_error());[/i] into the code, and came up with this error wich is within the function : [b]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[/b]

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


thanks for your help.

Share this post


Link to post
Share on other sites
you have a trailing comma in your UPDATE statement.  remove the comma after email='$email'.

Share this post


Link to post
Share on other sites
alright these errors are happening within the function I had double quotes in function [b]increaseBid() {   

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

is that a prob? I removed the comma also.

Share this post


Link to post
Share on other sites
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:

[code]$sql = 'select bid from plepictures where id='.$image_id;[/code]

Share this post


Link to post
Share on other sites
Ok I changed this is how it looks like [b]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'");

}[/b]
Im not getting any errors now but my [b]printf ( mysql_affected_rows()); [/b] 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

Share this post


Link to post
Share on other sites
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:

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

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.

Share this post


Link to post
Share on other sites
Ok, I changed all variables to globals within the function and came up with new error [b]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[/b]

that means i have to strip slashes or something?

Share this post


Link to post
Share on other sites
i don't know where that \" is coming from.  what does the query look like in the code?

Share this post


Link to post
Share on other sites
looks like this [b]$sql = "select bid from plepictures where id={$_POST[image_id]}";[/b]

Share this post


Link to post
Share on other sites

×

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.