I am following this tutorial http://www.css-resources.com/code-for-php-photo-gallery.html to create php photo gallery but i get errors undefined index photos, categories and cy when i run this code, it gets the category names in a textarea and adds them in the db, in the output also says no database selected but i have selected it in the config.php. Please help!!
<body onload='fix()'>
<?php
include_once ("config.php");
$pattern1 = '/[^a-zA-Z0-9\\_\\,]/i';
$pattern2 = '/[^a-zA-Z0-9\\.\\,\\-\\_]/i';
$replacement = '';
$P=$_POST['photo'];
$C=$_POST['category'];
$CY=$_POST['cy'];
$P=strip_tags($P);
$C=strip_tags($C);
$P=preg_replace($pattern2, $replacement, $P);
$C=preg_replace($pattern1, $replacement, $C);
$C=mysql_real_escape_string($C);
$P=mysql_real_escape_string($P);
if (strlen($P)<5 && strlen($C)<5) { unset($P);unset($C);
echo "<div id='form' class='form'>
<form name='myform' method='post' action='cms-write-photo-gallery.php' onsubmit='bye()'>
<table width='700' border='0' cellpadding='2' cellspacing='2' align='center'>
<tr>
<td width='60'>Categories separated by commas, e.g.: fish,birds,mammals,insects</td><br>
<td><textarea name='category' cols='50' rows='11'></textarea></td>
</tr>
<tr>
<td> </td><td><input name='save' type='submit' value='Save Categories to DB'>
<input name='reset' type='reset' value='Reset'></td></tr>
</table>
</form>
</div>";
}
if (strlen($C)>4){
$ct = explode(",", $C);$n=count($ct);
for ($i=0;$i<$n;$i++) {
mysql_query("INSERT INTO categories_ (id, category)
VALUES ('','".$ct[$i]."')") or die('Error ,saving failed');}
$rc = mysql_affected_rows();
if ($rc>0){echo '<script language="javascript">alert("The saving was successfully accomplished.");</script>';}
else{echo '<script language="javascript">alert("The saving was unsuccessful.");</script>';}
}
$cat=array();
$res = mysql_query("SELECT category FROM categories_ order by category") or die(mysql_error());
while ($row = mysql_fetch_row($res)) {
array_push ($cat, $row[0]);
}
$num_cats_in_table=mysql_num_rows($res);
if (strlen($P)>4||strlen($C)>4){unset($C);
echo "<div class='form2'><form name='myform2' method='post' action='cms-write-photo-gallery.php'><table width='700' border='0' cellpadding='2' cellspacing='2' align='center'><tr><td width='60'>Photos separated by commas, e.g.: bear.jpg,horse.jpg,cow.jpg</td><td><textarea name='photo' cols='50' rows='11'></textarea></td></tr><tr><td>Category</td><td><select name='cy'>";
for ($i=0;$i<$num_cats_in_table;$i++) {
echo "<option value='".$cat[$i]."'>".$cat[$i]."</option>";}
echo "</select></td></tr><tr><td> </td><td><input name='save2' type='submit' value='Save Photos to DB'>
<input name='reset2' type='reset' value='Reset'></td></tr></table></form></div>";
}
if (strlen($P)>4){
$ph = explode(",", $P);$nn=count($ph);
for ($i=0;$i<$nn;$i++) {
mysql_query("INSERT INTO photos (id, photo, category)
VALUES ('','".$ph[$i]."','$CY')") or die('Error ,saving failed');}
$rc = mysql_affected_rows();
if ($rc>0){unset($P);unset($CY);
echo '<script language="javascript">alert("The saving was successfully accomplished.");</script>';}
else{echo '<script language="javascript">alert("The saving was unsuccessful.");</script>';}
}
mysql_close();
?>
<div id='top' class='url'>
<h1>Creating Photo Gallery—Content Management System (CMS)</h1>
<div id='info' class='info'>Use letters, numbers, hyphens, dots and underscores only in photos. Use letters, numbers, and underscores only in categories. Categories and photos must be separated by commas with NO SPACES ANYWHERE and each category or photo must be under 30 characters long, e.g.: <b>fish,birds,mammals,insects</b>. Submit comma-separated categories first, then submit comma-separated photos, selecting category from the dropdown.<br></div>
</div>
<script language="javascript">
function bye(){e=document.getElementById('form');e.style.display='none';}
</script>
<?php include("navi.html"); ?>
</body>