Page 1 of 1
My script does not work, could it be because of...
Posted: Thu Oct 17, 2002 5:45 pm
by evilmonkey
Hi. This is a user submission script that comes from one of five radio buttons on the page before. The input form looks like this:
Code: Select all
<form method="POST" action="pratingsystem.php">
<?php
echo '<input type="hidden" name="jokeId" value="$id">'; //$id comes from what's earlier on the page, a dispay script
?>
<input type="radio" value="5" checked name="rating">Excellent
<input type="radio" name="rating" value="4">Good
<input type="radio" name="rating" value="3">OK
<input type="radio" name="rating" value="2">Not-so-good
<input type="radio" name="rating" value="1">Sucks</p>
<p><input type="submit" value="Submit" name="B1"></p>
</form>
The proccessing script looks like this:
Code: Select all
<?php
if ($rating==""){
echo "Go back and select a rating";
}
else
{
$db="mysql_connect('localhost', '', '')"
mysql_select_db(db_thawowi_2, $db)"
$number=0;
$number="SELECT number FROM table WHERE id='$jokeId'"; //I need the id part
$number=$number+1;
$query="UPDATE table SET number='$number' WHERE id='$jokeId'";
$result="mysql_query($query, $db); inserted the new total
$total=0;
$total="SELECT total FROM table WHERE id='$jokeID'";
$total='$total'+'$rating';
$secondquery="UPDATE table SET total='$total' WHERE id='$jokeID'";
$secondresult="mysql_query($secondquery, $db);
$average="$total/$number";
/*total is the number I get after adding all user submissions together,
this is (if I remeber math correctly) the formula for finding the arithmetic
mean */
$finalquery="UPDATE table SET average='$average'";
$finalresult="mysql_query($finalquery, $db)";
echo "some success message";
}
?>
Basically, what the above code does is it finds the average of all the user submissions, and records that in a database. $total is all user submissions added together (one person votes 3, anothr person votes 5, $total would be 8 etc.) $number is the total number of user submissions (one person votes 3, anothr person votes 5, $number would be 2 because 2 people voted). $total, $number and $average are IN THE DATABASE and I update the database with the new numbers for future reference (i.e. finding out the average after the next person votes).
My question is, will all this work in order? (update $number first, $total second, and $average third). If not, is there a way to make without writing 3 separate codes?
As of right now, this does not work. It gives me the success message, but when I go into my database, I see no changes. Why is this?
Please check for parse errors, this could be a stupid parse error. I probably messed up in single quotes, double quotes, no quotes, etc. Under which circumstances are each of these quotes used?
Thanks.
a quick look
Posted: Thu Oct 17, 2002 7:12 pm
by phpScott
well one thing that looks like it could be causing a issues is that when you do your "selects" from the database you write the query but miss a very important step which is to "submit" the query and get the results.
The next thing is that on the line
$result="mysql_query($query, $db); inserted the new total
it probably should read
$result="mysql_query($query, $db); //inserted the new total
to comment out your comment.
phpScott
Posted: Thu Oct 17, 2002 7:18 pm
by volka
Code: Select all
<?php
if (empty($rating) || !is_numeric($rating))
echo "Go back and select a rating";
else
{
$db=mysql_connect('localhost', '', '') or die(mysql_error());
mysql_select_db(db_thawowi_2, $db) or die(mysql_error());
$query = 'SELECT number, total from table where id='.$jokeId; // edit: just a ' too much
$result = mysql_query($query, $db) or die(mysql_error());
$row = mysql_fetch_row($result) or die('no such joke');
$rowї0] += 1;
$rowї1] += $rating;
$query = 'UPDATE table SET total='.$rowї0].',number='.$rowї1].' where id='.$jokeId;
$result = mysql_query($query, $db) or die(mysql_error());
// why storing the average? all data needed is in the db
echo "some success message";
}
?>
$str will contain
$val, not 3. Only within double-quoted strings variables will be replaced.
Do not put function-calls in quotes. "mysql_query(...)" doesn't make sense
What are you doing with
$total="SELECT total FROM table WHERE id='$jokeID'";?
Posted: Thu Oct 17, 2002 9:04 pm
by evilmonkey
Hi. For $total, I have to pull it out, add the new value to the $total, and work with the new $total to find an avarage.
As for "Why use the average", There is only one way I could avouid that, and that is adding all ratings to one field. the UPDATE query deletes previous fields. How do I get around that?
Also, volka, I noticed you put a peroid before the variables. For example, I have $row, while you have $.row[0]. Why, and how do I work with that?
Thanks.
Posted: Thu Oct 17, 2002 9:14 pm
by mydimension
the period is the concatenation operator in PHP. i.e.:
$str = "this plus " . "this"; //$str now equals "this plus this"
this comes in handy when using array values within strings. i.e.
$row[0] = "this";
$str = "this plus " . $row[0]; //$str now equals "this plus this"
hope that helps
Posted: Thu Oct 17, 2002 9:23 pm
by evilmonkey
I have an outside question. How would I put the slashes in the following script?
Code: Select all
<?php
echo "<input type="hidden" name="jokeId" value=".$id.">";
?>
I need slashes around "hidden" and "jokeID".
Thanks.
Posted: Thu Oct 17, 2002 9:24 pm
by volka
$total="SELECT total FROM table WHERE id='$jokeID'";
$total='$total'+'$rating';
Ah, I thought you're setting
$total and in the next line you're assigning a new value without doing something with the old value. But now I see that you're trying to add those values.
'$total' is the string-literal for
$total not the content of
$total, only double-quoted strings are evaluated - and they are eval. as strings.
Probably you want
$total=$total+$rating; which is the same as
$total += $rating;
You store
total and
number within the database.
Of course you can store the average as well. But you can also get it from the values already in the database.
Code: Select all
print('just ' . ' a ' . 'concatenation');
- will result in
- concatenation of 'just ' and ' a ' --> new string 'just a '
- concatenation of 'just a ' and 'concatenation' --> new string 'just a concatenation'
- output of that string
- returning TRUE or FALSE
each concatenation needs time and memory.
echo can take more than one parameter if seperated by ,
no concatenation is perform but all parameters are printed
Probably nothing you have to worry about

