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>&nbsp;</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>&nbsp;</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>

Hi,

First to address the database error. I'm not seeing mysql_connect anywhere in the code above. Is the database call made into config.php?

You might want to rewrite that since mysql_connect and related functions are deprecated. Click Here to read more about that

Also you're not providing the initial HTML form so it's hard to see if you might have a typo in the field names but I'm thinking you might not be "POSTING" your form.

I don't know how familiar you are with forms and PHP but there are 2 ways to send information from a form.

  1. get - Get is similar to writing a URL like daniweb.com/form.php?var1=value1&var2=value2
  2. post - submits data to be processed to the identified resource.

Here is a great explanation

So in a nut shell if your HTML form doesn't state what method to use it will default to "get" and PHP will get the values from the $_GET global variable instead of $_POST. Make sure your form states method="post". :)

my database call is made in the config.php , here's the code :

<? 
$con = mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('agenda',$con) or die(mysql_error()); 
?>

My form states "post" method i checked it

Hmm ok...

Well if your script dies and reports an error with no database selected then it must mean the mysql_connect and mysql_select_db are not called for some reason.

Do you mind posting the full config.php code? Just obfuscate any sensitive data (as opposed to simply removing it like your password in the previous post). A note on your connect function it is not recommended to use root in a production setting, I even frown on using root in development too. You should create a specific user for that database and give it specific rights to select, update, delete etc...