Jump to content

Archived

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

ahmed17

jokes script ---->need help

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]

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


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


Share this post


Link to post
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.

Share this post


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

Share this post


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

Share this post


Link to post
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]

Share this post


Link to post
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>

Share this post


Link to post
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]

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.