I am getting mad on this simple code lol 😃
Making a part of a gallery and have fixed one and one errors in like 5 hours but it never stops with errors.
<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>
This should be realy simple so maybe you could help me.
All the included files work perfectly fine as i have used them with other scripts