twilightnights Posted December 28, 2006 Share Posted December 28, 2006 I am trying to create a site where each of my jokes are rated. This will communicate with a mysql database that stores the rating and number of votes. I have only been using php recently and I am learning php, html, javascript, and mysql at the same time. I am having a problem having my form and php script page communicate with eachother. I'm not sure where the problem lies.This is the form on the page www.omgjokes.com/random.php<pre><form name="rating" method="post" action="ratejoke.php"> <font class="subfont"><P align="center">Rate this Joke:</font> <select name="userrating"> <option value="5.0" selected>5</option> <option value="4.0">4</option> <option value="3.0">3</option> <option value="2.0">2</option> <option value="1.0">1</option> <option value="0.0">0</option> <input type="hidden" name="jokeID" value=<?php echo"$jokeID;"?>> <input type="submit" value="Go!"> </select> </p> </form></pre>This is the code for ratejoke.php I have no idea why the pre tags won't work<pre><?phpinclude("dbinfo.inc.php");include("dbconnect.inc.php");$jokeID = $_POST['jokeID']$userrating = $_POST['userrating'];if (session_is_registered("rating$jokeID")) { echo "<center>Sorry! You have already voted!"; } else { $get_count = mysql_query("SELECT rating, votes FROM '$table' WHERE jokeID=$jokeID"); while(list($rating, $votes)=mysql_fetch_array($get_count)) { $new_count = ($votes + 1); $tut_rating2 = ($rating * $votes); $new_rating = (($userrating + $tut_rating2) / ($new_count)); $new_rating2 = number_format($new_rating, 2, '.', ''); $update_rating = mysql_query("UPDATE '$table' SET rating='$new_rating2',votes='$new_count' WHERE jokeID='$jokeID'"); $sessionvar = "rating$jokeID"; session_register($sessionvar); echo "<div align="center"><b> <p>Thanks for your vote!</p> <p>The new rating for this joke is: <br> <br> $new_rating2 out of 5</p>";mysql_close(); } } echo "<p align="center"><a href="javascript:history.back();"><< Back</a> | <a href="/index.php">Main Page</a></b><br> </p>";?> </pre> Link to comment https://forums.phpfreaks.com/topic/32097-need-help-with-form-and-php-communication/ Share on other sites More sharing options...
craygo Posted December 28, 2006 Share Posted December 28, 2006 this is wrong[code]<input type="hidden" name="jokeID" value=<?php echo"$jokeID;"?>>[/code]can use this[code]<input type="hidden" name="jokeID" value="<?=$jokeID?>">[/code]can probaly do this also[code] $get_count = mysql_query("SELECT rating, votes FROM '$table' WHERE jokeID=$jokeID"); while($r = mysql_fetch_assoc($get_count)){extract($r);[/code]Also pretty sure this is wrong[code]$sessionvar = "rating$jokeID";[/code]this will work[code]$sessionvar = "rating".$jokeID;[/code]Other than that looks fine accept for the session part. Are you starting the session at the top of your pages?? Try fixing those and make sure you have session_start(); as the first line at the top of your script.Ray Link to comment https://forums.phpfreaks.com/topic/32097-need-help-with-form-and-php-communication/#findComment-148961 Share on other sites More sharing options...
twilightnights Posted December 28, 2006 Author Share Posted December 28, 2006 Thanks, I'm pretty sure the form is now working properly, as when I view the source it has the correct jokeID, but the ratejoke.php still won't post for some reason. Do I have to have a session start on the same page as the form or do I have to close the session at any time? I tried to period concatenation thingie, I noticed in the tutorial I used they didn't use it though http://www.phpfreaks.com/tutorials/9/0.php[code]<?phpsession_start();include("dbinfo.inc.php");include("dbconnect.inc.php");$jokeID = $_POST['jokeID']$userrating = $_POST['userrating'];if (session_is_registered("rating.$jokeID")) { echo "<center>Sorry! You have already voted!"; } else { $get_count = mysql_query("SELECT rating, votes FROM '$table' WHERE jokeID='$jokeID'"); while($r = mysql_fetch_assoc($get_count)){extract($r);} $new_count = ($votes + 1); $tut_rating2 = ($rating * $votes); $new_rating = (($userrating + $tut_rating2) / ($new_count)); $new_rating2 = number_format($new_rating, 2, '.', ''); $update_rating = mysql_query("UPDATE '$table' SET rating='$new_rating2',votes='$new_count' WHERE jokeID='$jokeID'"); $sessionvar = "rating.$jokeID"; session_register($sessionvar); echo "<div align="center"><b> <p>Thanks for your vote!</p> <p>The new rating for this joke is: <br> <br> $new_rating2 out of 5</p>";mysql_close(); } echo "<p align="center"><a href="javascript:history.back();"><< Back</a> | <a href="/index.php">Main Page</a></b><br> </p>";?> [/code]And the form:[code]<form name="rating" method="post" action="ratejoke.php"> <font class="subfont"><P align="center">Rate this Joke:</font> <select name="userrating"> <option value="5.0" selected>5</option> <option value="4.0">4</option> <option value="3.0">3</option> <option value="2.0">2</option> <option value="1.0">1</option> <option value="0.0">0</option> <input type="hidden" name="jokeID" value="<?=$jokeID?>"> <input type="submit" value="Go!"> </select> </p> </form>[/code] Link to comment https://forums.phpfreaks.com/topic/32097-need-help-with-form-and-php-communication/#findComment-149014 Share on other sites More sharing options...
alpine Posted December 28, 2006 Share Posted December 28, 2006 [quote author=craygo link=topic=120186.msg492780#msg492780 date=1167336461]can use this[code]<input type="hidden" name="jokeID" value="<?=$jokeID?>">[/code][/quote]Naughty Ray - no offence, but you shouln't teach the bad habbit of using shortcode :(EDIT: twilightnights: missing semicolon after --> [color=blue]$jokeID = $_POST['jokeID'][/color][b][color=red];[/color][/b] Link to comment https://forums.phpfreaks.com/topic/32097-need-help-with-form-and-php-communication/#findComment-149019 Share on other sites More sharing options...
twilightnights Posted December 28, 2006 Author Share Posted December 28, 2006 thx for help, I added the semicolon and still can't get it to work. I also removed the $update_query variable as I didn't call it anywhere, didn't notice that.[code]<?phpsession_start();include("dbinfo.inc.php");include("dbconnect.inc.php");$jokeID = $_POST['jokeID'];$userrating = $_POST['userrating'];if (session_is_registered("rating.$jokeID")) { echo "<center>Sorry! You have already voted!"; } else { $get_count = mysql_query("SELECT rating, votes FROM '$table' WHERE jokeID='$jokeID'"); while($r = mysql_fetch_assoc($get_count)){extract($r);} $new_count = ($votes + 1); $tut_rating2 = ($rating * $votes); $new_rating = (($userrating + $tut_rating2) / ($new_count)); $new_rating2 = number_format($new_rating, 2, '.', ''); mysql_query("UPDATE '$table' SET rating='$new_rating2',votes='$new_count' WHERE jokeID='$jokeID'"); $sessionvar = "rating.$jokeID"; session_register($sessionvar); echo "<div align="center"><b> <p>Thanks for your vote!</p> <p>The new rating for this joke is: <br> <br> $new_rating2 out of 5</p>";mysql_close(); } echo "<p align="center"><a href="javascript:history.back();"><< Back</a> | <a href="/index.php">Main Page</a></b><br> </p>";?> [/code] Link to comment https://forums.phpfreaks.com/topic/32097-need-help-with-form-and-php-communication/#findComment-149033 Share on other sites More sharing options...
kenrbnsn Posted December 28, 2006 Share Posted December 28, 2006 Do not use the functions session_is_registered(), instead check to see if the session variable is setChange this:[code]<?phpif (session_is_registered("rating.$jokeID"))?>[/code]to[code]<?phpif (isset($_SESSION["rating.$jokeID"]))?>[/code]Also, do not use the function session_register().Change:[code]<?php $sessionvar = "rating.$jokeID"; session_register($sessionvar);?>[/code]to[code]<?php $sessionvar = "rating.$jokeID"; $_SESSION[$sessionvar] = $sessionvar;?>[/code]You are using unescaped double quotes in strings that are delimited by double quotes.Change:[code]<?php echo "<div align="center"><b> <p>Thanks for your vote!</p> <p>The new rating for this joke is: <br> <br> $new_rating2 out of 5</p>";?>[/code]to[code]<?php echo "<div align=\"center\"><b> <p>Thanks for your vote!</p> <p>The new rating for this joke is: <br> <br> $new_rating2 out of 5</p>";?>[/code]and this:[code]<?php echo "<p align="center"><a href="javascript:history.back();"><< Back</a> | <a href="/index.php">Main Page</a></b><br> </p>";?>[/code]to[code]<?php echo "<p align=\"center\"><a href=\"javascript:history.back();\"><< Back</a> | <a href=\"/index.php\">Main Page</a></b><br> </p>";?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/32097-need-help-with-form-and-php-communication/#findComment-149036 Share on other sites More sharing options...
twilightnights Posted December 28, 2006 Author Share Posted December 28, 2006 thanks a bunch. It appears the session thing is working find how I set it, but the problem with the not using escape characters it what made it not post. Right now it posts fine.. but when I try to run the mysql command on my database it says incorrect syntax. Substituting variables for actual names[code]$query="UPDATE '$table' SET rating='$new_rating2',votes='$new_count' WHERE jokeID='$jokeID'";[/code] Link to comment https://forums.phpfreaks.com/topic/32097-need-help-with-form-and-php-communication/#findComment-149040 Share on other sites More sharing options...
kenrbnsn Posted December 29, 2006 Share Posted December 29, 2006 On the mysql statement where this error is occurring do something like:[code]<?php$query="UPDATE '$table' SET rating='$new_rating2',votes='$new_count' WHERE jokeID='$jokeID'";$rs = mysql_query($query) or die("Problem with query: $query<br>" . mysql_error());?>[/code]This should show where the problem is happening.Ken Link to comment https://forums.phpfreaks.com/topic/32097-need-help-with-form-and-php-communication/#findComment-149168 Share on other sites More sharing options...
craygo Posted December 29, 2006 Share Posted December 29, 2006 [quote author=alpine link=topic=120186.msg492839#msg492839 date=1167343523]Naughty Ray - no offence, but you shouln't teach the bad habbit of using shortcode :([/quote]Sorry but what is wrong with using short code. Is there a problem with it or something?? Compatability??Not being sarcastic or anything just wondering if I should stop doing it. I'm all about typing as little as possible :) Link to comment https://forums.phpfreaks.com/topic/32097-need-help-with-form-and-php-communication/#findComment-149370 Share on other sites More sharing options...
kenrbnsn Posted December 29, 2006 Share Posted December 29, 2006 Using short tags may not be enabled on every host. Here's what the PHP manual says about the use of short tags:[quote][b]Note:[/b] Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags.[/quote]Ken Link to comment https://forums.phpfreaks.com/topic/32097-need-help-with-form-and-php-communication/#findComment-149372 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.