I’m having some problems updating a mySQL table with my PHP script. I am trying to design a page where you can create a category (for clothing) and insert it into table called categories. As there will be subcategories I have created a table, called subcategories, which will contain the parent_id and child_id.
No matter what I do I can’t insert, update or delete to the new table. Any ideas? I’ve included my code.
Thanks!
Neil
<?php
include(‘../../include/site_config.php’);
include(‘../../include/admin_config.php’);
checklogin(‘categories’);
$delete = $_GET[‘delete’];
$category = $
$edit_id = $_GET[‘edit_id’];
$edit = $
$new_name1= strtolower(trim(ereg_replace(” “,””,$_GET[‘new_name’])));
$category2= strtolower(trim(ereg_replace(” “,””,$
$dir1 = “../../” . $new_name1;
$dir2 = “../../” . $category2;
if(!empty($delete))
{
$sql = “DELETE FROM categories WHERE id=’$delete’ LIMIT 1”;
mysql_query($sql,$cn);
}
if(!empty($new_name))
{
$sql = “UPDATE categories SET category='”.$new_name1.”‘, link='”.$new_name1.”‘ WHERE id='”.$edit_id.”‘”;
mysql_query($sql,$cn);
$old_umask = umask(0);
@mkdir($dir1, 01777);
$file = “./../../” . $new_name1 . “/” . “index.php”;
$fp = fopen (“$file”, “wb”);
$content = ‘<? $category=”‘ . $new_name1 . ‘”; include (“../category.php”) ?>’;
fwrite($fp, $content);
fclose($fp);
}
if(!empty($category))
{
$sql = “INSERT INTO categories (category, link) VALUES(‘”.$category.”‘, ‘”.$category2.”‘)”;
mysql_query($sql,$cn);
$old_umask = umask(0);
@mkdir($dir2, 01777);
$file = “./../../” . $category2 . “/” . “index.php”;
$fp = fopen (“$file”, “wb”);
$content = ‘<? $category=”‘ . $category . ‘”; include (“../category.php”) ?>’;
fwrite($fp, $content);
fclose($fp);
}
$sql = “SELECT * FROM categories ORDER BY category”;
$result = mysql_query($sql,$cn);
?>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 3.2 Final//EN”>
<html>
<head>
<title>Categories Index</title>
<link rel=’stylesheet’ href=’../admin.css’>
</head>
<body bgcolor=”#FFFFFF” text=”#000000″ link=”#FF0000″ vlink=”#800000″ alink=”#FF00FF”>
<center>
<form action='<?=$PHP_SELF?>’ method=’get’>
<table cellpadding=’3′ cellspacing=’0′>
<tr>
<td class=’mainHeader’ colspan=’2′>Categories Index</td>
</tr>
<tr>
<td class=’formTitleLeft’>Index</td>
<td class=’searchResult’> </td>
</tr>
<?
while($row = mysql_fetch_assoc($result))
{
?>
<tr>
<td><?=$row[‘category’]?></td>
<td class=’searchResult’ align=’center’><a href='<?=$PHP_SELF?>?edit=<?=$row[‘id’]?>’><img border=’0′ src=’../images/edit.gif’></a> <a href='<?=$PHP_SELF?>?delete=<?=$row[‘id’]?>’ onClick=’return confirm(“Are you Sure?nThis cannot be undone.”);’><img border=’0′ src=’../images/delete.gif’></a></td>
</tr>
<?
}
?>
<tr>
<td><input type=’text’ name=’category’></td>
<td class=’searchResult’ align=’center’><input type=’submit’ value=’Add Category’></td>
</tr>
<?
if(!empty($edit))
{
$sql = “SELECT * FROM categories WHERE id='”.$edit.”‘”;
$result = mysql_query($sql,$cn);
$row = mysql_fetch_assoc($result);
?>
<tr>
<td colspan=’2′><br /><br /></td>
</tr>
<tr>
<td class=’mainHeader’ colspan=’3′>Edit Category</td>
</tr>
<tr>
<td align=’center’ colspan=’2′>
<input type=’hidden’ name=’edit_id’ value='<?=$row[‘id’]?>’>
</td>
</tr>
<tr>
<td align=’center’ colspan=’2′>
<input type=’text’ name=’new_name’ value='<?=$row[‘category’]?>’>
</td>
</tr>
<tr>
<td align=’center’ colspan=’2′>
<?
$SQL_2 = “SELECT
$RESULT_2 = MYSQL_QUERY($SQL_2);
?>
<select name=”parent_id3″ onChange=”populate3(myfrm)” width=”400″>
<option value=”0″ selected>- – – – SELECT A CATEGORY – – – -</option>
<?
WHILE($ROW_2 = MYSQL_FETCH_ARRAY($RESULT_2))
{
ECHO ‘<option value=”‘.$ROW_2[‘id’].'”>’.$ROW_2[‘category’].”</option> n”;
}
?>
</select>
</td>
</tr>
<tr>
<td class=’searchResult’ align=’center’ colspan=’2′><input type=’submit’ value=’Update Category’></td>
</tr>
<tr>
<td colspan=’2′ align=’center’>
<a href=”set_image.php?id=<?=$row[‘id’]?>”>Change Image</a>
</td>
</tr>
<?
}
?>
</table>
</form>
</center>
</body>
</html>