Jump to content

Need some serious help


grunger

Recommended Posts

Have a little problem with a so simple script. Fixed errors for like 5 hours but they never stop coming new ones.

 

 <html
<title>add to db</title>
<body>
<table width="550" border="0" cellpadding="2" cellspacing="1">
  <tr> 
   <td width="100">Url *</td> <td> 
    <input name="url" type="text" id="url" size="30" maxlength="300"></td>
</tr>
  <tr> 
   <td width="100">Thumb url</td>
   <td> 
    <input name="thumb_url" type="text" id="url" size="30" maxlength="300"></td>
</tr>
  <tr> 
   <td width="100">Name</td>
   <td> 
    <input name="name" type="text" id="name" size="30" maxlength="100"></td>
</tr>
  <tr> 
   <td width="100">Renderer</td> <td> 
    <input name="renderer" type="text" id="renderer" maxlength="100"></td>
</tr>
  <tr> 
   <td width="100"> </td>
   <td> 
    <input name="btnSign" type="submit" id="btnSign" value="Add render" onClick="return checkForm();"></td>
</tr>
</table>
</form>
<?php
// include the database configuration and
// open connection to database
include 'config.php';
include 'opendb.php';

// check if the form is submitted
if(isset($_POST['btnSign']))
{
    // get the input from $_POST variable
    // trim all input to remove extra spaces
    $url    		= trim($_POST['url']);
    $thumb_url   	= trim($_POST['thumb_url']);
    $name     		= trim($_POST['name']);
    $renderer 		= trim($_POST['renderer']);

	    // prepare the query string
    $query = "INSERT INTO gallery_db (ID, url, thumb_url, name, renderer) " .
             "VALUES ('0', '$url', '$thumb_url', '$name', '$renderer')";
		 	// execute the query to insert the input to database
    // if query fail the script will terminate         
    mysql_query($query) or die('Error, query failed. ' . mysql_error());

    exit;
?>
<form method="post" name="galleryform">

<?php
// =======================
// Show database
// =======================
// how many render entries to show per page
$rowsPerPage = 10;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use the value as page number
if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
}

// counting the offset ( where to start fetching the entries )
$offset = ($pageNum - 1) * $rowsPerPage;

// prepare the query string
$query = "SELECT ID, url, thumb_url, name, renderer) ".
         "FROM gallery_db ".
         "ORDER BY id DESC ".            // using ORDER BY to show the most current entry first
         "LIMIT $offset, $rowsPerPage";  // LIMIT is the core of paging

// execute the query 
$result = mysql_query($query) or die('Error, query failed. ' . mysql_error());

// if the database is empty show a message
if(mysql_num_rows($result) == 0)
{
?>
<p><br>
<br>Database is empty </p>
<?php
}
else
{
    // get all database entries
    while($row = mysql_fetch_array($result))
    {
        // list() is a convenient way of assign a list of variables
        // from an array values 
        list($url, $thumb_url, $name, $renderer) = $row;

        // change all HTML special characters,
        // to prevent some nasty code injection
        $name    = htmlspecialchars($name);
        $message = htmlspecialchars($message);        

?>
<table width="550" border="1" cellpadding="2" cellspacing="0">
<tr> 
  <td width="80" align="left"> <a href="Name:<?=$name;?>" class="name"> 
   <?php $renderer;?>
   </a> </td>
  <td align="right"><small> 
   <?php $url;?>
   </small></td>
</tr>
<tr> 
  <td> 
   <?php $thumb_url;?>
   <?php
        }
?>
  </td>
</tr>
</table>
<br>
<?php
    } // end while

// below is the code needed to show page numbers

// count how many rows we have in database
$query   = "SELECT COUNT(ID) AS numrows FROM gallery_db";
$result  = mysql_query($query) or die('Error, query failed. ' . mysql_error());
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

// how many pages we have when using paging?
$maxPage  = ceil($numrows/$rowsPerPage);
$nextLink = '';

// show the link to more pages ONLY IF there are 
// more than one page
if($maxPage > 1)
{
    // this page's path
    $self     = $_SERVER['PHP_SELF'];
    
    // we save each link in this array
    $nextLink = array();
    
    // create the link to browse from page 1 to page $maxPage
    for($page = 1; $page <= $maxPage; $page++)
    {
        $nextLink[] =  "<a href=\"$self?page=$page\">$page</a>";
    }
    
    // join all the link using implode() 
    $nextLink = "Go to page : " . implode(' » ', $nextLink);
}

// close the database connection since
// we no longer need it
include 'closedb.php';

?>
<table width="550" border="0" cellpadding="2" cellspacing="0">
<tr> 
  <td align="right" class="text"> 
   <?php $nextLink;?> 
  </td>
</tr>
</table>
<?php
}
?>
</body>
</html> 

 

Hope you guys can help me

Link to comment
https://forums.phpfreaks.com/topic/37940-need-some-serious-help/
Share on other sites

 

 

Can you end a php script with an unresolved if statement?

 

if(mysql_num_rows($result) == 0)
{
?>
<p><br>
<br>Database is empty </p>
<?php
}
else

 

This part I think you need to put echo $name; echo $renderer; echo $url;

You are not actually outputting anything to your browser by just stating the name

of the variable.

 

