Jump to content


Photo

jokes script ---->need help


  • Please log in to reply
11 replies to this topic

#1 ahmed17

ahmed17
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 13 September 2006 - 01:01 PM

hello, i write this script but not add any  joke to my database 
<?php
/* ============================================================= */
/*                                                               */
/*                                                               */
/*    Author:eng:ahmed _bohoty                                   */
/*                                                               */
/*                                                               */
/*                                                               */
/* ============================================================= */
?>

<html>
<head>
       <title>The Internet Joke Database</title>
</head>
<body>
<?php

$host="localhost";                //the name of localhost
$user="";               //username
$pass="";             // mypasswd
$database="ahmed17";  //database name which created
$table="jokes";   // table name

if (isset($addjoke)): // If the user wants to add ajoke

// Connect to the database server
$link=mysql_connect($host,$user,$pass)or print"failed connection";
// Select the jokes database
mysql_select_db($database,$link)or print"Could not open database";

////////////////////////////////////////////////////////////////|\
/*$sql = "CREATE TABLE Jokes (                                  \\
         ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,            \\
         JokeText TEXT,                                         \\
         JokeDate DATE NOT NULL                                 \\
       )";                                                      \\
if ( @mysql_query($sql) ) {                                     \\
  echo("<p>Jokes table successfully                             \\
created!</p>")                                                  \\
} else {                                                        \\
  echo("<p>Error creating Jokes table: " . mysql_error()        \\
. "</p>");                                                      \\
}                                                               \\
///////////////////////////////////////////////////////////////*/


//******************************************************************************
// If a joke has been submitted,
// add it to the database.
if($submitjoke==$SUBMIT)
{
 $sql="INSERT INTO $table SET JokeText='$joketext',JokeDate='CURDATE()'";
 if(mysql_query($sql))
{
     print("Your joke has been added.</p>");
}
 else
 {
     print("Error adding submitted joke".mysql_error());
 }
}
//******************************************************************************

print"<p> Here are all the jokes in our database:</p> ";

//******************************************************************************

// Request the text of all the jokes

$result=mysql_query("SELECT joketext FROM $table");
if(!$result)
{
    print"<b>Error Performing Query</b>".mysql_error();
    exit();
}

//******************************************************************************

// Display the text of each joke in a paragraph
while($row=mysql_fetch_array($result))
{
 print("<p>" . $row["JokeText"] . "</p>");
}

//******************************************************************************
    // When clicked, this link will load this page
    // with the joke submission form displayed.

echo("<p><a href='$PHP_SELF?addjoke=1'>Add aJoke!</a></p>");


//******************************************************************************
 endif;
?>
<form action="<?=$PHP_SELF?>"
method="post">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40"
wrap></textarea><br />
<input type="submit" name="submitjoke" value="SUBMIT"
/></p>
</form>
</body>
</html>


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 September 2006 - 01:09 PM

Looks like your server doesnt have register_globals enabled. So you'll need to use the superglobal arrays to access your user input.

For example instead of using $PHP_SELF you should use $_SERVER['PHP_SELF'] instead. The same applies ot $joketext you shold use $_POST['joketext'] instead.

You'll need to go through your code changing most of the variables to thier superglobal equivalent.

#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 13 September 2006 - 01:12 PM

either you have not set a whole bunch of variables, or else you seem to be coding with register_globals set to ON.  I hope that you just didn't set a bunch of variables, because you shouldn't code with register_globals set to ON.

if($submitjoke==$SUBMIT)

if register_globals is on, then this needs to be

if($submitjoke=='SUBMIT')

if they are off (like they should be) then it needs to look like this:

if($_POST['submitjoke']=='SUBMIT')

if globals are off, then $joketext needs to be replaced with $_POST['joketext'] but either way, unless you want people to inject malicious sql coding into your query, you need to sanitize that variable, instead of inserting it directly into your query.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 ahmed17

ahmed17
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 13 September 2006 - 05:26 PM

thanks  wildteen.&.Crayon  :)
i try more but no benfit...wat i do ?
if u can test script and tell me if script work or not ..and give me afree server which support php good




#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 September 2006 - 05:35 PM

If you wnat to test your PHP scripts without it being hosted on the web then install AMP (Apache PHP and MySQL) by getting either WAMP (for Windows) or XAMPP (for Windows, Mac or Linix) both are free to use. Once installed you can goto http://localhost/ and you'll be able to run your PHP scripts of off your computer.

Did you try our suggestions? If you did could you post the updated code here.

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 13 September 2006 - 05:36 PM

uh...ahmed, that's not really how things work around here... "you try but no benefit" - so what is going wrong? repost your modified code.

and...am i not understanding you, or did you just ask for free hosting?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 ahmed17

ahmed17
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 13 September 2006 - 08:39 PM

I AM SORRY ..BUT I MADE ALL CAHNGES BUT NO DATA ADDED TO MY DATABASE

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 September 2006 - 08:43 PM

