Jump to content

jokes script ---->need help


ahmed17

Recommended Posts

hello, i write this script but not add any  joke to my database 
[code]<?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>[/code]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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


[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 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>[/code]
Link to comment
Share on other sites

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

code after changes ..but not work
[code]
<?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>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.