Posted: Thu Oct 17, 2002 9:28 pm
by evilmonkey
Actually, the echo is something I have to worry about because PHP is giving me parse errors.

Posted: Thu Oct 17, 2002 9:33 pm
by volka
yo, I just edited the script.
It is completely untested (not even by compiler)
Within a '-string-literal you can use " freely.
Within a '-string-literal you have to escape other ' with ''
Within a "-string-literal you can use ' freely.
Within a "-string-literal you have to escape other " with \"
Code: Select all
echo 'here you can use " ', " and here ' ", " " have to be escaped", ' as well '' ';
Posted: Thu Oct 17, 2002 9:40 pm
by evilmonkey
Never mind, I got it. (the echo)
Volka, I tried using your script. Unfortunatly, no matter what I do, I get the "some success message", although nothing appears in the database. Please take a look at my submission and processiong scripts one more time. Thanks.
Code: Select all
<body onLoad="window.resizeTo(580,420)">
<?php
$db = mysql_connect("localhost","","l");
mysql_select_db("dbthawowi_2",$db);;
$callsign = $HTTP_POST_VARSї'id'];
$callsign = $_GETї'id'];
$query = "SELECT * FROM userjokes WHERE id='$id'";
$resultat = mysql_query($query) or die(mysql_error());
$row= mysql_fetch_object($resultat);
echo "Joke number ";
print"$row->id<br><br>";
print"$row->joke<br><br>";
echo "Joke submitted by: ";
print"$row->name ";
echo "on ";
$date_from_db = "$row->date";
$formatted_date = date("m/j/y",time($date_from_db));
echo $formatted_date;
?>
<HTML>
<body>
<form method="POST" action="pratingsystem.php">
<?php
echo "<input type="hidden" name="jokeId" value=".$id.">";
?>
<input type="radio" value="5" checked name="rating">Excellent&nbsp;
<input type="radio" name="rating" value="4">Good&nbsp;
<input type="radio" name="rating" value="3">OK&nbsp;
<input type="radio" name="rating" value="2">Not-so-good&nbsp;
<input type="radio" name="rating" value="1">Sucks</p>
<p><input type="submit" value="Submit" name="B1"></p>
</form>
</body>
</HTML>
</body>
This scripts displays the joke and gives the user the option to evaluate it.
And here is pratingsystem.php, the processing script:
Code: Select all
<html>
<body>
<?php
if (empty($rating) || !is_numeric($rating))
echo "Go back and select a rating";
else
{
$db=mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("dbthawowi_2", $db) or die(mysql_error());
$query = "SELECT number, total FROM evaluation WHERE id='$jokeId'";
$result = mysql_query($query, $db) or die(mysql_error());
$row = mysql_fetch_row($result) or die('no such joke');
$rowї0] += 1;
$rowї1] += $rating;
$secondquery = "UPDATE evaluation SET total='.$rowї0].',number='.$rowї1].' WHERE id='$jokeId'";
$secondresult = mysql_query($secondquery, $db) or die(mysql_error());
echo "some success message";
}
?>
</body>
</html>
Nothing gets updated, all values stay at zero.
Thanks.
Posted: Thu Oct 17, 2002 10:11 pm
by volka
there are some errors in
$secondquery = "UPDATE evaluation SET total='.$row[0].',number='.$row[1].' WHERE id='$jokeId'";
first: Only quote values of string-type-fields (varchar, text, blob, ...) in your table. your $query-string will be something like
Code: Select all
UPDATE evaluation SET total='.456.',number='.123.' WHERE id='2'
quoting all three numerical fields (at least I hope they are numbers in your table, i.e. int-type). Same in your SELECT-statement.
The real problem is that you still have troubles with string-evaluation and concatenation.
The (hopefully) correct statement is either
Code: Select all
// using string-evaluation/replacement
$secondquery = "UPDATE evaluation SET total=$rowї0],number=$rowї1] WHERE id=$jokeId";
or
Code: Select all
// using string-concatenation
$secondquery = 'UPDATE evaluation SET total='.$rowї0].',number='.$rowї1].' WHERE id='.$jokeId;
(and -of course- many more)
take those two statements and compare them with yours
just echo them before performing the query
Code: Select all
echo $secondquery;
$secondresult = mysql_query($secondquery, $db) or die(mysql_error());
Posted: Fri Oct 18, 2002 5:37 pm
by evilmonkey
Hi.
Thanks everyone, I have finished the script. The script does what i originally wanted it to do, calculates the average, as well as it records values from each user. But, most importantly, IT WORKS!!!
Thanks guys.
Posted: Fri Oct 18, 2002 6:23 pm
by Heavy
evilmonkey wrote:$resultat = mysql_query($query) or die(mysql_error());
$row= mysql_fetch_object($resultat);
...vadå Toronto, Canada?:?
Du skriver ju på Svenska!

Posted: Fri Oct 18, 2002 7:11 pm
by volka
oder aus einem deutschsprachigen Land

Posted: Fri Oct 18, 2002 7:11 pm
by evilmonkey
Uhh...
What did you say about Toronto, Canada?
No, I don't speak "Svenska"
You speak Russian/English? Those are the only two languages I can speak in.
Good luck.