Thast why we are requesting to see your code. So we can see what chnages you ahve made to your code.

So can you please repost the modified code here please so we can review it for any errors or problems we can see.

#9 ahmed17

ahmed17
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 14 September 2006 - 06:21 PM

no jokes added to my database
here's the code after changes


<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
       <title>Title here!</title>
</head>
<body>
<?php
$host="localhost";                //the name of localhost
$user="ahmed17";               //username
$pass="123456";              // mypasswd
$database="ahmed17";      //database name which created
$table="jokes";        // table name

$link = mysql_connect('localhost', "$user", "$pass")or print"failed connection";
 mysql_select_db($database,$link)or print "not exists";
 $sql="SELECT * FROM $table";
$result= mysql_db_query($database,$sql,$link);
$num=mysql_num_rows($result);
print"<li>number of fields:    $num<br><br></li>";
 echo"**********************<br>";
while($row=mysql_fetch_array($result))
{

   echo $row['id'];
   echo"<br>";
   echo $row['JokeText'];
    echo"<br>";
   echo $row['JokeDate'];
     echo"<br>";
     echo"**********************<br>";

   }
?>
</body>
</html>


#10 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 14 September 2006 - 07:39 PM

Try the below code

<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
      <title>Title here!</title>
</head>
<body>
<?php
$host="localhost";                //the name of localhost
$user="ahmed17";              //username
$pass="123456";              // mypasswd
$database="ahmed17";      //database name which created
$table="jokes";        // table name

$link = mysql_connect('localhost', "$user", "$pass") or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$sql="SELECT * FROM '$table'";
$result= mysql_db_query($database,$sql,$link);
$num=mysql_num_rows($result);
if($num > 0)
{
print"<li>number of fields:    $num<br><br></li>";
echo"**********************<br>";
while($row=mysql_fetch_array($result))
{

  echo $row['id'];
  echo"<br>";
  echo $row['JokeText'];
    echo"<br>";
  echo $row['JokeDate'];
    echo"<br>";
    echo"**********************<br>";

  }
}
else
echo("There is no Jokes to be displayed");
?>
</body>
</html>
www.karthi.us

#11 ahmed17

ahmed17
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 15 September 2006 - 06:41 PM

code after changes ..but not work
<?php
/* ============================================================= */
/*                                                               */
/*                                                               */
/*    Author:eng:ahmed _bohoty                                   */
/*                                                               */
/*                                                               */
/*                                                               */
/* ============================================================= */
?>

<html>
<head>
       <title>The Internet Joke Database</title>
</head>
<body>
<?php

$host="localhost";                //the name of localhost
$user="ahmed17";               //username
$pass="123456";              // mypasswd
$database="ahmed17";      //database name which created
$table="jokes";        // table name

//********************************************************

if (isset($addjoke)): // If the user wants to add ajoke

// Connect to the database server
$link=mysql_connect($host,$user,$pass)or print"failed connection";
// Select the jokes database
mysql_select_db($database,$link)or print"Could not open database";

////////////////////////////////////////////////////////////////|\
/*$sql = "CREATE TABLE Jokes (                                  \\
         ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,            \\
         JokeText TEXT,                                         \\
         JokeDate DATE NOT NULL                                 \\
       )";                                                      \\
if ( @mysql_query($sql) ) {                                     \\
  echo("<p>Jokes table successfully                             \\
created!</p>")                                                  \\
} else {                                                        \\
  echo("<p>Error creating Jokes table: " . mysql_error()        \\
. "</p>");                                                      \\
}                                                               \\
///////////////////////////////////////////////////////////////*/


//******************************************************************************
// If a joke has been submitted,
// add it to the database.
if($_POST['submitjoke']=='SUBMIT')
{
 $sql="INSERT INTO $table SET JokeText='$_POST['joketext'] ',JokeDate='CURDATE()'";
 if(mysql_query($sql))
{
     print("Your joke has been added.</p>");
}
 else
 {
     print("Error adding submitted joke".mysql_error());
 }
}
//******************************************************************************

print"<p> Here are all the jokes in our database:</p> ";

//******************************************************************************

// Request the text of all the jokes

$result=mysql_query("SELECT joketext FROM $table");
if(!$result)
{
    print"<b>Error Performing Query</b>".mysql_error();
    exit();
}

//******************************************************************************

// Display the text of each joke in a paragraph
while($row=mysql_fetch_array($result))
{
 print("<p>" . $row["JokeText"] . "</p>");
}

//******************************************************************************
    // When clicked, this link will load this page
    // with the joke submission form displayed.

echo("<p><a href='$PHP_SELF?addjoke=1'>Add aJoke!</a></p>");


//******************************************************************************
 endif;
?>
<form action="<?=$_SERVER['PHP_SELF']?>"
method="post">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40"
wrap></textarea><br />
<input type="submit" name="submitjoke" value="SUBMIT"
/></p>
</form>
</body>
</html>


#12 ahmed17

ahmed17
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 15 September 2006 - 10:15 PM

..............?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users