<tr> 
  <td width="80" align="left"> <a href="Name:<?=$name;?>" class="name"> 
   <?php $renderer;?>
   </a> </td>
  <td align="right"><small> 
   <?php $url;?>
   </small></td>
</tr>
<tr> 
  <td> 
   <?php $thumb_url;?>

 

There could be other things wrong I"m not sure but that's two things I noticed

All insert's have the or die and no errors are coming.

The else should work without a statement...

Tryed to rewrite all the

<?php $name ?>

to

<?php echo "$name";

 

Code now

 <html
<title>add to db</title>
<body>
<table width="550" border="0" cellpadding="2" cellspacing="1">
  <tr> 
   <td width="100">Url *</td> <td> 
    <input name="url" type="text" id="url" size="30" maxlength="300"></td>
</tr>
  <tr> 
   <td width="100">Thumb url</td>
   <td> 
    <input name="thumb_url" type="text" id="url" size="30" maxlength="300"></td>
</tr>
  <tr> 
   <td width="100">Name</td>
   <td> 
    <input name="name" type="text" id="name" size="30" maxlength="100"></td>
</tr>
  <tr> 
   <td width="100">Renderer</td> <td> 
    <input name="renderer" type="text" id="renderer" maxlength="100"></td>
</tr>
  <tr> 
   <td width="100"> </td>
   <td> 
    <input name="btnSign" type="submit" id="btnSign" value="Add render" onClick="return checkForm();"></td>
</tr>
</table>
</form>
<?php
// include the database configuration and
// open connection to database
include 'config.php';
include 'opendb.php';

// check if the form is submitted
if(isset($_POST['btnSign']))
{
    // get the input from $_POST variable
    // trim all input to remove extra spaces
    $url    		= trim($_POST['url']);
    $thumb_url   	= trim($_POST['thumb_url']);
    $name     		= trim($_POST['name']);
    $renderer 		= trim($_POST['renderer']);

	    // prepare the query string
    $query = "INSERT INTO gallery_db (ID, url, thumb_url, name, renderer) " .
             "VALUES ('0', '$url', '$thumb_url', '$name', '$renderer')";
		 	// execute the query to insert the input to database
    // if query fail the script will terminate         
    mysql_query($query) or die('Error, query failed. ' . mysql_error());

    exit;
?>
<form method="post" name="galleryform">

<?php
// =======================
// Show database
// =======================
// how many render entries to show per page
$rowsPerPage = 10;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use the value as page number
if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
}

// counting the offset ( where to start fetching the entries )
$offset = ($pageNum - 1) * $rowsPerPage;

// prepare the query string
$query = "SELECT ID, url, thumb_url, name, renderer) ".
         "FROM gallery_db ".
         "ORDER BY ID DESC ".            // using ORDER BY to show the most current entry first
         "LIMIT $offset, $rowsPerPage";  // LIMIT is the core of paging

// execute the query 
$result = mysql_query($query) or die('Error, query failed. ' . mysql_error());

// if the database is empty show a message
if(mysql_num_rows($result) == 0)
{
?>
<p><br>
<br>Database is empty </p>
<?php
}
else
}
    // get all database entries
    while($row = mysql_fetch_array($result))
    {
        // list() is a convenient way of assign a list of variables
        // from an array values 
        list($url, $thumb_url, $name, $renderer) = $row;

        // change all HTML special characters,
        // to prevent some nasty code injection
        $url    = htmlspecialchars($url);
        $thumb_url = htmlspecialchars($thumb_url);    
	$name = htmlspecialchars ($name);
	$renderer = htmlspecialchars ($renderer);    

?>
<table width="550" border="1" cellpadding="2" cellspacing="0">
<tr> 
  <td width="80" align="left"> <a href="Name:<?php echo "$name";?>" class="name"> 
   <?php echo "$renderer;"?>
   </a> </td>
  <td align="right"><small> 
   <?php echo "$url";?>
   </small></td>
</tr>
<tr> 
  <td> 
   <?php echo "$thumb_url";?>
   <?php
        }
?>
  </td>
</tr>
</table>
<br>
<?php
    } // end while

// below is the code needed to show page numbers

// count how many rows we have in database
$query   = "SELECT COUNT(ID) AS numrows FROM gallery_db";
$result  = mysql_query($query) or die('Error, query failed. ' . mysql_error());
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

// how many pages we have when using paging?
$maxPage  = ceil($numrows/$rowsPerPage);
$nextLink = '';

// show the link to more pages ONLY IF there are 
// more than one page
if($maxPage > 1)
{
    // this page's path
    $self     = $_SERVER['PHP_SELF'];
    
    // we save each link in this array
    $nextLink = array();
    
    // create the link to browse from page 1 to page $maxPage
    for($page = 1; $page <= $maxPage; $page++)
    {
        $nextLink[] =  "<a href=\"$self?page=$page\">$page</a>";
    }
    
    // join all the link using implode() 
    $nextLink = "Go to page : " . implode(' » ', $nextLink);
}

// close the database connection since
// we no longer need it
include 'closedb.php';

?>
<table width="550" border="0" cellpadding="2" cellspacing="0">
<tr> 
  <td align="right" class="text"> 
   <?php echo "$nextLink";?> 
  </td>
</tr>
</table>
<?php
}
?>
</body>
</html> 

 

Still need help :D

 

If anybody could make a similar code that worked it would be even nicer.

Archived

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

×
×
  • 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.